Team Ai
Datasetpublic

codekingpro/portable-devtools

sourceHugging Faceupdated 5mo agoView on Hugging Face
1likes14kdownloads
package-lock-json.md183 linesDownload Raw Back to configuring-npm
1---
2title: package-lock.json
3section: 5
4description: A manifestation of the manifest
5---
6
7### Description
8
9`package-lock.json` is automatically generated for any operations where npm modifies either the `node_modules` tree, or `package.json`.
10It describes the exact tree that was generated, such that subsequent installs are able to generate identical trees, regardless of intermediate dependency updates.
11
12This file is intended to be committed into source repositories, and serves various purposes:
13
14* Describe a single representation of a dependency tree such that teammates, deployments, and continuous integration are guaranteed to install exactly the same dependencies.
15
16* Provide a facility for users to "time-travel" to previous states of `node_modules` without having to commit the directory itself.
17
18* Facilitate greater visibility of tree changes through readable source control diffs.
19
20* Optimize the installation process by allowing npm to skip repeated metadata resolutions for previously-installed packages.
21
22* As of npm v7, lockfiles include enough information to gain a complete picture of the package tree, reducing the need to read `package.json` files, and allowing for significant performance improvements.
23
24When `npm` creates or updates `package-lock.json`, it will infer line endings and indentation from `package.json` so that the formatting of both files matches.
25
26### `package-lock.json` vs `npm-shrinkwrap.json`
27
28Both of these files have the same format, and perform similar functions in the root of a project.
29
30The difference is that `package-lock.json` cannot be published, and it will be ignored if found in any place other than the root project.
31
32In contrast, [npm-shrinkwrap.json](/configuring-npm/npm-shrinkwrap-json) allows publication, and defines the dependency tree from the point encountered.
33This is not recommended unless deploying a CLI tool or otherwise using the publication process for producing production packages.
34
35If both `package-lock.json` and `npm-shrinkwrap.json` are present in the root of a project, `npm-shrinkwrap.json` will take precedence and `package-lock.json` will be ignored.
36
37### Hidden Lockfiles
38
39In order to avoid processing the `node_modules` folder repeatedly, npm as of v7 uses a "hidden" lockfile present in `node_modules/.package-lock.json`.
40This contains information about the tree, and is used in lieu of reading the entire `node_modules` hierarchy provided that the following conditions are met:
41
42- All package folders it references exist in the `node_modules` hierarchy.
43- No package folders exist in the `node_modules` hierarchy that are not listed in the lockfile.
44- The modified time of the file is at least as recent as all of the package folders it references.
45
46That is, the hidden lockfile will only be relevant if it was created as part of the most recent update to the package tree.
47If another CLI mutates the tree in any way, this will be detected, and the hidden lockfile will be ignored.
48
49Note that it _is_ possible to manually change the _contents_ of a package in such a way that the modified time of the package folder is unaffected.
50For example, if you add a file to `node_modules/foo/lib/bar.js`, then the modified time on `node_modules/foo` will not reflect this change.
51If you are manually editing files in `node_modules`, it is generally best to delete the file at `node_modules/.package-lock.json`.
52
53As the hidden lockfile is ignored by older npm versions, it does not contain the backwards compatibility affordances present in "normal" lockfiles.
54That is, it is `lockfileVersion: 3`, rather than `lockfileVersion: 2`.
55
56### Handling Old Lockfiles
57
58When npm detects a lockfile from npm v6 or before during the package installation process, it is automatically updated to fetch missing information from either the `node_modules` tree or (in the case of empty `node_modules` trees or very old lockfile formats) the npm registry.
59
60### File Format
61
62#### `name`
63
64The name of the package this is a package-lock for.
65This will match what's in `package.json`.
66
67#### `version`
68
69The version of the package this is a package-lock for.
70This will match what's in `package.json`.
71
72#### `lockfileVersion`
73
74An integer version, starting at `1` with the version number of this document whose semantics were used when generating this `package-lock.json`.
75
76Note that the file format changed significantly in npm v7 to track information that would have otherwise required looking in `node_modules` or the npm registry.
77Lockfiles generated by npm v7 will contain `lockfileVersion: 2`.
78
79* No version provided: an "ancient" shrinkwrap file from a version of npm prior to npm v5.
80* `1`: The lockfile version used by npm v5 and v6.
81* `2`: The lockfile version used by npm v7 and v8. Backwards compatible to v1 lockfiles.
82* `3`: The lockfile version used by npm v9 and above.
83  Backwards compatible to npm v7.
84
85npm will always attempt to get whatever data it can out of a lockfile, even if it is not a version that it was designed to support.
86
87#### `packages`
88
89This is an object that maps package locations to an object containing the information about that package.
90
91The root project is typically listed with a key of `""`, and all other packages are listed with their relative paths from the root project folder.
92
93Package descriptors have the following fields:
94
95* version: The version found in `package.json`
96
97* resolved: The place where the package was actually resolved from.
98  In the case of packages fetched from the registry, this will be a url to a tarball.
99  In the case of git dependencies, this will be the full git url with commit sha.
100  In the case of link dependencies, this will be the location of the link target.
101  `registry.npmjs.org` is a magic value meaning "the currently configured registry".
102
103* integrity: A `sha512` or `sha1` [Standard Subresource Integrity](https://w3c.github.io/webappsec/specs/subresourceintegrity/) string for the artifact that was unpacked in this location.
104
105* link: A flag to indicate that this is a symbolic link.
106  If this is present, no other fields are specified, since the link target will also be included in the lockfile.
107
108* dev, optional, devOptional: If the package is strictly part of the
109  `devDependencies` tree, then `dev` will be true.
110  If it is strictly part of the `optionalDependencies` tree, then `optional` will be set.
111  If it is both a `dev` dependency _and_ an `optional` dependency of a non-dev dependency, then `devOptional` will be set.
112  (An `optional` dependency of a `dev` dependency will have both `dev` and `optional` set.)
113
114* inBundle: A flag to indicate that the package is a bundled dependency.
115
116* hasInstallScript: A flag to indicate that the package has a `preinstall`, `install`, or `postinstall` script.
117
118* hasShrinkwrap: A flag to indicate that the package has an `npm-shrinkwrap.json` file.
119
120* bin, license, engines, dependencies, optionalDependencies: fields from `package.json`
121
122* os: An array of operating systems this package is compatible with, as specified in `package.json`. This field is included when the package specifies OS restrictions.
123
124* cpu: An array of CPU architectures this package is compatible with, as specified in `package.json`. This field is included when the package specifies CPU restrictions.
125
126* funding: Funding information for the package, as specified in `package.json`. This field contains details about how to support the package maintainers.
127
128#### dependencies
129
130Legacy data for supporting versions of npm that use `lockfileVersion: 1`.
131This is a mapping of package names to dependency objects.
132Because the object structure is strictly hierarchical, symbolic link dependencies are somewhat challenging to represent in some cases.
133
134npm v7 ignores this section entirely if a `packages` section is present, but does keep it up to date in order to support switching between npm v6 and npm v7.
135
136Dependency objects have the following fields:
137
138* version: a specifier that varies depending on the nature of the package, and is usable in fetching a new copy of it.
139  Note that for peer dependencies that are not installed, or optional dependencies that are not installed, this field may be omitted.
140
141    * bundled dependencies: Regardless of source, this is a version number that is purely for informational purposes.
142    * registry sources: This is a version number.
143      (eg, `1.2.3`)
144    * git sources: This is a git specifier with resolved committish.
145      (eg, `git+https://example.com/foo/bar#115311855adb0789a0466714ed48a1499ffea97e`)
146    * http tarball sources: This is the URL of the tarball.
147      (eg, `https://example.com/example-1.3.0.tgz`)
148    * local tarball sources: This is the file URL of the tarball.
149      (eg `file:///opt/storage/example-1.3.0.tgz`)
150    * local link sources: This is the file URL of the link.
151      (eg `file:libs/our-module`)
152
153    **Note:** The `version` field may be omitted for certain types of dependencies, such as optional peer dependencies that are not installed. In these cases, only metadata fields like `dev`, `optional`, and `peer` will be present.
154
155* integrity: A `sha512` or `sha1` [Standard Subresource Integrity](https://w3c.github.io/webappsec/specs/subresourceintegrity/) string for the artifact that was unpacked in this location.
156  For git dependencies, this is the commit sha.
157
158* resolved: For registry sources this is path of the tarball relative to the registry URL.
159  If the tarball URL isn't on the same server as the registry URL then this is a complete URL.
160  `registry.npmjs.org` is a magic value meaning "the currently configured registry".
161
162* bundled:  If true, this is the bundled dependency and will be installed by the parent module.
163  When installing, this module will be extracted from the parent module during the extract phase, not installed as a separate dependency.
164
165* dev: If true then this dependency is either a development dependency ONLY of the top level module or a transitive dependency of one.
166  This is false for dependencies that are both a development dependency of the top level and a transitive dependency of a non-development dependency of the top level.
167
168* optional: If true then this dependency is either an optional dependency ONLY of the top level module or a transitive dependency of one.
169  This is false for dependencies that are both an optional dependency of the top level and a transitive dependency of a non-optional dependency of the top level.
170
171* requires: This is a mapping of module name to version.
172  This is a list of everything this module requires, regardless of where it will be installed.
173  The version should match via normal matching rules a dependency either in our `dependencies` or in a level higher than us.
174
175* dependencies: The dependencies of this dependency, exactly as at the top level.
176
177### See also
178
179* [npm shrinkwrap](/commands/npm-shrinkwrap)
180* [npm-shrinkwrap.json](/configuring-npm/npm-shrinkwrap-json)
181* [package.json](/configuring-npm/package-json)
182* [npm install](/commands/npm-install)
183 
codekingpro/portable-devtools · Team Ai