Open Maxim-Mazurok opened 2 years ago
Workaround is to remove all npm_*
keys from env and pass this new env to exec()
:
+ const newEnv = { ...process.env };
+ for (const key in newEnv) {
+ if (key.startsWith("npm_")) {
+ delete newEnv[key];
+ console.log(`${key} deleted from env`);
+ }
+ }
console.log(
`\n\nNew npm version: ${execSync(
["nodeenv\\Scripts\\activate", "npm -v"].join(" && "),
+ { env: newEnv }
)}`
);
Updated script produces correct results even when running with npx:
npx -r cross-env node index.js
results in
npm_config_prefix in ENV: "C:\Users\maxim\AppData\Local\nvs\default"
Current npm version: 7.15.1
Expected npm version: 7.16.0
Installing nodeenv, please wait...
+ npm_command deleted from env
+ npm_config_cache deleted from env
+ npm_config_globalconfig deleted from env
+ npm_config_init_module deleted from env
+ npm_config_metrics_registry deleted from env
+ npm_config_node_gyp deleted from env
+ npm_config_noproxy deleted from env
+ npm_config_prefix deleted from env
+ npm_config_r deleted from env
+ npm_config_userconfig deleted from env
+ npm_config_user_agent deleted from env
+ npm_execpath deleted from env
+ npm_lifecycle_event deleted from env
+ npm_lifecycle_script deleted from env
+ npm_node_execpath deleted from env
+ npm_package_json deleted from env
+ npm_package_name deleted from env
+ npm_package_version deleted from env
- New npm version: 7.15.1
+ New npm version: 7.16.0
In short,
npx
setsnpm_config_prefix
env var, which affects hownpm prefix
works, and because of this when we spawnnodeenv
from scripts started withnpx
, we get the correct node version, but incorrectnpm
version.Please consider the following code:
And if we run it like this:
we will get the following result:
As you can see, despite nodenv installing everything correctly, we are still using "current" npm that we ran
npx
with, not the expected or desired one.If we run the same script without
npx
, like so:We will get the expected results:
Not sure what can or should be done here from the nodeenv perspective. Probably nothing. I just spent quite some time debugging this and thought I'd share. I will probably solve this by clearing all these
npm_config_*
from env before spawning script that usesnodeenv
, will report results.