我正在使用持续集成,并发现了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 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 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.
这些命令在功能上非常相似,但不同之处在于安装包中指定的依赖项的方法。Json和包锁。json文件。
NPM ci会对应用的所有依赖项执行干净的安装,而NPM install可能会跳过系统中已经存在的某些安装。如果系统上已经安装的版本不是您的包,则可能会出现问题。Json意图安装,即安装版本不同于'所需'版本。
其他不同之处在于npm ci永远不会触及你的包*。json文件。如果软件包中的依赖版本不匹配,它将停止安装并显示一个错误。Json和包锁。json文件。
你可以在这里阅读官方文件中更好的解释。
此外,您可能想在这里阅读有关包锁的内容。
NPM ci将删除任何现有的node_modules文件夹,并依赖于包锁。Json文件来安装每个包的特定版本。它比npm安装快得多,因为它跳过了一些特性。它的清洁状态安装非常适合ci/cd管道和docker构建!您还可以使用它一次性安装所有东西,而不是特定的包。
你链接的文档有摘要:
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.