"npm install --save-optional" fails to properly modify package/package-lock.json if it fails

cli
help-wanted
priority:low
triaged

(Matthew Herbst) #1

What I Wanted to Do

npm install --save-optional win-node-env

This package helps with setting NODE_ENV and such on Windows machines for running scripts. I’m on a MacOS machine. The above command failed because win-node-env only installs on Windows machines (as it should). However, I was not actually trying to install it on my machine. Rather, I was running the command so that it would generate a proper entry in our package-lock.json file.

I expected the package to not install, but for it to be added properly in both package.json and package-lock.json

What Happened Instead

The command failed because the optional dependency I was trying to install cannot be installed on non-Windows machines, and I am on a MacOS machine. No entries were made in package.json or in package-lock.json.

Reproduction Steps

On a non-Windows machine, run:

npm install --save-optional win-node-env

Details

The reason I think this is a bug, is because if I add win-node-env manually to optionalDependencies in package.json, and then run npm install, I get no errors, AND, package-lock.json is properly modified to now have an entry for win-node-env with optional marked as true.

Platform Info

$ npm --versions
{ govlist: '1.0.0',
  npm: '6.4.1',
  ares: '1.10.1-DEV',
  cldr: '32.0',
  http_parser: '2.8.0',
  icu: '60.1',
  modules: '57',
  napi: '3',
  nghttp2: '1.32.0',
  node: '8.11.4',
  openssl: '1.0.2p',
  tz: '2017c',
  unicode: '10.0',
  uv: '1.19.1',
  v8: '6.2.414.54',
  zlib: '1.2.11' }

$ node -p process.platform
darwin

It’s possible that this is expected behavior, but it strikes me as incorrect. I should be able to use npm install --save-optional to add optional dependencies to our package.json and package-lock.json files regardless of if the optional dependencies I’m trying to add install or not.