我是否提交NPM 5创建的软件包锁。JSON文件?
npm 5今天发布,新功能包括确定性安装,并创建了一个创建软件包 - 洛克.json
文件。
该文件应该保存在源控件中吗?
我假设它类似于 <代码> Yarn.lock 和 composer.lock
,两者都应该保存在源控制中。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
是的,
软件包-Lock.json
旨在将其检查到源控件中。如果您使用的是NPM 5+,则可以在命令行上看到此通知:创建了一个lockfile作为package-lock.json。您应该提交此文件。
根据NPM帮助软件包 - 洛克
:Yes,
package-lock.json
is intended to be checked into source control. If you're using npm 5+, you may see this notice on the command line:created a lockfile as package-lock.json. You should commit this file.
According tonpm help package-lock.json
:是的,您应该:
package-lock.json
。npm ci
而不是npm npm install
在CI和本地开发机上构建应用程序时,npm CI
WorkFlow 需要 存在package> package> package-lock.json < /代码>。
npm install
命令的一个很大的缺点是其意外行为,它可能会突变package-lock.json
,而npm ci
仅使用版本 在Lockfile中指定,并产生错误package-lock.json
和package.json
因此,运行
npm安装
本地,ESP。在具有多个开发人员的大型团队中,可能会导致package-lock.json
和开发人员决定完全删除package> package> package> package> lock.json
。然而,有一个强大的用例可以相信项目的依赖项在不同的机器上以可靠的方式重复解决。
从
package-lock.json
您得到的完全:已知的状态。过去,我有没有
package-lock.json的项目一天,因为随机的依赖性有破坏性更新。
这些问题很难解决,因为您有时必须猜测最后一个工作版本是什么。
如果要添加新的依赖项,则仍然运行
npm install {dependency}
。如果要升级,请使用npm Update {dependenty}
或npm install $ {iDIDENDENCY}@{version}
并提交更改的package> package-lock.json
。如果升级失败,您可以恢复为最后一个已知的工作
package-lock.json
。to 报价npm doc :
以及
npm ci
vs vs vs之间的差异NPM安装
:注意:我发布了类似的答案在这里
Yes, you SHOULD:
package-lock.json
.npm ci
instead ofnpm install
when building your applications both on your CI and your local development machineThe
npm ci
workflow requires the existence of apackage-lock.json
.A big downside of
npm install
command is its unexpected behavior that it may mutate thepackage-lock.json
, whereasnpm ci
only uses the versions specified in the lockfile and produces an errorpackage-lock.json
andpackage.json
are out of syncpackage-lock.json
is missing.Hence, running
npm install
locally, esp. in larger teams with multiple developers, may lead to lots of conflicts within thepackage-lock.json
and developers to decide to completely delete thepackage-lock.json
instead.Yet there is a strong use-case for being able to trust that the project's dependencies resolve repeatably in a reliable way across different machines.
From a
package-lock.json
you get exactly that: a known-to-work state.In the past, I had projects without
package-lock.json
/npm-shrinkwrap.json
/yarn.lock
files whose build would fail one day because a random dependency got a breaking update.Those issue are hard to resolve as you sometimes have to guess what the last working version was.
If you want to add a new dependency, you still run
npm install {dependency}
. If you want to upgrade, use eithernpm update {dependency}
ornpm install ${dependendency}@{version}
and commit the changedpackage-lock.json
.If an upgrade fails, you can revert to the last known working
package-lock.json
.To quote npm doc:
And in regards to the difference between
npm ci
vsnpm install
:Note: I posted a similar answer here
是的,它旨在签入。我想建议它获得自己独特的提交。我们发现它为我们的差异增加了很多噪音。
Yes, it's intended to be checked in. I want to suggest that it gets its own unique commit. We find that it adds a lot of noise to our diffs.
是的,最好的做法是入住(是的,入住)
我同意,看到差异时会引起很多噪音或冲突。但是好处是:
package.json
中使用^1.2.3
,但是如何确保每次npm install
都会在您的开发机和构建服务器,尤其是那些间接依赖软件包?好吧,package-lock.json
将确保这一点。 (借助npm CI
,它基于锁定文件安装软件包)NPM审核修复
。Yes, the best practice is to check-in (YES, CHECK-IN)
I agree that it will cause a lot of noise or conflict when seeing the diff. But the benefits are:
^1.2.3
in yourpackage.json
, but how can you ensure each timenpm install
will pick up the same version in your dev machine and in the build server, especially those indirect dependency packages? Well,package-lock.json
will ensure that. (With the help ofnpm ci
which installs packages based on lock file)npm audit fix
.截至2023年4月,我的答案是:
当然,您必须提交此文件,
例如,CI平台上的标准安装命令将为
npm CI
需要该文件才能正常工作确保依赖树与所做的完全相同。我的旧答案既过时了(2018年),而且缺乏知识。是:
我不在项目中提交此文件。有什么意义?
由于我从不在pockage.json中使用 ^的
As of April 2023, my answer is:
OF COURSE YOU MUST COMMIT THIS FILE
For instance, standard install command on CI platforms would be
npm ci
needs that file to work properly to ENSURE the dependency tree is the exact same as commited.My old answer is both outdated (2018) and lacks knowledge. It was:
I don't commit this file in my projects. What's the point ?
Though it's true that I never use ^ in my package.json for libs because I had bad experiences with it.
对于在做git diff时抱怨噪音的人:
我所做的就是使用一个别名:
忽略整个存储库中的diffs intake-lock.json(每个人都使用它),您可以将其添加到
> .gitaTtributes < /code>:
这将导致差异显示“二进制文件a/package-lock.json和b/package-lock.json在更改软件包锁定文件时都会有所不同。此外,某些git服务(尤其是gitlab,但不是github, )在执行此操作时,在在线查看时,将从diffs中排除这些文件(不再更改10k行!)。
To the people complaining about the noise when doing git diff:
What I did was use an alias:
To ignore package-lock.json in diffs for the entire repository (everyone using it), you can add this to
.gitattributes
:This will result in diffs that show "Binary files a/package-lock.json and b/package-lock.json differ whenever the package lock file was changed. Additionally, some Git services (notably GitLab, but not GitHub) will also exclude these files (no more 10k lines changed!) from the diffs when viewing online when doing this.
是的,您可以提交此文件。来自 npm的官方文档:
Yes, you can commit this file. From the npm's official docs:
是的
,答案是肯定的,绝对总是将您的锁定命中率提交给git。
类似于Git Hashes
,如果您使用Git,则应使用锁紧file,因为它们具有相同的目的:
因为...
来自包装经理本身的
包装管理器供应商清楚地说,您应该提交Lockfile 。
NPM
https ://docs.npmjs.com/cli/v6/configuring-npm/package-locks
npm
npm 在没有lockfile的情况下安装时,它可以清楚地告诉您lockfile:yarn
“ noreferrer”> https://classic.yarnpkg .com/blog/2016/11/24/lockfiles-for-all/
pnpm
https://pnpm.io/git
原因是
一致性
一致性应该永远相同,其构建输出应应不会随着时间的流逝而改变。
来自NPM:
来自纱线:
更改的可追溯性
在发生变化时,您希望git跟踪这种变化。
来自NPM:
稳定性和安全性
引入错误和漏洞。
来自纱线:
常见的反对意见
“没有意义”
这是“无知的论点”,这是一个逻辑上的谬论。换句话说,“我不知道原因,所以没有”。
“它是生成的”
响应:生成不是缺陷。产生了git犯罪。我们不应该使用git吗?事实是,lockfile不是从包装中生成的确定性,因为它容易受到 time 的影响,并且在 npm注册表中的包装状态 >。这是一个快照,以实现稳定性。
“这会导致合并冲突”,
这是解决签到的锁紧件中的合并冲突的负担。
来自NPM:
来自纱线:
来自PNPM:
因此,所有软件包经理都可以自动解决锁紧冲突 。在较旧的版本中可能不是这种情况,但现在就是这种情况。
失败的唯一一次是如果
package.json
本身有冲突,因为您无法从无效的package.json
中安装。您必须手动解决这些冲突 ,就像您必须做的那样 。“合并冲突干扰了PR和MRS”
https://docs.renovatebot.com/noise-redacter.com/noise-reduction/#lock-file - 智慧
这是真的。 GIT提供商(GitHub,GitLab等)不会自动解决Lockfile冲突,因此这可能会增加合并的负担。但是,在称重这个骗局时,请理解锁紧files通常不会改变。它们仅在
package.json
deps更改时才更改,或者开发人员专门更改文件或已安装的node_modules
deps。“它会产生差异噪声”,
如果差异工具显示锁定差异,那是很多噪音。
这是事实,但是这是一个工具问题,许多工具可以优雅地处理(例如自动最小化,分页或虚拟滚动)。如果您根本不想看到lockfile差异,请尝试
git diff-。 ':(排除)yarn.lock'
,或者将文件标记为.gitattributes
中的二进制文件(但是,如果您将看到它的差异,如果您对您很重要)。“精确版本足够好”
评论/a>
这个想法是,在
package.json
的依赖项semver表达式中不使用范围与拥有lockfile的依赖关系semver表达式相同。这是错误的。即使您指定了确切的版本,您的依赖项也具有自己的依赖性,该依赖项可能会使用其版本范围,而不是确切的版本。因此,这最终不会锁定整个依赖树,而只能锁定其顶部。
“应用程序的Lockfiles,无库库”
的示例:
情感是,库需要对出血边缘deps做出反应,而没有锁定的lockfiles支持这一点。
来自纱线:
纱线文章深入消除了这一异议。 请阅读。
此参数中的常见错误是否认为 如果您不提交锁定,则不存在 。实际上, 它仍然存在 在您的机器上,锁定了依赖关系。通过对Lockfile进行限制,这种情况无法改善。
如果图书馆维护者希望不断测试兼容性,则在安装和构建库之前,他们应该删除其锁定文件(无论是否签入Lockfile!)。与签入的锁定文件的唯一区别在于,当发生这种情况时,您会有NODE_MODULES状态的持久记录,因此将来可以再现。
为此,有 greenkeeper 的机器人。绿色管理员的倡导者在Lockfiles中检查()和翻新bot毫无意义如果存在,请。
“锁紧files通过不同的系统产生不同的生成”,
这是提到的(例如在这里):不同的OS会生成不同的锁定文件。如果是这种情况,这是一个错误。
但是,不同版本的
npm
(或任何软件包管理器)可能会产生不同的Lockfile输出。我尚未确认这一点,但假设是这样,这是一个很小的代价来支付稳定性。为了解决此问题,贡献者将需要使用 nvm )的工具,需要切换其包装管理器版本。“ LockFiles可以是安全风险”
请参阅 snyk-为什么npm lockfiles可以成为注射恶意模块的安全盲点
这是一个真正的风险。一个带有Lockfile的公共项目可以接收具有Lockfile内容的恶意PR,一旦将分支拉出并安装了分支,可能会损害维护者的机器。
使用CI检查为此进行防御,例如 lockfile-lint 或简单地
npm ci
或YARN - IMMUTABLE
(YARN - 纱线1上的frozen-lockfile
),也许设置igaine
ignore-scripts 在您的中本地
NPMRC 。每当您安装带有不信任代码的软件包时,就会存在此风险。
总之,
总是犯下锁紧件。
Yes
The answer is yes, absolutely always commit your lockfile to git.
Analogy to git hashes
If you use git, you should use a lockfile because they serve the same purpose:
Because...
From the package managers themselves
Package manager vendors clearly say you should commit the lockfile.
npm
https://docs.npmjs.com/cli/v6/configuring-npm/package-locks
When
npm
installs without a lockfile, it tells you clearly to commit the lockfile:yarn
https://classic.yarnpkg.com/blog/2016/11/24/lockfiles-for-all/
pnpm
https://pnpm.io/git
Reasons
Consistency
A single commit should be the same forever, and its build output should not change over time.
From npm:
From yarn:
Traceability of changes
When something changes, you want git to track that change.
From npm:
Stability and security
Avoid introducing bugs and vulnerabilities.
From yarn:
Common Objections
"There's no point"
This is an "Argument from Ignorance" which is a logical fallacy. In other words, "I don't know the reason, so there is none".
"It's generated"
from comment
Response: being generated is not a flaw. Git commit hashes are generated; should we not use git? The truth is that the lockfile is not deterministically generated from package.json, because it is susceptible to time and the state of packages in the npm registry. It is a snapshot, for stability.
"It causes merge conflicts"
It's a burden to resolve merge conflicts in checked-in lockfiles.
From npm:
From yarn:
https://engineering.fb.com/2017/09/07/web/announcing-yarn-1-0/
From pnpm:
So all package managers resolve lockfile merge conflicts automatically. This may not be the case in older versions, but it is the case now.
The only time this fails is if the
package.json
itself has conflicts, because you can't install from an invalidpackage.json
. You must resolve those conflicts manually as you would have to do anyway."The merge conflicts interfere with PRs and MRs"
https://docs.renovatebot.com/noise-reduction/#lock-file-considerations
This is true. Git providers (GitHub, GitLab, etc) don't automatically resolve lockfile conflicts, so this may add a burden to merging. However when weighing this con, understand that lockfiles do not normally change; they only change when
package.json
deps change, or when a developer specifically changes the file or the installednode_modules
deps."It makes diff noise"
If diff tools show lockfile diffs, it's a lot of noise.
This is true, however it's a tooling problem which many tools can handle gracefully (such as auto-minimizing, paging, or virtual scrolling). If you don't want to see the lockfile diff at all, try
git diff -- . ':(exclude)yarn.lock'
, or alternatively mark the file as binary in.gitattributes
(however you won't see its diff, if that matters to you)."Exact versions are good enough"
comment
The idea is that not using ranges in your
package.json
's dependency semver expressions accomplishes the same thing as having a lockfile.This is false. Even if you specify exact versions, your dependencies have their own dependencies, which may use ranges for their versions, not exact versions. So this doesn't end up locking the whole dependency tree, only the top of it.
"Lockfiles for apps, no lockfiles for libraries"
Examples of this objection:
The sentiment is that libraries need to react to bleeding edge deps, and that not having lockfiles supports this.
From yarn:
The yarn article goes into depth to dispel this objection. Please read it.
A common error in this argument is the thought that if you don't commit the lockfile, it doesn't exist. In reality, it's still there on your machine, locking your dependencies. The situation is not improved by gitignoring the lockfile.
If a library maintainer wishes to continually test for compatibility, then they should delete their lockfile (whether the lockfile is checked in or not!) before installing and building their library. The only difference with a checked-in lockfile is that you have a persistent record of the state of node_modules when this happened, so it can be reproduced in the future.
There are bots like greenkeeper and renovate-bot for this. Greenkeeper advocates for checking in lockfiles (Greenkeeper and Lockfiles: A match made in heaven) and renovate-bot expresses no opinion but does commit lockfiles if present.
"Lockfiles are generated differently by different systems"
This is a claim mentioned (e.g. here): that different OSes generate different lockfile contents. If this is the case, this is a bug.
However it is possible that different versions of
npm
(or any package manager) may produce different lockfile output. I have not confirmed this, but hypothetically if so, it is a small price to pay for stability. To workaround this, contributors will need to switch their package manager version, by using a tool like nvm."Lockfiles can be a security risk"
See Snyk - Why npm lockfiles can be a security blindspot for injecting malicious modules
This is a real risk. A public project with a lockfile can receive a malicious PR with lockfile contents that could compromise a maintainer's machine once the branch is pulled and installed.
Defend against this with CI checks like lockfile-lint or simply
npm ci
oryarn --immutable
(yarn --frozen-lockfile
on Yarn 1), and perhaps settingignore-scripts
locally in yournpmrc
.This risk is present whenever you install a package with untrusted code.
In Conclusion
Always commit the lockfile.
是的,这是提交
package-lock.json
的标准练习。进行
package-lock.json
的主要原因是项目中的每个人都在同一软件包版本上。优点:
缺点:
npm install
无法确保项目中的每个人都在同一软件包版本上。NPM CI
将有助于解决此问题。Yes, it's a standard practice to commit
package-lock.json
.The main reason for committing
package-lock.json
is that everyone in the project is on the same package version.Pros:
Cons:
npm install
won't make sure that everyone in the project is on the same package version.npm ci
will help with this.禁用包装锁。json全球
在您的终端中键入以下内容:
这对我来说真的很有用,就像魔术一样
Disable package-lock.json globally
type the following in your terminal:
this really work for me like magic
所有反应都是肯定的;但是,根据文档,这会根据项目的类型而变化。
NPM文档部分的报价
但是,您应该在存储库中使用
package-lock.json
来锁定测试依赖项的版本,构建依赖项等。但是,如果您使用的是 lerna 用多个软件包管理项目,则应放置
软件包-lock.json
仅在您的存储库的根部,而不是在使用npm init
创建的每个子书中。您最终会得到这样的事情:All responses are affirmative; however, this varies according to the type of project, according to the documentation.
Quote from the npm docs section
This means that you don't need to publish your
package-lock.json
to npm for dependencies, but you should usepackage-lock.json
in your repository to lock the versions of your test dependencies, build dependencies, and so on.However, if you are using lerna to manage projects with multiple packages, you should place the
package-lock.json
only at the root of your repository, not in each subpackage created withnpm init
. You will end up with something like this:tldr
在这些情况下提交:
npm/yarn
软件包为项目。package.json
更新.lock
文件中的任何更改。lock
文件。在这些情况下不承诺:
这意味着另一个开发人员在上述情况下没有推动他们的锁定文件1,2,3
这意味着在上述情况下,另一个开发人员没有推动其锁定文件1,2,3
这意味着在上述情况下,另一个开发人员没有按下他们的锁定文件1,2,3
这意味着另一个开发人员在上述情况下没有推动其锁定文件1,2,3
结论
锁定文件的主要目标就是这样
所有开发人员
&amp;所有环境
&amp;所有安装项目的机器
应具有超准确库&amp; 基本上,版本仅有 3的情况。
TLDR
Commit in these situations:
npm/yarn
packages to project.package.json
updates the.lock
file..lock
file.Do not commit in these situations:
it means another dev did not push their lock file during the above cases 1,2,3
it means another dev did not push their lock file during the above cases 1,2,3
it means another dev did not push their lock file during the above cases 1,2,3
it means another dev did not push their lock file during the above cases 1,2,3
Conclusion
The main goal of a lock file is that
all developers
&all environments
&all machines
on which the project is installed should have ULTRA ACCURATE libraries & versionsso basically, there are only 3 situations for committing.
是的,您应该提交它,因为它包含有关已安装的内容的信息,并且此信息可能很有用 - 例如,您可以通过执行
npm ci
在ci/cd管道中使用它。实现确定性构建或其他开发人员想要通过npm ci
精确安装相同的依赖项。Yes you should commit it, because it holds information about what got installed and this information can be useful — for example, you can make use of it in CI/CD pipelines via executing
npm ci
in them in order achieve deterministic builds or when another developer wants to exactly install the same dependencies what you have installed vianpm ci
.关于为什么要提交软件包洛克.json
为什么不将精确版本保留在package.json?
您的软件包中。如果它们也有依赖关系(并且确实如此),那么这些版本就不会被锁定。
为什么不删除软件包洛克.json?
考虑一下,如果您删除软件包锁和重新安装,则强制依赖性树中所有包装的最新版本。意思是,您正在改变(可能)整个应用程序的行为。
您真正想做什么?
如果您有一些与NPM相关的怪异问题,只需删除node_modules并再次安装NPM。删除软件包锁。json从来都不是解决方案。
为什么要提交软件包洛克.json?
如果您不提交它,那么其他所有人都将获得的应用程序的版本与您在本地运行的版本不同。这意味着事情可能对您有用,但是破坏CI/生产/其他本地机器
希望它有助于理解。
Bit background about why to commit package-lock.json
Why not just keep exact versions in package.json?
Your package.json only points to the versions of your direct dependencies. If they have dependencies too (and they do), these versions won't be locked.
Why not delete package-lock.json?
Think about it, if you delete package-lock and re-install, you are forcing the latest versions of all packages in the dependency tree. Meaning, you are changing the behavior of (potentially) the entire application.
What are you really trying to do?
If you have some weird npm-related problem, simply remove node_modules and run npm install again. Removing package-lock.json is never the solution.
Why commit package-lock.json?
If you don't commit it, then the version of the application everyone else will get is different than what you are running locally. This means that things might work for you, but break on the CI/production/other local machines
Hope it helps to understand.
我对NPM的使用是生成缩小/uglified的CSS/JS,并生成Django应用程序提供的页面中所需的JavaScript。在我的应用程序中,JavaScript在页面上运行以创建动画,有时会执行Ajax调用,在VUE框架中工作和/或与CSS一起工作。如果package-lock.json对包装中的内容具有一些压倒性的控制。根据我的经验,它要么不影响NPM安装安装的内容,要么在此过程中,它并没有对我所知的我部署的应用程序产生不利影响。我不使用传统上稀薄客户端的MongoDB或其他此类应用程序。
我从仓库中删除包装锁。
因为NPM安装生成了此文件,而NPM安装是运行应用程序的每个服务器上部署过程的一部分。节点和NPM的版本控件是在每个服务器上手动完成的,但是我要小心它们是相同的。
当在服务器上运行
npm install
时,它会更改package-lock.json,而且,如果对服务器上的存储库记录的文件有更改,那么下一个部署将不允许您从Origin提取新更改。那是
您无法部署,因为拉力会覆盖已对包装锁的更改。
您甚至都不能覆盖本地生成的软件包 - 锁。 node_modules由于npm安装而导致,因此破坏了部署。现在,如果这表明已经在Node_Modules中安装了略有不同的版本,那再次从未引起我的问题。
如果node_modules不在您的存储库中(不应该是),则应忽略软件包锁。
如果我缺少某些内容,请在评论中纠正我,但是从该文件中进行版本控制的意义没有意义。文件poffage.json中有版本号,我认为此文件是在NPM安装时用于构建软件包的一个文件,就像我删除它时一样,NPM安装申诉如下
:应用NPM构建JS/CSS,如果我删除包裹锁。
My use of npm is to generate minified/uglified css/js and to generate the javascript needed in pages served by a django application. In my applications, Javascript runs on the page to create animations, some times perform ajax calls, work within a VUE framework and/or work with the css. If package-lock.json has some overriding control over what is in package.json, then it may be necessary that there is one version of this file. In my experience it either does not effect what is installed by npm install, or if it does, It has not to date adversely affected the applications I deploy to my knowledge. I don't use mongodb or other such applications that are traditionally thin client.
I remove package-lock.json from repo
because npm install generates this file, and npm install is part of the deploy process on each server that runs the app. Version control of node and npm are done manually on each server, but I am careful that they are the same.
When
npm install
is run on the server, it changes package-lock.json,and if there are changes to a file that is recorded by the repo on the server, the next deploy WONT allow you to pull new changes from origin. That is
you can't deploy because the pull will overwrite the changes that have been made to package-lock.json.
You can't even overwrite a locally generated package-lock.json with what is on the repo (reset hard origin master), as npm will complain when ever you issue a command if the package-lock.json does not reflect what is in node_modules due to npm install, thus breaking the deploy. Now if this indicates that slightly different versions have been installed in node_modules, once again that has never caused me problems.
If node_modules is not on your repo (and it should not be), then package-lock.json should be ignored.
If I am missing something, please correct me in the comments, but the point that versioning is taken from this file makes no sense. The file package.json has version numbers in it, and I assume this file is the one used to build packages when npm install occurs, as when I remove it, npm install complains as follows:
and the build fails, however when installing node_modules or applying npm to build js/css, no complaint is made if I remove package-lock.json