Skip to content

fix(nodeenv): point npm_config_prefix at the env in activate.bat and Activate.ps1 - #420

Merged
ekalinin merged 1 commit into
masterfrom
fix/win-activate-npm-config-prefix
Sep 27, 2026
Merged

ekalinin merged 1 commit into
masterfrom
fix/win-activate-npm-config-prefix

Conversation

@ekalinin

Copy link
Copy Markdown
Owner

Closes #309

What happens

npx (and npm exec) exports npm_config_prefix to the script it runs. npm's npm.cmd asks npm-prefix.js for the global prefix, and when an npm is installed under that prefix it runs that one instead of the npm next to it. The posix activate and activate.fish save and override the variable, but activate.bat and Activate.ps1 only touched PATH and NODE_PATH. So under cmd.exe and PowerShell an activated environment kept running the outer npm, exactly what the issue reports. Still reproducible with npm 11: npm.cmd has kept the prefix lookup.

What changes

  • activate.bat and Activate.ps1 save an inherited npm_config_prefix, set it to Scripts (npm's prefix on Windows is the directory holding node.exe, the same value the git-bash activate already uses) and deactivate restores or drops it.
  • A second activation without a deactivate in between is treated as a re-activation, so the value set by the first one is not saved as if it were inherited.
  • Windows environment names are case-insensitive, so the lowercase spelling covers NPM_CONFIG_PREFIX too.

Tests

  • tests/test_install_activate.py: the generated activate.bat, deactivate.bat and Activate.ps1 carry the save, set and restore lines (runs everywhere through fake_win).
  • tests/test_activate_win.py: cmd.exe and PowerShell are run with a leaked npm_config_prefix; after activation it points at Scripts, after deactivate the leaked value is back, and activate-twice-then-deactivate leaves it unset. These need a Windows host and run on the windows-latest matrix.

The tests were pushed on their own first: the two "points npm at Scripts" tests failed on all five Windows jobs against the unchanged templates, and the whole matrix is green with the fix.

…Activate.ps1

npx exports npm_config_prefix, and npm.cmd asks npm-prefix.js for the
global prefix: when an npm is installed under that prefix, npm.cmd runs
it instead of the one next to it. The posix and fish activate scripts
save and override the variable, the cmd and PowerShell ones only touched
PATH and NODE_PATH, so an activated environment kept running the outer
npm under cmd.exe and PowerShell.

activate.bat and Activate.ps1 now save an inherited npm_config_prefix,
set it to the Scripts directory (npm's prefix on Windows is the one
holding node.exe) and deactivate restores or drops it. A second
activation without a deactivate in between is treated as a re-activation,
so the value set by the first one is not mistaken for an inherited one.
Windows environment names are case-insensitive, so the lowercase spelling
covers NPM_CONFIG_PREFIX too.

Closes #309
@ekalinin
ekalinin merged commit 00d7b35 into master Sep 27, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong npm version when spawning nodeenv using npx

1 participant