我正在使用持续集成,并发现了npm ci命令。

我不知道在我的工作流中使用这个命令有什么好处。

它更快吗?这会让考试更难吗,之后呢?


当前回答

来自npm ci的官方文档:

In short, the main differences between using npm install and npm ci are: 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安装读取包。Json来创建依赖项列表,并使用包锁。Json来通知安装这些依赖项的哪个版本。如果依赖项不在包锁中。Json,它将被NPM install添加。

npm ci(也称为Clean Install)适用于自动化环境——比如测试平台、持续集成和部署——或者任何你想要确保你正在对依赖项进行干净安装的情况。

它直接从包锁安装依赖项。Json,并使用package。Json来验证没有不匹配的版本。如果任何依赖项缺失或版本不兼容,它将抛出一个错误。

使用npm install来添加新的依赖项,并更新项目上的依赖项。通常情况下,你会在开发过程中使用它,更新依赖列表,但在这种情况下使用npm ci可能是一个好主意。

如果你需要一个确定的、可重复的构建,请使用npm ci。例如,在持续集成、自动化作业等期间,以及第一次安装依赖项时,而不是npm install。

npm安装

Installs a package and all its dependencies. Dependencies are driven by npm-shrinkwrap.json and package-lock.json (in that order). without arguments: installs dependencies of a local module. Can install global packages. Will install any missing dependencies in node_modules. It may write to package.json or package-lock.json. When used with an argument (npm i packagename) it may write to package.json to add or update the dependency. when used without arguments, (npm i) it may write to package-lock.json to lock down the version of some dependencies if they are not already in this file.

NPM CI

至少需要npm v5.7.1。 需要package-lock。Json或npm-shrinkwrap。Json。 如果这两个文件的依赖关系与package.json不匹配,则抛出错误。 删除node_modules并一次性安装所有依赖项。 它从来不写打包。Json或者package-lock.json。

算法

而npm ci从包锁生成整个依赖树。Json或npm-shrinkwrap。NPM install使用以下算法更新node_modules的内容(source):

从磁盘加载现有的node_modules树 克隆树 取包裹。Json和各种元数据,并将其添加到克隆 遍历克隆并添加任何缺失的依赖项 依赖项将尽可能靠近顶部添加 不破坏任何其他模块 将原始树与克隆树进行比较,并列出 将一种转换为另一种的操作 执行所有的行动,最深的第一 操作类型包括安装、更新、删除和移动

其他回答

npm install is the command used to install the dependencies listed in a project's package.json file, while npm ci is a command that installs dependencies from a package-lock.json or npm-shrinkwrap.json file. The npm ci command is typically used in continuous integration (CI) environments, where the package-lock.json or npm-shrinkwrap.json file is checked into version control and should not be modified. Because npm ci installs dependencies from a locked file, it is a faster and more reliable way to install dependencies than npm install, which could install different versions of dependencies based on the state of the package.json file.

它做了一个干净的安装,在你需要删除node_modules并重新运行npm i的情况下使用它。

我不知道为什么有些人认为它是“持续集成”的缩写。有一个npm install命令可以作为npm i运行,还有一个npm clean-install命令可以作为npm ci运行。

NPM ci将删除任何现有的node_modules文件夹,并依赖于包锁。Json文件来安装每个包的特定版本。它比npm安装快得多,因为它跳过了一些特性。它的清洁状态安装非常适合ci/cd管道和docker构建!您还可以使用它一次性安装所有东西,而不是特定的包。

NPM ci -安装package-lock.json中列出的内容 NPM安装-不改变包中的任何版本。Json,使用包。Json来创建/更新包锁。Json,然后安装package-lock.json中列出的内容 NPM update -更新包。Json包到最新版本,然后使用包。Json来创建/更新包锁。Json,然后安装package-lock.json中列出的内容

或者换种说法,npm ci改变0个包文件,npm install改变1个包文件,npm update改变2个包文件。

你链接的文档有摘要:

In short, the main differences between using npm install and npm ci are: 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.