Search code examples
node.jsenvironment-variablesparent-childspawn

How can I pass all current environmental variables in a node spawn?


I am spawning child tasks in Node the following way:

function createChildProc(prog, params0, cmdAndEnv) {
    var spawn = require('child_process').spawn;
    return spawn(prog, params0, cmdAndEnv);
}

Where cmdAndEnv manually contain the environment variables created during the initial fire up of Node.

Instead of manually issuing all the environmental variables into the spawned child, is there a way to have node automatically inject the current environment variables into the child's environment?


Solution

  • The third argument is used to specify additional options. One of these options is env which contains the environment key-value pairs in an object.

    return spawn(prog, params0, { env: cmdAndEnv });
    

    See the documentation for more details.