when setting a value to false with npm config the corresponding npm environment variable is not set


(quil0o) #1

What I Wanted to Do

i want to set a config value ‘foo’ - defined in a package.json - to ‘false’ and access the value of ‘foo’ via its environment variable ‘npm_package_config_foo’.

What Happened Instead

the environment variable ‘npm_package_config_foo’ is unset/empty/has not been set to ‘false’.

Reproduction Steps

simlified package.json:

{
  "name": "abc",
  "config": {
    "foo": "true"
  },
  "scripts": {
    "test":"echo $npm_package_config_foo"
  }
}

in terminal:

#  returns npm_package_config_foo to be set to the default value 'true'
$ npm run test
> true

# set foo to false or null or undefined
# always results in npm_package_config_foo not being set at all
$ npm config set abc:foo false
$ npm run test
>

$ npm config set abc:foo null
$ npm run test
>

$ npm config set abc:foo undefined
$ npm run test
>

Details

actually, i don’t know if this behaviour is considered a bug but i found it inconsistent because the default value of ‘foo’ (‘true’) is being available via the envvar while the ‘false’ value is not stored in the envvar. i consider ‘false’, ‘null’, ‘undefined’ as string literals put into a config value but not being interpreted as (js?) boolean or undefined types.

debug log for 'npm config set abc:foo false’

npm info it worked if it ends with ok
npm verb cli [ '/usr/bin/node',
npm verb cli   '/usr/bin/npm',
npm verb cli   '--loglevel',
npm verb cli   'verbose',
npm verb cli   'config',
npm verb cli   'set',
npm verb cli   'abc:foo',
npm verb cli   'false' ]
npm info using npm@6.7.0
npm info using node@v8.15.0
npm info config set "abc:foo" "false"
npm verb exit [ 0, true ]
npm timing npm Completed in 601ms
npm info ok 

debug log for ‘npm run test’ (obfuscated some of my machines pathes)

npm info it worked if it ends with ok
npm verb cli [ '/usr/bin/node',
npm verb cli   '/usr/bin/npm',
npm verb cli   '--loglevel',
npm verb cli   'verbose',
npm verb cli   'run',
npm verb cli   'test' ]
npm info using npm@6.7.0
npm info using node@v8.15.0
npm verb run-script [ 'pretest', 'test', 'posttest' ]
npm info lifecycle abc@~pretest: abc@
npm info lifecycle abc@~test: abc@

> abc@ test XXXXXXXXXXXXXXX
> echo $npm_package_config_foo

npm verb lifecycle abc@~test: unsafe-perm in lifecycle true
npm verb lifecycle abc@~test: PATH: /usr/lib/node_modules/npm/node_modules/npm-lifecycle/node-gyp-bin:XXXXXXXXXXXXXXX
npm verb lifecycle abc@~test: CWD: XXXXXXXXXXXXXXX
npm info lifecycle abc@~posttest: abc@
npm verb exit [ 0, true ]
npm timing npm Completed in 448ms
npm info ok

Platform Info

$ npm --versions
{ npm: '6.7.0',
  ares: '1.10.1-DEV',
  cldr: '32.0',
  http_parser: '2.8.0',
  icu: '60.1',
  modules: '57',
  napi: '3',
  nghttp2: '1.33.0',
  node: '8.15.0',
  openssl: '1.0.2q',
  tz: '2017c',
  unicode: '10.0',
  uv: '1.23.2',
  v8: '6.2.414.75',
  zlib: '1.2.11' }

$ node -p process.platform
linux

(John Gee) #2

I reproduced this on Mac. Like you, not sure if will be considered a bug. For my own interest, I checked a couple of details of behaviour with blackbox tests:

  1. It is the npm script runtime, not npm config:
$ npm config set abc:foo null
$ npm config get abc:foo 
null
  1. The environment variable is empty rather than unset. Tested with:
"unset_test":"echo \\[${npm_package_config_foo-unset}\\]"
$ npm run -s unset_test
[true]
$ npm config set abc:foo undefined
$ npm run -s unset_test
[]

(quil0o) #3

txs for digging a bit deeper! i can see the same behaviour as you described on my linux as well.


(Lars Willighagen) #4

Relevant commit, makes it seem like it is (or was) intentional.