简体   繁体   English

我如何通过'npm ci'跳过安装可选依赖项?

[英]How I can skip installing optional dependencies by 'npm ci'?

How I can skip installing optional dependencies from package-lock.json by npm ci ?我如何跳过npm cipackage-lock.json安装可选依赖项?

You can use npm ci --no-optional .您可以使用 npm ci --no-optional 。 If npm still installs the optional package.如果 npm 仍然安装可选包。 Then try after removing package.lock.json and run the command again.然后尝试删除 package.lock.json 并再次运行命令。

A bit late, but you can use npm ci --no-optional .有点晚了,但您可以使用npm ci --no-optional I tested it.我测试了它。 Make shure your npm version is a least 6.13.4 (maybe earlier version will work but I can't confirm).确保您的 npm 版本至少为 6.13.4(也许早期版本可以使用,但我无法确认)。

In order to make npm ci --no-optional skip/ignore an optional pacakge, it's important to understand how npm intracts with package.json and pacakge-lock.json.为了使npm ci --no-optional跳过/忽略一个可选的包,重要的是要了解npm如何与 package.json 和 pacakge-lock.json 相结合。

  1. npm install --no-optional (is only effective if pacakge-lock.json doesn't exists otherwise it would ignore --no-optional)* npm install --no-optional (仅当 pacakge-lock.json 不存在时有效,否则它会忽略 --no-optional)*
  2. npm ci --no-optional is only effective if pakcage-lock.json was already created with npm install --no-optional **. npm ci --no-optional仅当 pakcage-lock.json 已使用npm install --no-optional ** 创建时才有效。

* This means if you want to make an already installed package an optional, you can would have to * 这意味着如果你想让一个已经安装的 package 成为可选的,你必须

  1. Add it "optionalDependencies": either manulally or through npm install pacakge-name --save-optional添加它"optionalDependencies":手动或通过npm install pacakge-name --save-optional
  2. Delete the pacakge-lock.json.删除 pacakge-lock.json。
  3. then run rm -rf node_modules/然后运行rm -rf node_modules/
  4. Lastly run npm install --no-optional最后运行npm install --no-optional
  5. Add this point npm ci --no-optional isn't suppose to install it.添加这一点npm ci --no-optional不应该安装它。

** TIP : you could debug if a certian package is assigned as optional by running npm ls package-name **提示:您可以通过运行npm ls package-name来调试 certian package 是否被指定为可选

Note : This one the reason why its recommended to keep trak pacakge-lock.json with git repo of the project.注意:这就是为什么它建议将 trak pacakge-lock.json 与项目的 git 回购保持一致的原因。

It's not a proper solution, rather an ugly one, but it helped me out.这不是一个合适的解决方案,而是一个丑陋的解决方案,但它帮助了我。 It looks like npm ci --no-optional doesn't work and probably never worked.看起来npm ci --no-optional不起作用并且可能从未起作用。 But at the same time flag --production works.但同时标志--production有效。 And if we afford mutating package.json (eg in a docker container) then...如果我们负担得起改变package.json (例如在 docker 容器中),那么......

So I wrote a simple script that:所以我写了一个简单的脚本:

  • reads package.json content读取package.json内容
  • Object.assign(cfg.dependencies, cfg.devDependencies)
  • delete cfg.devDependencies
  • overwrites the initial package.json覆盖初始package.json

So finally we have:所以最后我们有:

  • dependencies contains both normal & dev dependencies dependencies包含普通和开发依赖项
  • devDependencies section is empty devDependencies部分为空
  • optionalDependencies are intact optionalDependencies完好无损

And when we run npm ci --production we got what we want - no optional dependencies (in my case cypress ).当我们运行npm ci --production我们得到了我们想要的东西——没有可选的依赖项(在我的例子中是cypress )。 Due to the fact that all these steps are performed inside of a docker container we can mutate package.json .由于所有这些步骤都是在 docker 容器内执行的,我们可以改变package.json

But I'm not sure that it'll help you too.但我不确定它是否也会帮助你。

I was facing this issue with CI workflow script and even "--no-optional" was not working我在 CI 工作流脚本中遇到了这个问题,甚至“--no-optional”也不起作用

npm ci --no-optional

The above command only worked when I added the optional package as上面的命令仅在我添加可选包时有效

"optionalDependencies": {
    "fsevents": "^2.3.2"
}

in the package.json file在 package.json 文件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM