npm Community Forum (Archive)

The npm community forum has been discontinued.

To discuss usage of npm, visit the GitHub Support Community.

Optional incompatible packages are still downloaded

What I Wanted to Do

I wanted optional large incompatible optional packages to not be downloaded.

What Happened Instead

They were downloaded even though they were known to be incompatible, and didn’t end up in the final node_modules.

Reproduction Steps

There’s a repro in

git clone
cd bazel-optional-dep-download
npm run test-npm


The repro installs @bazel/bazel, which contains 3 optional dependencies with OS restrictions :

Each of these platform specific packages contains a large (~160mb) binary.

The final installed node_modules contains only one of these packages, but through the verbose logs we can see that all three were downloaded:

npm http fetch GET 200 1383ms
npm http fetch GET 200 1427ms
npm http fetch GET 200 1439ms
// ...
npm http fetch GET 200 6038ms
npm http fetch GET 200 21486ms
npm http fetch GET 200 25509ms

The first set of requests already contains the package.json data for the packages so it is already possible to know that the package is incompatible with the OS, and thus should be ignored since it is a optional.
There should be no need to download it.

Platform Info

$ npm --versions
{ 'bazel-optional-dep-download': '1.0.0',
  npm: '6.4.1',
  ares: '1.14.0',
  cldr: '33.1',
  http_parser: '2.8.0',
  icu: '62.1',
  modules: '64',
  napi: '3',
  nghttp2: '1.33.0',
  node: '10.10.0',
  openssl: '1.1.0i',
  tz: '2018e',
  unicode: '11.0',
  uv: '1.23.0',
  v8: '',
  zlib: '1.2.11' }
$ node -p process.platform

This isn’t a documented behavior when optionalDependencies is specified in package.json
Sorry to bother you @zkat, is this a bug or a feature?