Skip to content

feat(nodeenv): add --isolate-npm to keep npm config inside the environment - #396

Merged
ekalinin merged 7 commits into
masterfrom
feat/isolate-npm
Sep 15, 2026
Merged

ekalinin merged 7 commits into
masterfrom
feat/isolate-npm

Conversation

@ekalinin

@ekalinin ekalinin commented Sep 13, 2026 •

Copy link
Copy Markdown
Owner

Closes #154

Summary

Adds --isolate-npm (and the isolate_npm key in ~/.nodeenvrc): when set, the generated bin/activate, bin/activate.fish and the node shim export npm_config_cache, npm_config_userconfig and npm_config_init_module pointing inside the environment ($NODE_VIRTUAL_ENV/.npm, .npmrc, .npm-init.js), and deactivate_node restores the previous values.

Today only prefix is redirected, so npm's cache, userconfig and init-module still live under $HOME. When $HOME is missing or read-only (the reporter's case, HOME=/nonexistent), npm install inside an activated environment fails with ENOENT ... mkdir '/nonexistent'.

  • Opt-in, the default behaviour is unchanged. Redirecting userconfig by default would silently drop private registries and auth tokens from ~/.npmrc; redirecting cache by default would stop the cache being shared across environments.
  • All three settings move together with the flag.
  • Environment variables in the activation scripts rather than $prefix/etc/npmrc: npm reads the user config before the global one, so a userconfig line in globalconfig is displayed by npm config ls -l but never used (checked on npm 11). One mechanism for all three keeps it in line with how prefix is handled.
  • Only lowercase npm_config_* variables are set; npm matches env keys case-insensitively, so the uppercase duplicate that prefix carries is not repeated.
  • Same scope as prefix: activate, activate.fish, shim (and bin/node for --node=system). The Windows scripts do not configure npm today and are left as they are; on win32 the flag logs a warning (not for --list) and is otherwise ignored.
  • The fish restore block is wrapped in if set -q NODE_VIRTUAL_ENV, so the deactivate_node nondestructive pass at the top of activate.fish does not erase pre-existing npm_config_* values before they are saved.
  • Without the flag the generated scripts are identical to the current output except for a blank line where each placeholder sits.

Changes

  • nodeenv.py: Config.isolate_npm, --isolate-npm argument, NPM_ISOLATE / NPM_UNISOLATE snippet dicts next to DISABLE_PROMPT / ENABLE_PROMPT, __NPM_ISOLATE__ / __NPM_UNISOLATE__ placeholders in ACTIVATE_SH, ACTIVATE_FISH and SHIM, their replacement in install_activate(), win32 warning in main().
  • tests/test_install_activate.py: default-off check; sh test that sources the generated activate in sh and checks the values both while active and after deactivate_node; fish content test (guard, save, restore, cleanup lines); shim content test; --node=system test that runs the generated bin/node through a fake node and reads the exported values back; placeholder test now runs with and without the flag.
  • tests/nodeenv_test.py: config default, ~/.nodeenvrc loading, argparse, win32 warning, no warning on posix, no warning with --list.
  • README.rst, CHANGES: option description, config block line, changelog entry.

Checks

  • pytest tests -m "not integration": 228 passed
  • flake8 --extend-ignore=E127 nodeenv.py tests setup.py: clean
  • Manual with node 26.8.2 / npm 11.19.1: nodeenv --isolate-npm --prebuilt env, after . env/bin/activate the cache, userconfig and init-module entries of npm config ls -l point under env/; HOME=/nonexistent npm install is-odd succeeds (on master it fails with the ENOENT above); after deactivate_node the three variables are back to their previous values.
  • Generated activate, activate.fish and shim without the flag compared against master: identical ignoring blank lines.

Not covered here

  • Cygwin: the snippets use the POSIX $NODE_VIRTUAL_ENV path while prefix goes through cygpath -w; the existing SHIM has the same inconsistency for NPM_CONFIG_PREFIX.
  • The fish snippet was checked textually only (fish is not installed locally).
  • The pre-existing deactivate_node nondestructive behaviour in activate.fish for NODE_PATH, NPM_CONFIG_PREFIX and npm_config_prefix is left as is.

@ekalinin
ekalinin merged commit 035edde into master Sep 15, 2026
38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Three npm configs point outside virtualenv

1 participant