我正在使用持续集成,并发现了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 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 install is great for development and in the CI when you want to cache the node_modules directory. When to use this? You can do this if you are making a package for other people to use (you do NOT include node_modules in such a release). Regarding the caching, be careful, if you plan to support different versions of Node.js remember that node_modules might have to be reinstalled due to differences between the Node.js runtime requirements. If you wish to stick to one version, stick to the latest LTS.

当你要测试和发布一个产品应用程序(最终产品,不被其他包使用)时,应该使用NPM ci,因为让安装尽可能确定是很重要的,这个安装将花费更长的时间,但最终会使你的应用程序更可靠(你确实在这样的版本中包含了node_modules)。坚持使用Node.js的LTS版本。

NPM I和NPM ci都使用NPM缓存(如果存在的话),这个缓存通常位于~/. NPM。

此外,npm ci尊重包锁。json文件。与npm install不同,npm install会重写文件并总是安装新版本。

好处:你可以根据你想要的复杂程度来混合它们。在git的特性分支上,你可以缓存node_modules来提高团队的工作效率,在合并请求和主分支上依赖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 ci将删除任何现有的node_modules文件夹,并依赖于包锁。Json文件来安装每个包的特定版本。它比npm安装快得多,因为它跳过了一些特性。它的清洁状态安装非常适合ci/cd管道和docker构建!您还可以使用它一次性安装所有东西,而不是特定的包。