Reputation: 5639
npm@5 has been published, it has a new feature package-lock.json
file (after npm install
) which confuses me. I want to know, what is the effect of this file?
Upvotes: 563
Views: 397896
Reputation: 450
The goal of package-lock.json
file is to keep track of the exact version of every package that is installed so that a product is 100% reproducible in the same way even if packages are updated by their maintainers.
This solves a very specific problem that package.json
left unsolved. In package.json you can set which versions you want to upgrade to (patch or minor), using the semver notation.
Upvotes: 2
Reputation: 13349
It stores an exact, versioned dependency tree rather than using starred versioning like package.json
itself (e.g. 1.0.*). This means you can guarantee the dependencies for other developers or prod releases, etc. It also has a mechanism to lock (hence the name package-lock
) the tree but generally will regenerate if package.json
changes.
From the npm docs:
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.
And optimize the installation process by allowing npm to skip repeated metadata resolutions for previously-installed packages."
To answer jrahhali's question below about just using the package.json
with exact version numbers. Bear in mind that your package.json
contains only your direct dependencies, not the dependencies of your dependencies (sometimes called nested or transitive dependencies). This means with the standard package.json
you can't control the versions of those nested dependencies, referencing them directly or as peer dependencies won't help as you also don't control the version tolerance that your direct dependencies define for these nested dependencies.
Even if you lock down the versions of your direct dependencies you cannot 100% guarantee that your full dependency tree will be identical every time. Secondly you might want to allow non-breaking changes (based on semantic versioning) of your direct dependencies which gives you even less control of nested dependencies plus you again can't guarantee that your direct dependencies won't at some point break semantic versioning rules themselves.
The solution to all this is the package-lock.json
file which as described above locks in the versions of the full dependency tree. This allows you to guarantee your dependency tree for other developers or for releases, whilst still allowing testing of new dependency versions (direct or indirect) using your standard package.json
.
NB. The previous npm-shrinkwrap.json
did pretty much the same thing, but the lock file renames it so that its function is clearer. If there's already a shrink wrap file in the project, then this will be used instead of any lock file.
Since NPM v8.3.0 (2021-12-09), package.json
has a property overrides
which might help you not to touch package-lock.json
file.
To make sure the package foo is always installed as version 1.0.0 no matter what version your dependencies rely on:
{
"overrides": {
"foo": "1.0.0"
}
}
https://docs.npmjs.com/cli/v9/configuring-npm/package-json#overrides
Upvotes: 532
Reputation: 133
package.json
file contains the main names of packages & libraries which you installed and you can edit it, but package-lock.json
contains the details of each package and the link of repository of each package (consider it as the details of the packages which came from package.json
).
Reference: package-lock
Upvotes: -3
Reputation: 36520
It's an very important improvement for npm: guarantee exact same version of every package.
How to make sure your project built with same packages in different environments in a different time? Let's say, you may use ^1.2.3
in your package.json
, or some of your dependencies are using that way, but how can you ensure each time npm install
will pick up same version in your dev machine and in the build server? package-lock.json will ensure that.
npm install
will re-generate the lock file.
When on build server or deployment server, do npm ci
(which will read from the lock file, and install the whole package tree)
Upvotes: 103
Reputation: 9
package-lock.json
: It contains the exact version details that is currently installed for your Application.
Upvotes: 0
Reputation: 41
This file is automatically created and used by npm to keep track of your package installations and to better manage the state and history of your project’s dependencies. You shouldn’t alter the contents of this file.
Upvotes: 3
Reputation: 693
One important thing to mention as well is the security improvement that comes with the package-lock file. Since it keeps all the hashes of the packages if someone would tamper with the public npm registry and change the source code of a package without even changing the version of the package itself it would be detected by the package-lock file.
Upvotes: 18
Reputation: 1323
package-lock.json
is written to when a numerical value in a property such as the "version" property, or a dependency property is changed in package.json
.
If these numerical values in package.json
and package-lock.json
match, package-lock.json
is read from.
If these numerical values in package.json
and package-lock.json
do not match, package-lock.json
is written to with those new values, and new modifiers such as the caret and tilde if they are present. But it is the numeral that is triggering the change to package-lock.json
.
To see what I mean, do the following. Using package.json
without package-lock.json
, run npm install
with:
{
"name": "test",
"version": "1.0.0",
...
"devDependencies": {
"sinon": "7.2.2"
}
}
package-lock.json
will now have:
"sinon": {
"version": "7.2.2",
Now copy/paste both files to a new directory. Change package.json
to (only adding caret):
{
"name": "test",
"version": "1.0.0",
...
"devDependencies": {
"sinon": "^7.2.2"
}
}
run npm install
. If there were no package-lock.json
file, [email protected] would be installed. npm install
is reading from package-lock.json
and installing 7.2.2.
Now change package.json
to:
{
"name": "test",
"version": "1.0.0",
...
"devDependencies": {
"sinon": "^7.3.0"
}
}
run npm install
. package-lock.json
has been written to, and will now show:
"sinon": {
"version": "^7.3.0",
Upvotes: 49