我正在使用持续集成,并发现了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.

其他回答

你链接的文档有摘要:

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.

值得注意的是,像alpine这样的轻节点docker镜像没有安装Python,这是npm ci使用的node-gyp的依赖项。

我认为,为了让npm ci工作,你需要在你的构建中安装Python作为依赖。

更多信息在这里Docker和npm - gyp ERR!不可以

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

这些命令在功能上非常相似,但不同之处在于安装包中指定的依赖项的方法。Json和包锁。json文件。

NPM ci会对应用的所有依赖项执行干净的安装,而NPM install可能会跳过系统中已经存在的某些安装。如果系统上已经安装的版本不是您的包,则可能会出现问题。Json意图安装,即安装版本不同于'所需'版本。

其他不同之处在于npm ci永远不会触及你的包*。json文件。如果软件包中的依赖版本不匹配,它将停止安装并显示一个错误。Json和包锁。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.