我最近刚升级到npm@5。我现在有一个包锁。包含package.json中的所有内容的Json文件。我希望,当我运行npm install时,依赖版本将从锁文件中提取,以确定应该在我的node_modules目录中安装什么。奇怪的是,它实际上最终修改和重写了我的包锁。json文件。

例如,锁文件的typescript被指定为2.1.6版本。然后,在执行npm install命令后,版本被更改为2.4.1。这似乎违背了锁文件的全部目的。

我错过了什么?我如何让npm尊重我的锁文件?


当前回答

我发现在npm 5.7.1的新版本中加入了新的命令npm ci,它将从package-lock安装。json只

The new npm ci command installs from your lock-file ONLY. If your package.json and your lock-file are out of sync then it will report an error. It works by throwing away your node_modules and recreating it from scratch. Beyond guaranteeing you that you'll only get what is in your lock-file it's also much faster (2x-10x!) than npm install when you don't start with a node_modules. As you may take from the name, we expect it to be a big boon to continuous integration environments. We also expect that folks who do production deploys from git tags will see major gains.

其他回答

Npm install检测对包的任何更改。Json文件,以反映相应的依赖列表。

例:如果用户添加或删除了一个新的依赖项,构建将下载或删除本地计算机中的依赖项。我们可以将其与java中的.m2存储库进行比较,其中maven会不断跟踪pom.xml文件以更新依赖项。

package-lock。Json是package的副本。Json在运行时被内部进程使用,唯一的区别是包锁。Json对用户来说是只读的。

简短的回答:

NPM安装荣誉包锁。Json,如果它满足package.json的要求。 如果它不满足这些要求,则更新包并覆盖包锁。 如果你希望安装失败,而不是覆盖包锁发生时,使用npm ci。


下面是一个可能解释这些事情的场景(经过NPM 6.3.0验证)

在包中声明一个依赖项。json:

"depA": "^1.0.0"

然后执行npm install,它会生成一个包锁。json:

"depA": "1.0.0"

几天后,“depA”的一个更新的小版本发布了,比如“1.1.0”,那么下面的情况是成立的:

npm ci       # respects only package-lock.json and installs 1.0.0

npm install  # also, respects the package-lock version and keeps 1.0.0 installed 
             # (i.e. when package-lock.json exists, it overrules package.json)

接下来,手动更新包。json:

"depA": "^1.1.0"

然后重新运行:

npm ci      # will try to honor package-lock which says 1.0.0
            # but that does not satisfy package.json requirement of "^1.1.0" 
            # so it would throw an error 

npm install # installs "1.1.0" (as required by the updated package.json)
            # also rewrites package-lock.json version to "1.1.0"
            # (i.e. when package.json is modified, it overrules the package-lock.json)

将来,您将能够使用——from-lock-file(或类似的)标志仅从包锁安装。Json而不修改它。

这对于CI等环境非常有用,因为这些环境中可复制的构建非常重要。

有关该特性的跟踪,请参见https://github.com/npm/npm/issues/18286。

使用npm ci命令代替npm install。

ci代表clean install。

它将基于包锁安装项目依赖项。Json文件,而不是宽大的包。Json文件依赖关系。

它将生成与团队成员相同的构建,并且速度更快。

你可以在这篇博客文章中阅读更多信息: https://blog.npmjs.org/post/171556855892/introducing-npm-ci-for-faster-more-reliable

使用新引进的

npm ci

NPM ci承诺为大型团队带来最大的好处。让开发人员能够在包锁上“签名”,从而促进大型团队之间更有效的协作,并且能够准确地安装锁文件中的内容,每个月可以为开发人员节省数十个小时(如果不是数百个小时的话),将团队解放出来,花更多时间构建和发布令人惊叹的东西。

引入npm ci以实现更快、更可靠的构建