【问题标题】:`npm install` always installs everything from package.json`npm install` 总是从 package.json 安装所有东西
【发布时间】:2018-10-15 23:26:51
【问题描述】:

我正在尝试设置一些依赖于几个 Node 模块的 CI 测试,但不需要在 package.json 中安装所有内容。我想我可以这样做:

npm install --no-save eslint stylelint stylelint-config-standard stylelint-order stylelint-scss

但是,这样做仍然会在 package.json 中安装我的 devDependencies 中的所有内容。如何告诉 NPM 忽略我的 package.json,只安装我明确告诉它的内容?


编辑:为了更好地展示我遇到的问题,我从我的一个项目中删除了node_modules,并尝试运行npm install --no-save mkdirp。 mkdirp 是一个非常简单的模块,只有一个依赖项,但正如您从下面的输出中看到的那样,NPM 继续前进,仍然在我的package.json 中安装了所有内容。

jacob@RYZEN:~/Repositories/new-site$ npm install --no-save mkdirp

> puppeteer@1.8.0 install /mnt/c/Users/Jacob/Repositories/new-site/node_modules/puppeteer
> node install.js

Downloading Chromium r588429 - 103.7 Mb [====================] 100% 0.0s
Chromium downloaded to /mnt/c/Users/Jacob/Repositories/new-site/node_modules/puppeteer/.local-chromium/linux-588429

> node-sass@4.9.3 install /mnt/c/Users/Jacob/Repositories/new-site/node_modules/node-sass
> node scripts/install.js

Cached binary found at /home/jacob/.npm/node-sass/4.9.3/linux-x64-57_binding.node

> gifsicle@3.0.4 postinstall /mnt/c/Users/Jacob/Repositories/new-site/node_modules/gifsicle
> node lib/install.js

  ✔ gifsicle pre-build test passed successfully

> jpegtran-bin@3.2.0 postinstall /mnt/c/Users/Jacob/Repositories/new-site/node_modules/jpegtran-bin
> node lib/install.js

  ✔ jpegtran pre-build test passed successfully

> optipng-bin@3.1.4 postinstall /mnt/c/Users/Jacob/Repositories/new-site/node_modules/optipng-bin
> node lib/install.js

  ✔ optipng pre-build test passed successfully

> pngquant-bin@3.1.1 postinstall /mnt/c/Users/Jacob/Repositories/new-site/node_modules/pngquant-bin
> node lib/install.js

  ✔ pngquant pre-build test passed successfully

> node-sass@4.9.3 postinstall /mnt/c/Users/Jacob/Repositories/new-site/node_modules/node-sass
> node scripts/build.js

Binary found at /mnt/c/Users/Jacob/Repositories/new-site/node_modules/node-sass/vendor/linux-x64-57/binding.node
Testing binary
Binary is fine

> swiper@4.3.5 postinstall /mnt/c/Users/Jacob/Repositories/new-site/node_modules/swiper
> node -e "console.log('\u001b[35m\u001b[1mLove Swiper? Support Vladimir\'s work by donating or pledging on patreon:\u001b[22m\u001b[39m\n > \u001b[32mhttps://patreon.com/vladimirkharlampidi\u001b[0m\n')"

Love Swiper? Support Vladimir's work by donating or pledging on patreon:
 > https://patreon.com/vladimirkharlampidi

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.4 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.4: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

+ mkdirp@0.5.1
added 1969 packages from 803 contributors and audited 24004 packages in 201.431s
found 21 vulnerabilities (4 low, 8 moderate, 9 high)
  run `npm audit fix` to fix them, or `npm audit` for details

【问题讨论】:

标签: node.js npm continuous-integration


【解决方案1】:

我已经弄清楚为什么会这样了;显然,如果你有一个package-lock.json,NPM 总是在那里安装所有东西,不管你通过什么标志。解决方案是--no-package-lock 标志。

npm install --no-package-lock --no-save --quiet stylelint-config-standard stylelint-order stylelint-scss

【讨论】:

  • 这个used to work up to NPM 6,但似乎不再适用于 NPM 7.8.0。即使您删除 package-lock.json 并传递您想要的所有参数,npm install local-iso-dt --no-package-lock --production 仍会将所有内容安装在 package.json 中,而不仅仅是零依赖 local-iso-dt 模块。
【解决方案2】:

根据 npm 文档,您只能安装 dependencies,不能安装其他任何东西。 https://docs.npmjs.com/cli/install

使用 --production 标志(或当 NODE_ENV 环境变量 设置为生产),npm 将不会安装列出的模块 devDependencies。

注意: --production 标志在添加 对项目的依赖。

所以,只需运行npm install --production

【讨论】:

  • 这更接近于我想要做的,但我根本不需要安装devDependenciesdependencies,所以它不是很理想。我会看看它是否适合我的目的。
  • 已测试,这似乎安装依赖项;如果我尝试安装不在dependencies 中的其他模块(即npm i --production stylelint),它们实际上并没有安装。我需要能够独立于package.json 安装模块
  • 对不起,我误解了你的问题。仅运行 npm install --no-save someDependency 只会将其安装到 node_modules 中,而不会更新 package.json。
  • 嗯,这也是我的想法,但这样做似乎是在 package.json 中安装所有内容。将在几台机器上进行更多测试以进行验证。
  • 您确定不是您列出的那些依赖项依赖的所有其他依赖项吗?如果你安装了foo,而foo 需要bar baz qux,你也会在node_modules 中看到foo bar baz qux
猜你喜欢
  • 2020-11-05
  • 2014-05-30
  • 1970-01-01
  • 1970-01-01
  • 2017-02-10
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
相关资源
最近更新 更多