Skip to content

fix(nodeenv): read npm's parseable listing in freeze - #414

Merged
ekalinin merged 1 commit into
masterfrom
fix/freeze-requirements
Sep 24, 2026
Merged

ekalinin merged 1 commit into
masterfrom
fix/freeze-requirements

Conversation

@ekalinin

Copy link
Copy Markdown
Owner

Closes #287.

Problem

freeze scraped the drawn npm ls tree:

NPM_LIST=$(eval ${npmls} | grep -E '^.{4}\w{1}'| \
                           grep -o -E "$re"| grep -v npm)

Every filter in that pipeline loses packages. On node 24.21.0 / npm 11.19.0:

npm ls -g freeze
@sindresorhus/[email protected] -
[email protected] [email protected]
[email protected] [email protected]
[email protected] -
[email protected] -
  • Scoped packages vanish. ├── @scope/[email protected] has an @ in the column grep -E '^.{4}\w{1}' wants a word character in, so the line never reaches the second grep. The version regexp has no / either, so even a surviving line would yield [email protected] without the scope.
  • Any name containing npm vanishes. grep -v npm was meant to skip npm itself and also eats npmlog, npm-check-updates, npm-run-all.
  • corepack is kept. node.js installs it on its own. Feeding the frozen file back runs npm install -g [email protected], which exits 0 and silently downgrades the bundled copy: corepack --version then reports 0.9.0 on a node 24 environment.

The result is a requirements file that describes a different environment than the one it was taken from, with no warning either way.

The fish freeze prints nothing at all under npm >= 1: set -l NPM_LIST is declared inside the else block and fish scopes it there, so the echo after end sees an empty variable. The same function greps "$re" where the variable is $RE, its npm 0.x branch runs npm list installed active >/dev/null ^/dev/null - the list goes to /dev/null and, since fish 3.0 turned on stderr-nocaret, ^/dev/null reaches npm as a literal argument - and echo $NPM_LIST joins a fish list with spaces where a requirements file needs newlines.

Fix

Read npm ls --depth=0 --parseable --long instead. It prints path:name@version[:flags], one package per line:

/env/lib:lib@:/env/lib
/env/lib/node_modules/@sindresorhus/is:@sindresorhus/[email protected]
/env/lib/node_modules/extra:[email protected]:EXTRANEOUS

The scope survives, and the tree root is the only line without /node_modules/ in its path, in global and in local mode alike, so one sed both drops it and extracts the name. npm and corepack are excluded by exact name, ^(npm|corepack)@, which anchors what grep -v npm only approximated. :EXTRANEOUS and the other trailing flags are stripped; the package itself is installed and stays.

The npm 0.x branch of the posix script is untouched - npm list installed active predates --parseable --long. In the fish script that branch is corrected to 2>/dev/null, since as written the function cannot work at all.

npm ls --json would be more robust still, but parsing it means spawning node from inside a shell function.

Verification

tests/test_activate_shells.py already sources the generated scripts in sh, dash, bash, zsh and fish. It gets a stub npm next to the existing stub node - it answers -v and returns a fixed --parseable --long listing - and three tests for freeze, freeze -l and freeze FILE, parametrised over every shell. No download and no network. They fail in all five shells before the change; zsh stays xfail on the known source guard (#398).

End to end on a real environment, the same reproduction as above:

freeze -> nodeenv -p -r node-requirements.txt -> freeze now returns the identical list, and corepack stays at the version node.js shipped. The fish script produces the same output.

pytest tests green (352 passed, 20 xfailed), flake8 --extend-ignore=E127 nodeenv.py tests setup.py clean.

Documentation

README gains a note that packages bundled with node.js are left out, and the answer to the question the issue actually asks - freeze saves the packages, .node-version pins the runtime. [email protected] is dropped from the sample requirements file, since freeze will not write such a line any more.

freeze scraped the drawn `npm ls` tree, and every filter in that pipeline
lost packages. `grep -E '^.{4}\w{1}'` dropped each scoped package, because
`@scope/name` puts an `@` in the column the filter wants a word character
in, and the version regexp had no `/` to keep the scope with either.
`grep -v npm` was meant to skip npm itself and also ate npmlog,
npm-check-updates and every other name containing "npm". What it did keep
was corepack, which node.js installs on its own: feeding such a file back
runs `npm install -g [email protected]`, which exits 0 and silently downgrades
the bundled copy.

Read `npm ls --depth=0 --parseable --long` instead. It prints
`path:name@version[:flags]`, one package per line, so the scope survives and
the tree root is the only line without `/node_modules/` in its path, in
global and in local mode alike. npm and corepack are excluded by exact name
now.

The fish version printed nothing at all under npm >= 1: its `set -l
NPM_LIST` sits inside the `else` block, which fish scopes it to, and it
greped `$re` where the variable is `$RE`. Its npm 0.x branch sent the list
to /dev/null and passed `^/dev/null` to npm as a literal argument, fish 3.0
having turned on stderr-nocaret, and `echo` joined the packages with spaces
where a requirements file needs newlines.

tests/test_activate_shells.py gets a stub npm and checks freeze, `freeze -l`
and `freeze FILE` in every shell it runs.

Closes #287
@ekalinin
ekalinin merged commit d163302 into master Sep 24, 2026
44 checks passed
@ekalinin ekalinin added the bug label Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Freeze requirements properly

1 participant