[英]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 ci
从package-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 相结合。
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)*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 成为可选的,你必须
"optionalDependencies":
either manulally or through npm install pacakge-name --save-optional
"optionalDependencies":
手动或通过npm install pacakge-name --save-optional
rm -rf node_modules/
rm -rf node_modules/
npm install --no-optional
npm install --no-optional
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:所以我写了一个简单的脚本:
package.json
contentpackage.json
内容Object.assign(cfg.dependencies, cfg.devDependencies)
delete cfg.devDependencies
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.