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

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

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


当前回答

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个包文件。

其他回答

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

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

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

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.

你链接的文档有摘要:

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 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来获得确定的结果。