Skip to content

[BUG] Impossible to reliably passthrough NODE_OPTIONS in .npmrc #8335

Open
@aczekajski

Description

@aczekajski

Is there an existing issue for this?

  • I have searched the existing issues

This issue exists in the latest npm version

  • I am using the latest npm

Current Behavior

If I add following line to .npmrc:

node-options = "${NODE_OPTIONS} --use-system-ca"

it correctly works if the NODE_OPTIONS was set before to some node flags in my env. For example, if I do

export NODE_OPTIONS=--inspect
npm run foo

then the Node being run by the "foo" script, receives --inspect --use-system-ca as node options and it's fine.

But, if the NODE_OPTIONS env var is not set to anything when i do npm run foo, then the Node receives ${NODE_OPTIONS} --use-system-ca as options and Node rejects all the options (--use-system-ca is ignored as well).

As a result of npm leaving var substitutions untouched if the var is not set, there is no way to reliably extend the node options via .npmrc instead of replacing them.

Expected Behavior

Either:

  • ${FOO} var substitution pattern should eval to empty string if FOO is not defined
  • or possibility to provide defaults in a bash-like way should be possible. So ${FOO:-''} can be used to just have an empty string if the var is not defined at all

Steps To Reproduce

  1. Add this script to package.json: "foo": "node -e 'console.log(process.env.NODE_OPTIONS);'"
  2. Add this line in .npmrc: node-options = "${NODE_OPTIONS} --max-old-space-size=1"
  3. run NODE_OPTIONS='' npm run foo - as expected the Node will fail to run because it runs out of the assigned 1mb of memory, meaning the flags were passed just fine
  4. unset NODE_OPTIONS to make sure it is not set to any value, not even an empty string
  5. npm run foo
  6. You can see ${NODE_OPTIONS} --max-old-space-size=1 printed and Node finishes just fine which means that the NODE_OPTIONS were ignored because of the incorrect string resulting from unsubstituted variable

Environment

  • npm: 11.4.1
  • Node.js: 22.15.1
  • OS Name: Windows
  • System Model Name:
  • npm config:
; "user" config from C:\Users\(...)\.npmrc

email = (protected)
script-shell = "C:\\Program Files\\Git\\usr\\bin\\bash.exe"

; "project" config from C:\(...)\.npmrc

fetch-retries = 4
legacy-peer-deps = true
node-options = "${NODE_OPTIONS} --max-old-space-size=1"
save-exact = true
save-prefix = ""

; node bin location = C:\Users\(...)\AppData\Local\fnm_multishells\32040_1748498818910\node.exe
; node version = v22.15.1
; npm local prefix = C:\(...)
; npm version = 10.9.2
; cwd = C:\(...)
; HOME = C:\Users\(...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Enhancementnew feature or improvementsemver:majorbackwards-incompatible breaking changes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions