zerolab-fe / one-question-per-day

每天一个小问题
21 stars 7 forks source link

npm install 和 npm ci 的区别 #113

Open GleanCoder1116 opened 3 years ago

GleanCoder1116 commented 3 years ago

在使用gitlab中的ci自动构建的时候;对于前端来说;一般会执行 npm install 这样的一个过程;这个是过程是比较耗时的;npm 介绍中安装依赖有另外一个命令:

来自 npm 文档:

简而言之,使用 npm install 和 npm ci 的主要区别是:

  • The project must have an existing package-lock.json or npm-shrinkwrap.json.
  • If dependencies in the package lock do not match those in package.json, npm ci will exit with an error, instead of updating the package lock.
  • npm ci can only install entire projects at a time: individual dependencies cannot be added with this command.
  • If a node_modules is already present, it will be automatically removed before npm ci begins its install.
  • It will never write to package.json or any of the package-locks: installs are essentially frozen.

个人理解

实际上,npm install 读取 package.json 来创建依赖项列表,并使用 package-lock.json。通知这些依赖项的哪个版本要安装。如果依赖项不在package-lock.json 它将在 npm install添加到package-lock.json

npm ci (以持续集成命名)直接从package-lock.json中安装依赖项。使用 package.json 只是为了验证没有不匹配的版本。如果任何依赖项丢失或具有不兼容的版本,它将抛出错误。

npm ci 将删除所有现有的 node _ modules 文件夹,包括缓存的,并依赖于 package-lock。来安装每个包的特定版本。它比 npm install 快得多,因为它略过了一些特性。它的清洁状态安装非常适合 ci/cd pipelines 和 docker builder!您还可以使用它一次性安装所有软件,而不是特定的软件包。

更多内容

npm docs