-
Notifications
You must be signed in to change notification settings - Fork 29.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Spawn is ignoring all environment variables including PATH. #12986
Comments
I don't like this solution, but this is how you get it working. Why does the other child_process methods have the environment variable PATH included, but spawn does not??? require('child_process').spawn('node', ['--version'], {
cwd: process.cwd(),
env: {
PATH: process.env.PATH
},
stdio: 'inherit'
}); |
Did you try adding Also,
prints my |
Hmm something interesting is going on. But the behavior still doesn't seem right, however it could be intended. require('child_process').spawn('node', ['-pe', 'process.env.PATH'], {
cwd: process.cwd() + '/backend',
stdio: 'inherit'
}); Prints my path as expected. however this: require('child_process').spawn('node', ['-pe', 'process.env.PATH'], {
cwd: process.cwd() + '/backend',
env: {
NODE_CONFIG_DIR: process.cwd() + '/config'
},
stdio: 'inherit',
shell: true // doesn't matter if shell: true is here or not.
}); PATH is undefined... This doesn't seem right that it would clear all the env variables when you provide an env property. If this is the case, then without having to add many environment variables to env obj, how can I just change one then? |
Above is expected and documented behaviour. default value for If you wonder why, ask yourself how you would delete a env var during spawn that you didn't want passed from the parent process to its child. |
I think I found the answer, it wasn't obvious when I first read the docs.
Thanks for your help. Looks like all I have to do is clone Update: @sam-github I didn't see your comment as I was posting this. Thanks :) |
Another option is to use require('child_process').spawn('env', ['node', '--version'], {stdio: 'inherit'}) I found this actually worked much better than doing something like: require('child_process').spawn('node', ['--version'], {stdio: 'inherit', env: Object.assign(process.env, {MYENV: 1})}) Found it over here. |
Version: output of
node -v
Platform: output of
uname -a
(UNIX), or version and 32 or 64-bit (Windows)Subsystem: if known, please specify affected core module name
If possible, please provide code that demonstrates the problem, keeping it as
simple and free of external dependencies as you are able.
-->
So I use nvm since I have many node projects, which many have different versions. Everything has been going well until... I needed to use spawn. Node is refusing to work with spawn. It is as if node was never installed. So I tried exec, and exec can find the version of node I am using and it works as expected. Spawn on the other hand is throwing errors. It fails to include environment variables. I seriously shouldn't have to include environment variables for node. It should just use my existing ones.
console.log('version', require('child_process').execSync('node --version').toString());
works as expectedrequire('child_process').spawn('node', ['--version'], {stdio: 'inherit'});
fails horriblyThe text was updated successfully, but these errors were encountered: