51

I'm currently trying to run process using spawn. What I am trying to run from shell is the following;

NODE_ENV=production node app/app.js

Here's the code to run that;

var spawn = require('child_process').spawn;
var start = spawn('NODE_ENV=production',['node','app/app.js']);

However, I got the following error;

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: spawn ENOENT
    at errnoException (child_process.js:980:11)
    at Process.ChildProcess._handle.onexit (child_process.js:771:34)

How can I do that using spawn ?

tanguy_k
  • 11,307
  • 6
  • 54
  • 58
aacanakin
  • 2,844
  • 4
  • 25
  • 42

4 Answers4

109

Your usage of spawn is not correct:

spawn( command, args, options ):

Launches a new process with the given command, with command line arguments in args. If omitted, args defaults to an empty Array.

The third argument is used to specify additional options, which defaults to:

{ cwd: undefined, env: process.env }

Use env to specify environment variables that will be visible to the new process, the default is process.env.

So the env variable NODE_ENV should be provided on the options argument:

// ES6 Object spread eases extending process.env
spawn( 'node', ['app.js'], { env: { ...process.env, NODE_ENV: 'test' } }})

See also How do I debug "Error: spawn ENOENT" on node.js?

laconbass
  • 17,080
  • 8
  • 46
  • 54
  • 8
    With ES6/7 Stage 3 syntax you can do this in one line using object spread syntax - `spawn('node', ['app.js'], {...process.env, NODE_ENV: 'test})` – jlyonsmith Nov 14 '17 at 23:03
16

Syntax: spawn(command, [args], [options])

var spawn = require('child_process').spawn;
var start = spawn('node', ['app.js'], {env: {NODE_ENV: 'production'}});

Or keep default ENV variables:

var spawn = require('child_process').spawn;

var productionEnv = process.env; // TODO should clone process.env like utils.extend
productionEnv.NODE_ENV = 'production';

var start = spawn('node', ['app.js'], {env: productionEnv});

My test:

app.js

console.log(process.env);

spawn.js

var spawn = require('child_process').spawn;
var start = spawn('node', ['app.js'], {env: {NODE_ENV: 'production'}});

start.stdout.pipe(process.stdout);

from terminal:

node spawn

output:

{ NODE_ENV: 'production' }
damphat
  • 18,246
  • 8
  • 45
  • 59
8

This worked for me

var spawn = require('child_process').spawn;
var productionEnv = Object.create(process.env);
productionEnv.NODE_ENV = 'production';
var start = spawn('node', ['app.js'], {env: productionEnv});

this did not

var spawn = require('child_process').spawn;
var start = spawn('node', ['app.js'], {env: {NODE_ENV: 'production'}});
laconbass
  • 17,080
  • 8
  • 46
  • 54
John Papa
  • 21,880
  • 4
  • 61
  • 60
0

add shell option worked for me

gulp.task('xxx', function (callback) {
    process.chdir('xxx/');
    var spawn = require('child_process').spawn;
    var productionEnv = Object.create(process.env);
    // var jekyll = spawn('gulp', ['stylecheck'], {stdio: 'inherit', env: productionEnv});
    var jekyll = spawn('gulp', ['stylecheck'], {stdio: 'inherit', env: productionEnv, shell: true});

    jekyll.on('exit', function (code) {
        console.log(arguments);
    });
});
Augustine
  • 49
  • 3
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – thewaywewere Jun 09 '17 at 02:34
  • 1
    see nodejs api: https://nodejs.org/api/child_process.html#child_process_spawning_bat_and_cmd_files_on_windows – Augustine Jun 12 '17 at 08:58
  • You just copied and pasted the code which doesn't explained well. Looks like better one but please explain it – M. Inam Jun 19 '21 at 13:04
  • this nodes official docs may help: https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options – Augustine Jul 09 '21 at 12:14