gpt4 book ai didi

NPM5,package-lock.json 与 package.json 有什么区别?

转载 作者:行者123 更新时间:2023-12-02 15:20:29 24 4
gpt4 key购买 nike

将 NPM 更新到版本 5 后,我发现 package-lock.json 文件包含 package.json。

这两个文件有什么区别?

package-lock.json 有哪些优点?

最佳答案

package.json 文件:列出您的项目所依赖的包。允许您使用语义版本控制规则指定项目可以使用的包的版本。

根据 npm 文档,

package-lock.json is automatically generated for any operations where npm modifies either the node_modules tree, or package.json . It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates.

This file is intended to be committed into source repositories, and serves various purposes:

  • Describe a single representation of a dependency tree such that teammates, deployments, and continuous integration are guaranteed to install exactly the same dependencies.

  • Provide a facility for users to "time-travel" to previous states of node_modules without having to commit the directory itself.

  • To facilitate greater visibility of tree changes through readable source control diffs.

基本上,package-lock.json 用于通过允许 npm 跳过先前安装的软件包的重复元数据解析来优化安装过程。

在 npm 5.x.x 之前,package.json 是项目的真实来源。 package.json 中存在的是法律。 npm 用户喜欢这种模型,并且非常习惯于维护他们的包文件。然而,当包锁第一次被引入时,它的表现却与许多人的预期相反。考虑到预先存在的包和包锁,对 package.json 的更改(许多用户认为是事实来源)并未反射(reflect)在包锁中。

示例:包 A,版本 1.0.0 位于包和包锁中。在package.json中,A被手动编辑为1.1.0版本。如果认为 package.json 是真实来源的用户运行 npm install,他们会期望安装 1.1.0 版本。但是,尽管在 package.json 中列出了 v1.1.0,但仍安装了 1.0.0 版本。

示例:package-lock 中不存在模块,但 package.json 中确实存在该模块。作为将 package.json 视为事实来源的用户,我希望安装我的模块。但是,由于该模块不存在于 package-lock 中,因此未安装它,并且我的代码因找不到该模块而失败。

Official npm Documentation 中了解有关 package-lock.json 的更多信息!

关于NPM5,package-lock.json 与 package.json 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48456236/

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