gpt4 book ai didi

npm - "npm install"和 "npm ci"和有什么区别?

转载 作者:行者123 更新时间:2023-12-03 04:22:31 27 4
gpt4 key购买 nike

我正在进行持续集成并发现了 npm ci命令。

我不知道在我的工作流程中使用此命令有什么优势。

速度更快吗?这会让测试变得更难吗?好吗?

最佳答案

来自the official documentation for 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 读取 package.json 以创建依赖项列表,并使用 package-lock.json 告知哪个要安装的这些依赖项的版本。 如果依赖项不在 package-lock.json 中,它将由 npm install 添加。

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

它直接从 package-lock.json 安装依赖项,并仅使用 package.json 来验证是否存在不匹配的版本。 如果缺少任何依赖项或版本不兼容,则会抛出错误

使用npm install添加新的依赖项,并更新项目的依赖项。通常,您会在开发期间拉取更新依赖项列表的更改后使用它,但在这种情况下使用 npm ci 可能是个好主意。

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

npm install

  • 安装软件包及其所有依赖项。
  • 依赖项由 npm-shrinkwrap.jsonpackage-lock.json(按顺序)驱动。
  • 不带参数:安装本地模块的依赖项。
  • 可以安装全局包。
  • 将在 node_modules 中安装所有缺少的依赖项。
  • 它可能会写入package.jsonpackage-lock.json
    • 当与参数 (npm i packagename) 一起使用时,它可能会写入 package.json 以添加或更新依赖项。
    • 当不带参数使用时,(npm i)它可能会写入package-lock.json来锁定某些依赖项的版本(如果它们尚未在此版本中)文件。

npm ci

  • 至少需要 npm v5.7.1 .
  • 需要存在 package-lock.jsonnpm-shrinkwrap.json
  • 如果这两个文件的依赖项与 package.json 不匹配,则会引发错误。
  • 立即删除node_modules并安装所有依赖项
  • 它永远不会写入 package.jsonpackage-lock.json

算法

npm cipackage-lock.jsonnpm-shrinkwrap.json生成整个依赖关系树时,npm install 使用以下算法更新 node_modules 的内容 ( source ):

load the existing node_modules tree from disk
clone the tree
fetch the package.json and assorted metadata and add it to the clone
walk the clone and add any missing dependencies
dependencies will be added as close to the top as is possible
without breaking any other modules
compare the original tree with the cloned tree and make a list of
actions to take to convert one to the other
execute all of the actions, deepest first
kinds of actions are install, update, remove and move

关于npm - "npm install"和 "npm ci"和有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52499617/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com