Using spawn function with NODE_ENV=production

前端 未结 4 658
天命终不由人
天命终不由人 2021-02-02 07:42

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

4条回答
  •  后悔当初
    2021-02-02 08:07

    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' }
    

提交回复
热议问题