From acd5b062a811fcd98849df908ce26855823ca671 Mon Sep 17 00:00:00 2001 From: isaacs Date: Mon, 11 Jan 2021 17:30:58 -0800 Subject: [PATCH] fix(docs): update package-lock.json docs PR-URL: https://github.com/npm/cli/pull/2475 Credit: @isaacs Close: #2475 Reviewed-by: @wraithgar --- docs/content/commands/npm-audit.md | 1 - docs/content/commands/npm-ci.md | 2 +- docs/content/commands/npm-shrinkwrap.md | 14 +- docs/content/configuring-npm/install.md | 53 +- ...inkwrap-json.md => npm-shrinkwrap-json.md} | 20 +- docs/content/configuring-npm/npmrc.md | 53 +- docs/content/configuring-npm/package-json.md | 743 ++++++++++-------- .../configuring-npm/package-lock-json.md | 257 ++++-- docs/content/configuring-npm/package-locks.md | 178 ----- 9 files changed, 666 insertions(+), 655 deletions(-) rename docs/content/configuring-npm/{shrinkwrap-json.md => npm-shrinkwrap-json.md} (61%) delete mode 100644 docs/content/configuring-npm/package-locks.md diff --git a/docs/content/commands/npm-audit.md b/docs/content/commands/npm-audit.md index 2c0a8f58047ca..7ad950a6ba99e 100644 --- a/docs/content/commands/npm-audit.md +++ b/docs/content/commands/npm-audit.md @@ -190,5 +190,4 @@ $ npm audit --audit-level=moderate ### See Also * [npm install](/commands/npm-install) -* [package-locks](/configuring-npm/package-locks) * [config](/using-npm/config) diff --git a/docs/content/commands/npm-ci.md b/docs/content/commands/npm-ci.md index 0d874f4f27a50..925ba8de2e5b7 100644 --- a/docs/content/commands/npm-ci.md +++ b/docs/content/commands/npm-ci.md @@ -68,4 +68,4 @@ cache: ### See Also * [npm install](/commands/npm-install) -* [package-locks](/configuring-npm/package-locks) +* [package-lock.json](/configuring-npm/package-lock-json) diff --git a/docs/content/commands/npm-shrinkwrap.md b/docs/content/commands/npm-shrinkwrap.md index 05d5706b9f9f1..dce50b7843bc3 100644 --- a/docs/content/commands/npm-shrinkwrap.md +++ b/docs/content/commands/npm-shrinkwrap.md @@ -13,18 +13,18 @@ npm shrinkwrap ### Description This command repurposes `package-lock.json` into a publishable -`npm-shrinkwrap.json` or simply creates a new one. The file created and updated -by this command will then take precedence over any other existing or future -`package-lock.json` files. For a detailed explanation of the design and purpose -of package locks in npm, see [package-locks](/configuring-npm/package-locks). +`npm-shrinkwrap.json` or simply creates a new one. The file created and +updated by this command will then take precedence over any other existing +or future `package-lock.json` files. For a detailed explanation of the +design and purpose of package locks in npm, see +[package-lock-json](/configuring-npm/package-lock-json). ### See Also * [npm install](/commands/npm-install) * [npm run-script](/commands/npm-run-script) * [npm scripts](/using-npm/scripts) -* [package.js](/configuring-npm/package-json) -* [package-locks](/configuring-npm/package-locks) +* [package.json](/configuring-npm/package-json) * [package-lock.json](/configuring-npm/package-lock-json) -* [shrinkwrap.json](/configuring-npm/shrinkwrap-json) +* [npm-shrinkwrap.json](/configuring-npm/npm-shrinkwrap-json) * [npm ls](/commands/npm-ls) diff --git a/docs/content/configuring-npm/install.md b/docs/content/configuring-npm/install.md index 3fc91045c6971..43fce4868ba97 100644 --- a/docs/content/configuring-npm/install.md +++ b/docs/content/configuring-npm/install.md @@ -6,17 +6,27 @@ description: Download and install node and npm ### Description -To publish and install packages to and from the public npm registry, you must install Node.js and the npm command line interface using either a Node version manager or a Node installer. **We strongly recommend using a Node version manager to install Node.js and npm.** We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages globally. +To publish and install packages to and from the public npm registry, you +must install Node.js and the npm command line interface using either a Node +version manager or a Node installer. **We strongly recommend using a Node +version manager to install Node.js and npm.** We do not recommend using a +Node installer, since the Node installation process installs npm in a +directory with local permissions and can cause permissions errors when you +run npm packages globally. ### Overview -- [Checking your version of npm and Node.js](#checking-your-version-of-npm-and-node-js) -- [Using a Node version manager to install Node.js and npm](#using-a-node-version-manager-to-install-node-js-and-npm) -- [Using a Node installer to install Node.js and npm](#using-a-node-installer-to-install-node-js-and-npm) +- [Checking your version of npm and + Node.js](#checking-your-version-of-npm-and-node-js) +- [Using a Node version manager to install Node.js and + npm](#using-a-node-version-manager-to-install-node-js-and-npm) +- [Using a Node installer to install Node.js and + npm](#using-a-node-installer-to-install-node-js-and-npm) ### Checking your version of npm and Node.js -To see if you already have Node.js and npm installed and check the installed version, run the following commands: +To see if you already have Node.js and npm installed and check the +installed version, run the following commands: ``` node -v @@ -25,7 +35,10 @@ npm -v ### Using a Node version manager to install Node.js and npm -Node version managers allow you to install and switch between multiple versions of Node.js and npm on your system so you can test your applications on multiple versions of npm to ensure they work for users on different versions. +Node version managers allow you to install and switch between multiple +versions of Node.js and npm on your system so you can test your +applications on multiple versions of npm to ensure they work for users on +different versions. #### OSX or Linux Node version managers @@ -39,28 +52,36 @@ Node version managers allow you to install and switch between multiple versions ### Using a Node installer to install Node.js and npm -If you are unable to use a Node version manager, you can use a Node installer to install both Node.js and npm on your system. +If you are unable to use a Node version manager, you can use a Node +installer to install both Node.js and npm on your system. * [Node.js installer](https://nodejs.org/en/download/) -* [NodeSource installer](https://github.com/nodesource/distributions). If you use Linux, we recommend that you use a NodeSource installer. +* [NodeSource installer](https://github.com/nodesource/distributions). If + you use Linux, we recommend that you use a NodeSource installer. #### OS X or Windows Node installers -If you're using OS X or Windows, use one of the installers from the [Node.js download page](https://nodejs.org/en/download/). Be sure to install the version labeled **LTS**. Other versions have not yet been tested with npm. +If you're using OS X or Windows, use one of the installers from the +[Node.js download page](https://nodejs.org/en/download/). Be sure to +install the version labeled **LTS**. Other versions have not yet been +tested with npm. #### Linux or other operating systems Node installers -If you're using Linux or another operating system, use one of the following installers: +If you're using Linux or another operating system, use one of the following +installers: -- [NodeSource installer](https://github.com/nodesource/distributions) (recommended) -- One of the installers on the [Node.js download page](https://nodejs.org/en/download/) - -Or see [this page](https://nodejs.org/en/download/package-manager/) to install npm for Linux in the way many Linux developers prefer. +- [NodeSource installer](https://github.com/nodesource/distributions) + (recommended) +- One of the installers on the [Node.js download + page](https://nodejs.org/en/download/) +Or see [this page](https://nodejs.org/en/download/package-manager/) to +install npm for Linux in the way many Linux developers prefer. #### Less-common operating systems -For more information on installing Node.js on a variety of operating systems, see [this page][pkg-mgr]. - +For more information on installing Node.js on a variety of operating +systems, see [this page][pkg-mgr]. [pkg-mgr]: https://nodejs.org/en/download/package-manager/ diff --git a/docs/content/configuring-npm/shrinkwrap-json.md b/docs/content/configuring-npm/npm-shrinkwrap-json.md similarity index 61% rename from docs/content/configuring-npm/shrinkwrap-json.md rename to docs/content/configuring-npm/npm-shrinkwrap-json.md index 7fd5baa67fb83..ab0a241079380 100644 --- a/docs/content/configuring-npm/shrinkwrap-json.md +++ b/docs/content/configuring-npm/npm-shrinkwrap-json.md @@ -1,26 +1,30 @@ --- -title: shrinkwrap.json +title: npm-shrinkwrap.json section: 5 description: A publishable lockfile --- ### Description -`npm-shrinkwrap.json` is a file created by [`npm shrinkwrap`](/commands/npm-shrinkwrap). It is identical to +`npm-shrinkwrap.json` is a file created by [`npm +shrinkwrap`](/commands/npm-shrinkwrap). It is identical to `package-lock.json`, with one major caveat: Unlike `package-lock.json`, `npm-shrinkwrap.json` may be included when publishing a package. The recommended use-case for `npm-shrinkwrap.json` is applications deployed through the publishing process on the registry: for example, daemons and command-line tools intended as global installs or `devDependencies`. It's -strongly discouraged for library authors to publish this file, since that would -prevent end users from having control over transitive dependency updates. +strongly discouraged for library authors to publish this file, since that +would prevent end users from having control over transitive dependency +updates. -Additionally, if both `package-lock.json` and `npm-shrinkwrap.json` are present -in a package root, `package-lock.json` will be ignored in favor of this file. +If both `package-lock.json` and `npm-shrinkwrap.json` are present in a +package root, `npm-shrinkwrap.json` will be preferred over the +`package-lock.json` file. -For full details and description of the `npm-shrinkwrap.json` file format, refer -to the manual page for [package-lock.json](/configuring-npm/package-lock-json). +For full details and description of the `npm-shrinkwrap.json` file format, +refer to the manual page for +[package-lock.json](/configuring-npm/package-lock-json). ### See also diff --git a/docs/content/configuring-npm/npmrc.md b/docs/content/configuring-npm/npmrc.md index f0edfda61c9a8..83310ffa9c7f2 100644 --- a/docs/content/configuring-npm/npmrc.md +++ b/docs/content/configuring-npm/npmrc.md @@ -6,13 +6,14 @@ description: The npm config files ### Description -npm gets its config settings from the command line, environment -variables, and `npmrc` files. +npm gets its config settings from the command line, environment variables, +and `npmrc` files. -The `npm config` command can be used to update and edit the contents -of the user and global npmrc files. +The `npm config` command can be used to update and edit the contents of the +user and global npmrc files. -For a list of available configuration options, see [config](/using-npm/config). +For a list of available configuration options, see +[config](/using-npm/config). ### Files @@ -23,20 +24,19 @@ The four relevant files are: * global config file ($PREFIX/etc/npmrc) * npm builtin config file (/path/to/npm/npmrc) -All npm config files are an ini-formatted list of `key = value` -parameters. Environment variables can be replaced using -`${VARIABLE_NAME}`. For example: +All npm config files are an ini-formatted list of `key = value` parameters. +Environment variables can be replaced using `${VARIABLE_NAME}`. For +example: ```bash prefix = ${HOME}/.npm-packages ``` -Each of these files is loaded, and config options are resolved in -priority order. For example, a setting in the userconfig file would -override the setting in the globalconfig file. +Each of these files is loaded, and config options are resolved in priority +order. For example, a setting in the userconfig file would override the +setting in the globalconfig file. -Array values are specified by adding "[]" after the key name. For -example: +Array values are specified by adding "[]" after the key name. For example: ```bash key[] = "first value" @@ -45,7 +45,9 @@ key[] = "second value" #### Comments -Lines in `.npmrc` files are interpreted as comments when they begin with a `;` or `#` character. `.npmrc` files are parsed by [npm/ini](https://github.com/npm/ini), which specifies this comment syntax. +Lines in `.npmrc` files are interpreted as comments when they begin with a +`;` or `#` character. `.npmrc` files are parsed by +[npm/ini](https://github.com/npm/ini), which specifies this comment syntax. For example: @@ -61,24 +63,24 @@ When working locally in a project, a `.npmrc` file in the root of the project (ie, a sibling of `node_modules` and `package.json`) will set config values specific to this project. -Note that this only applies to the root of the project that you're -running npm in. It has no effect when your module is published. For -example, you can't publish a module that forces itself to install -globally, or in a different location. +Note that this only applies to the root of the project that you're running +npm in. It has no effect when your module is published. For example, you +can't publish a module that forces itself to install globally, or in a +different location. Additionally, this file is not read in global mode, such as when running `npm install -g`. #### Per-user config file -`$HOME/.npmrc` (or the `userconfig` param, if set in the environment -or on the command line) +`$HOME/.npmrc` (or the `userconfig` param, if set in the environment or on +the command line) #### Global config file -`$PREFIX/etc/npmrc` (or the `globalconfig` param, if set above): -This file is an ini-file formatted list of `key = value` parameters. -Environment variables can be replaced as above. +`$PREFIX/etc/npmrc` (or the `globalconfig` param, if set above): This file +is an ini-file formatted list of `key = value` parameters. Environment +variables can be replaced as above. #### Built-in config file @@ -86,9 +88,8 @@ Environment variables can be replaced as above. This is an unchangeable "builtin" configuration file that npm keeps consistent across updates. Set fields in here using the `./configure` -script that comes with npm. This is primarily for distribution -maintainers to override default configs in a standard and consistent -manner. +script that comes with npm. This is primarily for distribution maintainers +to override default configs in a standard and consistent manner. ### See also diff --git a/docs/content/configuring-npm/package-json.md b/docs/content/configuring-npm/package-json.md index 8b9b4ffbb1074..caa1e16a32678 100644 --- a/docs/content/configuring-npm/package-json.md +++ b/docs/content/configuring-npm/package-json.md @@ -6,8 +6,9 @@ description: Specifics of npm's package.json handling ### Description -This document is all you need to know about what's required in your package.json -file. It must be actual JSON, not just a JavaScript object literal. +This document is all you need to know about what's required in your +package.json file. It must be actual JSON, not just a JavaScript object +literal. A lot of the behavior described in this document is affected by the config settings described in [`config`](/using-npm/config). @@ -15,32 +16,36 @@ settings described in [`config`](/using-npm/config). ### name If you plan to publish your package, the *most* important things in your -package.json are the name and version fields as they will be required. The name -and version together form an identifier that is assumed to be completely unique. -Changes to the package should come along with changes to the version. If you don't -plan to publish your package, the name and version fields are optional. +package.json are the name and version fields as they will be required. The +name and version together form an identifier that is assumed to be +completely unique. Changes to the package should come along with changes +to the version. If you don't plan to publish your package, the name and +version fields are optional. The name is what your thing is called. Some rules: -* The name must be less than or equal to 214 characters. This includes the scope for - scoped packages. -* The names of scoped packages can begin with a dot or an underscore. This is not permitted without a scope. +* The name must be less than or equal to 214 characters. This includes the + scope for scoped packages. +* The names of scoped packages can begin with a dot or an underscore. This + is not permitted without a scope. * New packages must not have uppercase letters in the name. -* The name ends up being part of a URL, an argument on the command line, and a - folder name. Therefore, the name can't contain any non-URL-safe characters. +* The name ends up being part of a URL, an argument on the command line, + and a folder name. Therefore, the name can't contain any non-URL-safe + characters. Some tips: * Don't use the same name as a core Node module. -* Don't put "js" or "node" in the name. It's assumed that it's js, since you're - writing a package.json file, and you can specify the engine using the "engines" - field. (See below.) -* The name will probably be passed as an argument to require(), so it should - be something short, but also reasonably descriptive. -* You may want to check the npm registry to see if there's something by that name - already, before you get too attached to it. +* Don't put "js" or "node" in the name. It's assumed that it's js, since + you're writing a package.json file, and you can specify the engine using + the "engines" field. (See below.) +* The name will probably be passed as an argument to require(), so it + should be something short, but also reasonably descriptive. +* You may want to check the npm registry to see if there's something by + that name already, before you get too attached to it. + A name can be optionally prefixed by a scope, e.g. `@myorg/mypackage`. See [`scope`](/using-npm/scope) for more detail. @@ -48,14 +53,15 @@ A name can be optionally prefixed by a scope, e.g. `@myorg/mypackage`. See ### version If you plan to publish your package, the *most* important things in your -package.json are the name and version fields as they will be required. The name -and version together form an identifier that is assumed to be completely unique. -Changes to the package should come along with changes to the version. If you don't -plan to publish your package, the name and version fields are optional. +package.json are the name and version fields as they will be required. The +name and version together form an identifier that is assumed to be +completely unique. Changes to the package should come along with changes +to the version. If you don't plan to publish your package, the name and +version fields are optional. Version must be parseable by -[node-semver](https://github.com/npm/node-semver), which is bundled -with npm as a dependency. (`npm install semver` to use it yourself.) +[node-semver](https://github.com/npm/node-semver), which is bundled with +npm as a dependency. (`npm install semver` to use it yourself.) More on version numbers and ranges at [semver](/using-npm/semver). @@ -66,8 +72,8 @@ package, as it's listed in `npm search`. ### keywords -Put keywords in it. It's an array of strings. This helps people -discover your package as it's listed in `npm search`. +Put keywords in it. It's an array of strings. This helps people discover +your package as it's listed in `npm search`. ### homepage @@ -82,71 +88,83 @@ Example: ### bugs The url to your project's issue tracker and / or the email address to which -issues should be reported. These are helpful for people who encounter issues -with your package. +issues should be reported. These are helpful for people who encounter +issues with your package. It should look like this: ```json -{ "url" : "https://github.com/owner/project/issues" -, "email" : "project@hostname.com" +{ + "url" : "https://github.com/owner/project/issues", + "email" : "project@hostname.com" } ``` -You can specify either one or both values. If you want to provide only a url, -you can specify the value for "bugs" as a simple string instead of an object. +You can specify either one or both values. If you want to provide only a +url, you can specify the value for "bugs" as a simple string instead of an +object. If a url is provided, it will be used by the `npm bugs` command. ### license -You should specify a license for your package so that people know how they are -permitted to use it, and any restrictions you're placing on it. +You should specify a license for your package so that people know how they +are permitted to use it, and any restrictions you're placing on it. -If you're using a common license such as BSD-2-Clause or MIT, add a -current SPDX license identifier for the license you're using, like this: +If you're using a common license such as BSD-2-Clause or MIT, add a current +SPDX license identifier for the license you're using, like this: ```json -{ "license" : "BSD-3-Clause" } +{ + "license" : "BSD-3-Clause" +} ``` -You can check [the full list of SPDX license IDs](https://spdx.org/licenses/). -Ideally you should pick one that is +You can check [the full list of SPDX license +IDs](https://spdx.org/licenses/). Ideally you should pick one that is [OSI](https://opensource.org/licenses/alphabetical) approved. -If your package is licensed under multiple common licenses, use an [SPDX license -expression syntax version 2.0 string](https://www.npmjs.com/package/spdx), like this: +If your package is licensed under multiple common licenses, use an [SPDX +license expression syntax version 2.0 +string](https://www.npmjs.com/package/spdx), like this: ```json -{ "license" : "(ISC OR GPL-3.0)" } +{ + "license" : "(ISC OR GPL-3.0)" +} ``` If you are using a license that hasn't been assigned an SPDX identifier, or if you are using a custom license, use a string value like this one: ```json -{ "license" : "SEE LICENSE IN " } +{ + "license" : "SEE LICENSE IN " +} ``` Then include a file named `` at the top level of the package. -Some old packages used license objects or a "licenses" property containing an -array of license objects: +Some old packages used license objects or a "licenses" property containing +an array of license objects: ```json // Not valid metadata -{ "license" : - { "type" : "ISC" - , "url" : "https://opensource.org/licenses/ISC" +{ + "license" : { + "type" : "ISC", + "url" : "https://opensource.org/licenses/ISC" } } // Not valid metadata -{ "licenses" : - [ - { "type": "MIT" - , "url": "https://www.opensource.org/licenses/mit-license.php" - } - , { "type": "Apache-2.0" - , "url": "https://opensource.org/licenses/apache2.0.php" +{ + "licenses" : [ + { + "type": "MIT", + "url": "https://www.opensource.org/licenses/mit-license.php" + }, + { + "type": "Apache-2.0", + "url": "https://opensource.org/licenses/apache2.0.php" } ] } @@ -155,35 +173,49 @@ array of license objects: Those styles are now deprecated. Instead, use SPDX expressions, like this: ```json -{ "license": "ISC" } +{ + "license": "ISC" +} +``` -{ "license": "(MIT OR Apache-2.0)" } +```json +{ + "license": "(MIT OR Apache-2.0)" +} ``` Finally, if you do not wish to grant others the right to use a private or unpublished package under any terms: ```json -{ "license": "UNLICENSED" } +{ + "license": "UNLICENSED" +} ``` + Consider also setting `"private": true` to prevent accidental publication. ### people fields: author, contributors -The "author" is one person. "contributors" is an array of people. A "person" -is an object with a "name" field and optionally "url" and "email", like this: +The "author" is one person. "contributors" is an array of people. A +"person" is an object with a "name" field and optionally "url" and "email", +like this: ```json -{ "name" : "Barney Rubble" -, "email" : "b@rubble.com" -, "url" : "http://barnyrubble.tumblr.com/" +{ + "name" : "Barney Rubble", + "email" : "b@rubble.com", + "url" : "http://barnyrubble.tumblr.com/" } ``` -Or you can shorten that all into a single string, and npm will parse it for you: +Or you can shorten that all into a single string, and npm will parse it for +you: ```json -"Barney Rubble (http://barnyrubble.tumblr.com/)" +{ + "author": "Barney Rubble (http://barnyrubble.tumblr.com/)" +} ``` Both email and url are optional either way. @@ -193,58 +225,61 @@ npm also sets a top-level "maintainers" field with your npm user info. ### funding You can specify an object containing an URL that provides up-to-date -information about ways to help fund development of your package, or -a string URL, or an array of these: +information about ways to help fund development of your package, or a +string URL, or an array of these: - "funding": { +```json +{ + "funding": { + "type" : "individual", + "url" : "http://example.com/donate" + }, + + "funding": { + "type" : "patreon", + "url" : "https://www.patreon.com/my-account" + }, + + "funding": "http://example.com/donate", + + "funding": [ + { "type" : "individual", "url" : "http://example.com/donate" - } - - "funding": { + }, + "http://example.com/donateAlso", + { "type" : "patreon", "url" : "https://www.patreon.com/my-account" } - - "funding": "http://example.com/donate" - - "funding": [ - { - "type" : "individual", - "url" : "http://example.com/donate" - }, - "http://example.com/donateAlso", - { - "type" : "patreon", - "url" : "https://www.patreon.com/my-account" - } - ] - + ] +} +``` Users can use the `npm fund` subcommand to list the `funding` URLs of all -dependencies of their project, direct and indirect. A shortcut to visit each -funding url is also available when providing the project name such as: -`npm fund ` (when there are multiple URLs, the first one will be -visited) +dependencies of their project, direct and indirect. A shortcut to visit +each funding url is also available when providing the project name such as: +`npm fund ` (when there are multiple URLs, the first one will +be visited) ### files -The optional `files` field is an array of file patterns that describes -the entries to be included when your package is installed as a -dependency. File patterns follow a similar syntax to `.gitignore`, but -reversed: including a file, directory, or glob pattern (`*`, `**/*`, and such) -will make it so that file is included in the tarball when it's packed. Omitting -the field will make it default to `["*"]`, which means it will include all files. +The optional `files` field is an array of file patterns that describes the +entries to be included when your package is installed as a dependency. File +patterns follow a similar syntax to `.gitignore`, but reversed: including a +file, directory, or glob pattern (`*`, `**/*`, and such) will make it so +that file is included in the tarball when it's packed. Omitting the field +will make it default to `["*"]`, which means it will include all files. -Some special files and directories are also included or excluded regardless of -whether they exist in the `files` array (see below). +Some special files and directories are also included or excluded regardless +of whether they exist in the `files` array (see below). -You can also provide a `.npmignore` file in the root of your package or -in subdirectories, which will keep files from being included. At the -root of your package it will not override the "files" field, but in -subdirectories it will. The `.npmignore` file works just like a -`.gitignore`. If there is a `.gitignore` file, and `.npmignore` is -missing, `.gitignore`'s contents will be used instead. +You can also provide a `.npmignore` file in the root of your package or in +subdirectories, which will keep files from being included. At the root of +your package it will not override the "files" field, but in subdirectories +it will. The `.npmignore` file works just like a `.gitignore`. If there is +a `.gitignore` file, and `.npmignore` is missing, `.gitignore`'s contents +will be used instead. Files included with the "package.json#files" field _cannot_ be excluded through `.npmignore` or `.gitignore`. @@ -276,24 +311,28 @@ Conversely, some files are always ignored: * `node_modules` * `config.gypi` * `*.orig` -* `package-lock.json` (use shrinkwrap instead) +* `package-lock.json` (use + [`npm-shrinkwrap.json`](/configuring-npm/npm-shrinkwrap-json) if you wish + it to be published) ### main -The main field is a module ID that is the primary entry point to your program. -That is, if your package is named `foo`, and a user installs it, and then does -`require("foo")`, then your main module's exports object will be returned. +The main field is a module ID that is the primary entry point to your +program. That is, if your package is named `foo`, and a user installs it, +and then does `require("foo")`, then your main module's exports object will +be returned. -This should be a module ID relative to the root of your package folder. +This should be a module relative to the root of your package folder. -For most modules, it makes the most sense to have a main script and often not -much else. +For most modules, it makes the most sense to have a main script and often +not much else. ### browser If your module is meant to be used client-side the browser field should be used instead of the main field. This is helpful to hint users that it might -rely on primitives that aren't available in Node.js modules. (e.g. `window`) +rely on primitives that aren't available in Node.js modules. (e.g. +`window`) ### bin @@ -302,35 +341,45 @@ install into the PATH. npm makes this pretty easy (in fact, it uses this feature to install the "npm" executable.) To use this, supply a `bin` field in your package.json which is a map of -command name to local file name. On install, npm will symlink that file into -`prefix/bin` for global installs, or `./node_modules/.bin/` for local +command name to local file name. On install, npm will symlink that file +into `prefix/bin` for global installs, or `./node_modules/.bin/` for local installs. For example, myapp could have this: ```json -{ "bin" : { "myapp" : "./cli.js" } } +{ + "bin": { + "myapp": "./cli.js" + } +} ``` -So, when you install myapp, it'll create a symlink from the `cli.js` script to -`/usr/local/bin/myapp`. +So, when you install myapp, it'll create a symlink from the `cli.js` script +to `/usr/local/bin/myapp`. -If you have a single executable, and its name should be the name -of the package, then you can just supply it as a string. For example: +If you have a single executable, and its name should be the name of the +package, then you can just supply it as a string. For example: ```json -{ "name": "my-program" -, "version": "1.2.5" -, "bin": "./path/to/program" } +{ + "name": "my-program", + "version": "1.2.5", + "bin": "./path/to/program" +} ``` would be the same as this: ```json -{ "name": "my-program" -, "version": "1.2.5" -, "bin" : { "my-program" : "./path/to/program" } } +{ + "name": "my-program", + "version": "1.2.5", + "bin": { + "my-program": "./path/to/program" + } +} ``` Please make sure that your file(s) referenced in `bin` starts with @@ -339,93 +388,88 @@ executable! ### man -Specify either a single file or an array of filenames to put in place for the -`man` program to find. +Specify either a single file or an array of filenames to put in place for +the `man` program to find. If only a single file is provided, then it's installed such that it is the -result from `man `, regardless of its actual filename. For example: +result from `man `, regardless of its actual filename. For +example: ```json -{ "name" : "foo" -, "version" : "1.2.3" -, "description" : "A packaged foo fooer for fooing foos" -, "main" : "foo.js" -, "man" : "./man/doc.1" +{ + "name": "foo", + "version": "1.2.3", + "description": "A packaged foo fooer for fooing foos", + "main": "foo.js", + "man": "./man/doc.1" } ``` -would link the `./man/doc.1` file in such that it is the target for `man foo` +would link the `./man/doc.1` file in such that it is the target for `man +foo` If the filename doesn't start with the package name, then it's prefixed. So, this: ```json -{ "name" : "foo" -, "version" : "1.2.3" -, "description" : "A packaged foo fooer for fooing foos" -, "main" : "foo.js" -, "man" : [ "./man/foo.1", "./man/bar.1" ] +{ + "name": "foo", + "version": "1.2.3", + "description": "A packaged foo fooer for fooing foos", + "main": "foo.js", + "man": [ + "./man/foo.1", + "./man/bar.1" + ] } ``` will create files to do `man foo` and `man foo-bar`. Man files must end with a number, and optionally a `.gz` suffix if they are -compressed. The number dictates which man section the file is installed into. +compressed. The number dictates which man section the file is installed +into. ```json -{ "name" : "foo" -, "version" : "1.2.3" -, "description" : "A packaged foo fooer for fooing foos" -, "main" : "foo.js" -, "man" : [ "./man/foo.1", "./man/foo.2" ] +{ + "name": "foo", + "version": "1.2.3", + "description": "A packaged foo fooer for fooing foos", + "main": "foo.js", + "man": [ + "./man/foo.1", + "./man/foo.2" + ] } ``` + will create entries for `man foo` and `man 2 foo` ### directories -The CommonJS [Packages](http://wiki.commonjs.org/wiki/Packages/1.0) spec details a -few ways that you can indicate the structure of your package using a `directories` -object. If you look at [npm's package.json](https://registry.npmjs.org/npm/latest), -you'll see that it has directories for doc, lib, and man. +The CommonJS [Packages](http://wiki.commonjs.org/wiki/Packages/1.0) spec +details a few ways that you can indicate the structure of your package +using a `directories` object. If you look at [npm's +package.json](https://registry.npmjs.org/npm/latest), you'll see that it +has directories for doc, lib, and man. In the future, this information may be used in other creative ways. -#### directories.lib - -Tell people where the bulk of your library is. Nothing special is done -with the lib folder in any way, but it's useful meta info. - #### directories.bin If you specify a `bin` directory in `directories.bin`, all the files in that folder will be added. -Because of the way the `bin` directive works, specifying both a -`bin` path and setting `directories.bin` is an error. If you want to -specify individual files, use `bin`, and for all the files in an -existing `bin` directory, use `directories.bin`. +Because of the way the `bin` directive works, specifying both a `bin` path +and setting `directories.bin` is an error. If you want to specify +individual files, use `bin`, and for all the files in an existing `bin` +directory, use `directories.bin`. #### directories.man A folder that is full of man pages. Sugar to generate a "man" array by walking the folder. -#### directories.doc - -Put markdown files in here. Eventually, these will be displayed nicely, -maybe, someday. - -#### directories.example - -Put example scripts in here. Someday, it might be exposed in some clever way. - -#### directories.test - -Put your tests in here. It is currently not exposed, but it might be in the -future. - ### repository Specify the place where your code lives. This is helpful for people who @@ -435,72 +479,80 @@ command will be able to find you. Do it like this: ```json -"repository": { - "type" : "git", - "url" : "https://github.com/npm/cli.git" -} - -"repository": { - "type" : "svn", - "url" : "https://v8.googlecode.com/svn/trunk/" +{ + "repository": { + "type": "git", + "url": "https://github.com/npm/cli.git" + } } ``` -The URL should be a publicly available (perhaps read-only) url that can be handed -directly to a VCS program without any modification. It should not be a url to an -html project page that you put in your browser. It's for computers. +The URL should be a publicly available (perhaps read-only) url that can be +handed directly to a VCS program without any modification. It should not +be a url to an html project page that you put in your browser. It's for +computers. -For GitHub, GitHub gist, Bitbucket, or GitLab repositories you can use the same -shortcut syntax you use for `npm install`: +For GitHub, GitHub gist, Bitbucket, or GitLab repositories you can use the +same shortcut syntax you use for `npm install`: ```json -"repository": "npm/npm" +{ + "repository": "npm/npm", -"repository": "github:user/repo" + "repository": "github:user/repo", -"repository": "gist:11081aaa281" + "repository": "gist:11081aaa281", -"repository": "bitbucket:user/repo" + "repository": "bitbucket:user/repo", -"repository": "gitlab:user/repo" + "repository": "gitlab:user/repo" +} ``` -If the `package.json` for your package is not in the root directory (for example -if it is part of a monorepo), you can specify the directory in which it lives: +If the `package.json` for your package is not in the root directory (for +example if it is part of a monorepo), you can specify the directory in +which it lives: ```json -"repository": { - "type" : "git", - "url" : "https://github.com/facebook/react.git", - "directory": "packages/react-dom" +{ + "repository": { + "type": "git", + "url": "https://github.com/facebook/react.git", + "directory": "packages/react-dom" + } } ``` ### scripts -The "scripts" property is a dictionary containing script commands that are run -at various times in the lifecycle of your package. The key is the lifecycle -event, and the value is the command to run at that point. +The "scripts" property is a dictionary containing script commands that are +run at various times in the lifecycle of your package. The key is the +lifecycle event, and the value is the command to run at that point. -See [`scripts`](/using-npm/scripts) to find out more about writing package scripts. +See [`scripts`](/using-npm/scripts) to find out more about writing package +scripts. ### config -A "config" object can be used to set configuration parameters used in package -scripts that persist across upgrades. For instance, if a package had the -following: +A "config" object can be used to set configuration parameters used in +package scripts that persist across upgrades. For instance, if a package +had the following: ```json -{ "name" : "foo" -, "config" : { "port" : "8080" } } +{ + "name": "foo", + "config": { + "port": "8080" + } +} ``` and then had a "start" command that then referenced the `npm_package_config_port` environment variable, then the user could override that by doing `npm config set foo:port 8001`. -See [`config`](/using-npm/config) and [`scripts`](/using-npm/scripts) for more on package -configs. +See [`config`](/using-npm/config) and [`scripts`](/using-npm/scripts) for +more on package configs. ### dependencies @@ -509,17 +561,19 @@ version range. The version range is a string which has one or more space-separated descriptors. Dependencies can also be identified with a tarball or git URL. -**Please do not put test harnesses or transpilers in your -`dependencies` object.** See `devDependencies`, below. +**Please do not put test harnesses or transpilers or other "development" +time tools in your `dependencies` object.** See `devDependencies`, below. -See [semver](/using-npm/semver) for more details about specifying version ranges. +See [semver](/using-npm/semver) for more details about specifying version +ranges. * `version` Must match `version` exactly * `>version` Must be greater than `version` * `>=version` etc * `=1.0.2 <2.1.2" - , "baz" : ">1.0.2 <=2.3.4" - , "boo" : "2.0.1" - , "qux" : "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0" - , "asd" : "http://asdf.com/asdf.tar.gz" - , "til" : "~1.2" - , "elf" : "~1.2.3" - , "two" : "2.x" - , "thr" : "3.3.x" - , "lat" : "latest" - , "dyl" : "file:../dyl" +{ + "dependencies": { + "foo": "1.0.0 - 2.9999.9999", + "bar": ">=1.0.2 <2.1.2", + "baz": ">1.0.2 <=2.3.4", + "boo": "2.0.1", + "qux": "<1.0.0 || >=2.3.1 <2.4.5 || >=2.5.2 <3.0.0", + "asd": "http://asdf.com/asdf.tar.gz", + "til": "~1.2", + "elf": "~1.2.3", + "two": "2.x", + "thr": "3.3.x", + "lat": "latest", + "dyl": "file:../dyl" } } ``` @@ -573,8 +629,8 @@ Git urls are of the form: If `#` is provided, it will be used to clone exactly that commit. If the commit-ish has the format `#semver:`, `` can be any valid semver range or exact version, and npm will look for any tags -or refs matching that range in the remote repository, much as it would for a -registry dependency. If neither `#` or `#semver:` is +or refs matching that range in the remote repository, much as it would for +a registry dependency. If neither `#` or `#semver:` is specified, then `master` is used. Examples: @@ -606,9 +662,9 @@ included. For example: #### Local Paths -As of version 2.0.0 you can provide a path to a local directory that contains a -package. Local paths can be saved using `npm install -S` or -`npm install --save`, using any of these forms: +As of version 2.0.0 you can provide a path to a local directory that +contains a package. Local paths can be saved using `npm install -S` or `npm +install --save`, using any of these forms: ```bash ../foo/bar @@ -629,32 +685,32 @@ in which case they will be normalized to a relative path and added to your } ``` -This feature is helpful for local offline development and creating -tests that require npm installing where you don't want to hit an -external server, but should not be used when publishing packages -to the public registry. +This feature is helpful for local offline development and creating tests +that require npm installing where you don't want to hit an external server, +but should not be used when publishing packages to the public registry. ### devDependencies If someone is planning on downloading and using your module in their -program, then they probably don't want or need to download and build -the external test or documentation framework that you use. +program, then they probably don't want or need to download and build the +external test or documentation framework that you use. -In this case, it's best to map these additional items in a `devDependencies` -object. +In this case, it's best to map these additional items in a +`devDependencies` object. -These things will be installed when doing `npm link` or `npm install` -from the root of a package, and can be managed like any other npm -configuration param. See [`config`](/using-npm/config) for more on the topic. +These things will be installed when doing `npm link` or `npm install` from +the root of a package, and can be managed like any other npm configuration +param. See [`config`](/using-npm/config) for more on the topic. For build steps that are not platform-specific, such as compiling -CoffeeScript or other languages to JavaScript, use the `prepare` -script to do this, and make the required package a devDependency. +CoffeeScript or other languages to JavaScript, use the `prepare` script to +do this, and make the required package a devDependency. For example: ```json -{ "name": "ethopia-waza", +{ + "name": "ethopia-waza", "description": "a delightfully fruity coffee varietal", "version": "1.2.3", "devDependencies": { @@ -667,17 +723,18 @@ For example: } ``` -The `prepare` script will be run before publishing, so that users -can consume the functionality without requiring them to compile it -themselves. In dev mode (ie, locally running `npm install`), it'll -run this script as well, so that you can test it easily. +The `prepare` script will be run before publishing, so that users can +consume the functionality without requiring them to compile it themselves. +In dev mode (ie, locally running `npm install`), it'll run this script as +well, so that you can test it easily. ### peerDependencies In some cases, you want to express the compatibility of your package with a host tool or library, while not necessarily doing a `require` of this host. -This is usually referred to as a *plugin*. Notably, your module may be exposing -a specific interface, expected and specified by the host documentation. +This is usually referred to as a *plugin*. Notably, your module may be +exposing a specific interface, expected and specified by the host +documentation. For example: @@ -691,34 +748,38 @@ For example: } ``` -This ensures your package `tea-latte` can be installed *along* with the second -major version of the host package `tea` only. `npm install tea-latte` could -possibly yield the following dependency graph: +This ensures your package `tea-latte` can be installed *along* with the +second major version of the host package `tea` only. `npm install +tea-latte` could possibly yield the following dependency graph: ```bash ├── tea-latte@1.3.5 └── tea@2.2.0 ``` -**NOTE: npm versions 1 and 2 will automatically install `peerDependencies` if -they are not explicitly depended upon higher in the dependency tree. In the -next major version of npm (npm@3), this will no longer be the case. You will -receive a warning that the peerDependency is not installed instead.** The -behavior in npms 1 & 2 was frequently confusing and could easily put you into -dependency hell, a situation that npm is designed to avoid as much as possible. +In npm versions 3 through 6, `peerDependencies` were not automatically +installed, and would raise a warning if an invalid version of the peer +dependency was found in the tree. As of npm v7, peerDependencies _are_ +installed by default. -Trying to install another plugin with a conflicting requirement will cause an -error. For this reason, make sure your plugin requirement is as broad as -possible, and not to lock it down to specific patch versions. +Trying to install another plugin with a conflicting requirement may cause +an error if the tree cannot be resolved correctly. For this reason, make +sure your plugin requirement is as broad as possible, and not to lock it +down to specific patch versions. -Assuming the host complies with [semver](https://semver.org/), only changes in -the host package's major version will break your plugin. Thus, if you've worked -with every 1.x version of the host package, use `"^1.0"` or `"1.x"` to express -this. If you depend on features introduced in 1.5.2, use `">= 1.5.2 < 2"`. +Assuming the host complies with [semver](https://semver.org/), only changes +in the host package's major version will break your plugin. Thus, if you've +worked with every 1.x version of the host package, use `"^1.0"` or `"1.x"` +to express this. If you depend on features introduced in 1.5.2, use +`"^1.5.2"`. ### peerDependenciesMeta -When a user installs your package, npm will emit warnings if packages specified in `peerDependencies` are not already installed. The `peerDependenciesMeta` field serves to provide npm more information on how your peer dependencies are to be used. Specifically, it allows peer dependencies to be marked as optional. +When a user installs your package, npm will emit warnings if packages +specified in `peerDependencies` are not already installed. The +`peerDependenciesMeta` field serves to provide npm more information on how +your peer dependencies are to be used. Specifically, it allows peer +dependencies to be marked as optional. For example: @@ -738,7 +799,10 @@ For example: } ``` -Marking a peer dependency as optional ensures npm will not emit a warning if the `soy-milk` package is not installed on the host. This allows you to integrate and interact with a variety of host packages without requiring all of them to be installed. +Marking a peer dependency as optional ensures npm will not emit a warning +if the `soy-milk` package is not installed on the host. This allows you to +integrate and interact with a variety of host packages without requiring +all of them to be installed. ### bundledDependencies @@ -759,26 +823,28 @@ If we define a package.json like this: "name": "awesome-web-framework", "version": "1.0.0", "bundledDependencies": [ - "renderized", "super-streams" + "renderized", + "super-streams" ] } ``` + we can obtain `awesome-web-framework-1.0.0.tgz` file by running `npm pack`. This file contains the dependencies `renderized` and `super-streams` which can be installed in a new project by executing `npm install -awesome-web-framework-1.0.0.tgz`. Note that the package names do not include -any versions, as that information is specified in `dependencies`. +awesome-web-framework-1.0.0.tgz`. Note that the package names do not +include any versions, as that information is specified in `dependencies`. If this is spelled `"bundleDependencies"`, then that is also honored. ### optionalDependencies -If a dependency can be used, but you would like npm to proceed if it cannot be -found or fails to install, then you may put it in the `optionalDependencies` -object. This is a map of package name to version or url, just like the -`dependencies` object. The difference is that build failures do not cause -installation to fail. Running `npm install --no-optional` will prevent these -dependencies from being installed. +If a dependency can be used, but you would like npm to proceed if it cannot +be found or fails to install, then you may put it in the +`optionalDependencies` object. This is a map of package name to version or +url, just like the `dependencies` object. The difference is that build +failures do not cause installation to fail. Running `npm install +--no-optional` will prevent these dependencies from being installed. It is still your program's responsibility to handle the lack of the dependency. For example, something like this: @@ -809,32 +875,30 @@ Entries in `optionalDependencies` will override entries of the same name in You can specify the version of node that your stuff works on: ```json -{ "engines" : { "node" : ">=0.10.3 <0.12" } } +{ + "engines": { + "node": ">=0.10.3 <15" + } +} ``` And, like with dependencies, if you don't specify the version (or if you specify "\*" as the version), then any version of node will do. -If you specify an "engines" field, then npm will require that "node" be -somewhere on that list. If "engines" is omitted, then npm will just assume -that it works on node. - -You can also use the "engines" field to specify which versions of npm -are capable of properly installing your program. For example: +You can also use the "engines" field to specify which versions of npm are +capable of properly installing your program. For example: ```json -{ "engines" : { "npm" : "~1.0.20" } } +{ + "engines": { + "npm": "~1.0.20" + } +} ``` -Unless the user has set the `engine-strict` config flag, this -field is advisory only and will only produce warnings when your package is installed as a dependency. - -### engineStrict - -**This feature was removed in npm 3.0.0** - -Prior to npm 3.0.0, this feature was used to treat this package as if the -user had set `engine-strict`. It is no longer used. +Unless the user has set the `engine-strict` config flag, this field is +advisory only and will only produce warnings when your package is installed +as a dependency. ### os @@ -842,14 +906,23 @@ You can specify which operating systems your module will run on: ```json -"os" : [ "darwin", "linux" ] +{ + "os": [ + "darwin", + "linux" + ] +} ``` -You can also block instead of allowing operating systems, -just prepend the blocked os with a '!': +You can also block instead of allowing operating systems, just prepend the +blocked os with a '!': ```json -"os" : [ "!win32" ] +{ + "os": [ + "!win32" + ] +} ``` The host operating system is determined by `process.platform` @@ -863,62 +936,62 @@ If your code only runs on certain cpu architectures, you can specify which ones. ```json -"cpu" : [ "x64", "ia32" ] +{ + "cpu": [ + "x64", + "ia32" + ] +} ``` Like the `os` option, you can also block architectures: ```json -"cpu" : [ "!arm", "!mips" ] +{ + "cpu": [ + "!arm", + "!mips" + ] +} ``` The host architecture is determined by `process.arch` -### preferGlobal - -**DEPRECATED** - -This option used to trigger an npm warning, but it will no longer warn. It is -purely there for informational purposes. It is now recommended that you install -any binaries as local devDependencies wherever possible. - ### private -If you set `"private": true` in your package.json, then npm will refuse -to publish it. +If you set `"private": true` in your package.json, then npm will refuse to +publish it. -This is a way to prevent accidental publication of private repositories. If -you would like to ensure that a given package is only ever published to a -specific registry (for example, an internal registry), then use the -`publishConfig` dictionary described below to override the `registry` config -param at publish-time. +This is a way to prevent accidental publication of private repositories. +If you would like to ensure that a given package is only ever published to +a specific registry (for example, an internal registry), then use the +`publishConfig` dictionary described below to override the `registry` +config param at publish-time. ### publishConfig This is a set of config values that will be used at publish-time. It's especially handy if you want to set the tag, registry or access, so that you can ensure that a given package is not tagged with "latest", published -to the global public registry or that a scoped module is private by default. - -Any config values can be overridden, but only "tag", "registry" and "access" -probably matter for the purposes of publishing. +to the global public registry or that a scoped module is private by +default. -See [`config`](/using-npm/config) to see the list of config options that can be -overridden. +See [`config`](/using-npm/config) to see the list of config options that +can be overridden. ### workspaces The optional `workspaces` field is an array of file patterns that describes -locations within the local file system that the install client should look up -to find each [workspace](/using-npm/workspaces) that needs to be symlinked to -the top level `node_modules` folder. +locations within the local file system that the install client should look +up to find each [workspace](/using-npm/workspaces) that needs to be +symlinked to the top level `node_modules` folder. It can describe either the direct paths of the folders to be used as workspaces or it can define globs that will resolve to these same folders. -In the following example, all folders located inside the folder `./packages` -will be treated as workspaces as long as they have valid `package.json` files -inside them: +In the following example, all folders located inside the folder +`./packages` will be treated as workspaces as long as they have valid +`package.json` files inside them: ```json { @@ -937,20 +1010,20 @@ npm will default some values based on package contents. * `"scripts": {"start": "node server.js"}` - If there is a `server.js` file in the root of your package, then npm - will default the `start` command to `node server.js`. + If there is a `server.js` file in the root of your package, then npm will + default the `start` command to `node server.js`. * `"scripts":{"install": "node-gyp rebuild"}` - If there is a `binding.gyp` file in the root of your package and you have not defined an `install` or `preinstall` script, npm will - default the `install` command to compile using node-gyp. + If there is a `binding.gyp` file in the root of your package and you have + not defined an `install` or `preinstall` script, npm will default the + `install` command to compile using node-gyp. * `"contributors": [...]` - If there is an `AUTHORS` file in the root of your package, npm will - treat each line as a `Name (url)` format, where email and url - are optional. Lines which start with a `#` or are blank, will be - ignored. + If there is an `AUTHORS` file in the root of your package, npm will treat + each line as a `Name (url)` format, where email and url are + optional. Lines which start with a `#` or are blank, will be ignored. ### SEE ALSO diff --git a/docs/content/configuring-npm/package-lock-json.md b/docs/content/configuring-npm/package-lock-json.md index a3083410f7e2a..4d994bbc8c0a2 100644 --- a/docs/content/configuring-npm/package-lock-json.md +++ b/docs/content/configuring-npm/package-lock-json.md @@ -14,132 +14,223 @@ 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. +* 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. +* 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. +* 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. +* Optimize the installation process by allowing npm to skip repeated + metadata resolutions for previously-installed packages. -One key detail about `package-lock.json` is that it cannot be published, and it -will be ignored if found in any place other than the toplevel package. It shares -a format with [npm-shrinkwrap.json](/configuring-npm/shrinkwrap-json), which is essentially the same file, but -allows publication. This is not recommended unless deploying a CLI tool or -otherwise using the publication process for producing production packages. +* 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. -If both `package-lock.json` and `npm-shrinkwrap.json` are present in the root of -a package, `package-lock.json` will be completely ignored. +### `package-lock.json` vs `npm-shrinkwrap.json` +Both of these files have the same format, and perform similar functions in +the root of a project. -### File Format +The difference is that `package-lock.json` is that it cannot be published, +and it will be ignored if found in any place other than the root project. -#### name +In contrast, [npm-shrinkwrap.json](/configuring-npm/npm-shrinkwrap-json) allows +publication, and defines the dependency tree from the point encountered. +This is not recommended unless deploying a CLI tool or otherwise using the +publication process for producing production packages. -The name of the package this is a package-lock for. This must match what's in -`package.json`. +If 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. -#### version +### Hidden Lockfiles -The version of the package this is a package-lock for. This must match what's in -`package.json`. +In order to avoid processing the `node_modules` folder repeatedly, npm as +of v7 uses a "hidden" lockfile present in +`node_modules/.package-lock.json`. This contains information about the +tree, and is used in lieu of reading the entire `node_modules` hierarchy +provided that the following conditions are met: -#### lockfileVersion +- All package folders it references exist in the `node_modules` hierarchy. +- No package folders exist in the `node_modules` hierarchy that are not + listed in the lockfile. +- The modified time of the file is at least as recent as all of the package + folders it references. -An integer version, starting at `1` with the version number of this document -whose semantics were used when generating this `package-lock.json`. +That is, the hidden lockfile will only be relevant if it was created as +part of the most recent update to the package tree. If another CLI mutates +the tree in any way, this will be detected, and the hidden lockfile will be +ignored. -#### packageIntegrity +Note 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. +For 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. If you +are manually editing files in `node_modules`, it is generally best to +delete the file at `node_modules/.package-lock.json`. -This is a [subresource -integrity](https://w3c.github.io/webappsec/specs/subresourceintegrity/) value -created from the `package.json`. No preprocessing of the `package.json` should -be done. Subresource integrity strings can be produced by modules like -[`ssri`](https://www.npmjs.com/package/ssri). +As the hidden lockfile is ignored by older npm versions, it does not +contain the backwards compatibility affordances present in "normal" +lockfiles. That is, it is `lockfileVersion: 3`, rather than +`lockfileVersion: 2`. -#### preserveSymlinks +### Handling Old Lockfiles -Indicates that the install was done with the environment variable -`NODE_PRESERVE_SYMLINKS` enabled. The installer should insist that the value of -this property match that environment variable. +When 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. -#### dependencies +### File Format -A mapping of package name to dependency object. Dependency objects have the -following properties: +#### `name` -##### version +The name of the package this is a package-lock for. This will match what's +in `package.json`. -This is a specifier that uniquely identifies this package and should be -usable in fetching a new copy of it. +#### `version` -* bundled dependencies: Regardless of source, this is a version number that is purely for informational purposes. -* registry sources: This is a version number. (eg, `1.2.3`) -* git sources: This is a git specifier with resolved committish. (eg, `git+https://example.com/foo/bar#115311855adb0789a0466714ed48a1499ffea97e`) -* http tarball sources: This is the URL of the tarball. (eg, `https://example.com/example-1.3.0.tgz`) -* local tarball sources: This is the file URL of the tarball. (eg `file:///opt/storage/example-1.3.0.tgz`) -* local link sources: This is the file URL of the link. (eg `file:libs/our-module`) +The version of the package this is a package-lock for. This will match +what's in `package.json`. -##### integrity +#### `lockfileVersion` -This is a [Standard Subresource -Integrity](https://w3c.github.io/webappsec/specs/subresourceintegrity/) for this -resource. +An integer version, starting at `1` with the version number of this +document whose semantics were used when generating this +`package-lock.json`. -* For bundled dependencies this is not included, regardless of source. -* For registry sources, this is the `integrity` that the registry provided, or if one wasn't provided the SHA1 in `shasum`. -* For git sources this is the specific commit hash we cloned from. -* For remote tarball sources this is an integrity based on a SHA512 of - the file. -* For local tarball sources: This is an integrity field based on the SHA512 of the file. +Note 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. Lockfiles generated by npm v7 will contain +`lockfileVersion: 2`. -##### resolved +* No version provided: an "ancient" shrinkwrap file from a version of npm + prior to npm v5. +* `1`: The lockfile version used by npm v5 and v6. +* `2`: The lockfile version used by npm v7, which is backwards compatible + to v1 lockfiles. +* `3`: The lockfile version used by npm v7, _without_ backwards + compatibility affordances. This is used for the hidden lockfile at + `node_modules/.package-lock.json`, and will likely be used in a future + version of npm, once support for npm v6 is no longer relevant. -* For bundled dependencies this is not included, regardless of source. -* For registry sources this is path of the tarball relative to the registry - URL. If the tarball URL isn't on the same server as the registry URL then - this is a complete URL. +npm 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. -##### bundled +#### `packages` -If true, this is the bundled dependency and will be installed by the parent -module. When installing, this module will be extracted from the parent -module during the extract phase, not installed as a separate dependency. +This is an object that maps package locations to an object containing the +information about that package. -##### dev +The root project is typically listed with a key of `""`, and all other +packages are listed with their relative paths from the root project folder. -If true then this dependency is either a development dependency ONLY of the -top level module or a transitive dependency of one. 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. +Package descriptors have the following fields: -##### optional +* version: The version found in `package.json` -If true then this dependency is either an optional dependency ONLY of the -top level module or a transitive dependency of one. 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. +* resolved: The place where the package was actually resolved from. In + the case of packages fetched from the registry, this will be a url to a + tarball. In the case of git dependencies, this will be the full git url + with commit sha. In the case of link dependencies, this will be the + location of the link target. -All optional dependencies should be included even if they're uninstallable -on the current platform. +* 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. +* link: A flag to indicate that this is a symbolic link. If this is + present, no other fields are specified, since the link target will also + be included in the lockfile. -##### requires +* dev, optional, devOptional: If the package is strictly part of the + `devDependencies` tree, then `dev` will be true. If it is strictly part + of the `optionalDependencies` tree, then `optional` will be set. If it + is both a `dev` dependency _and_ an `optional` dependency of a non-dev + dependency, then `devOptional` will be set. (An `optional` dependency of + a `dev` dependency will have both `dev` and `optional` set.) -This is a mapping of module name to version. This is a list of everything -this module requires, regardless of where it will be installed. The version -should match via normal matching rules a dependency either in our -`dependencies` or in a level higher than us. +* inBundle: A flag to indicate that the package is a bundled dependency. +* hasInstallScript: A flag to indicate that the package has a `preinstall`, + `install`, or `postinstall` script. -##### dependencies +* hasShrinkwrap: A flag to indicate that the package has an + `npm-shrinkwrap.json` file. + +* bin, license, engines, dependencies, optionalDependencies: fields from + `package.json` + +#### dependencies -The dependencies of this dependency, exactly as at the top level. +Legacy data for supporting versions of npm that use `lockfileVersion: 1`. +This is a mapping of package names to dependency objects. Because the +object structure is strictly hierarchical, symbolic link dependencies are +somewhat challenging to represent in some cases. + +npm 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. + +Dependency objects have the following fields: + +* version: a specifier that varies depending on the nature of the package, + and is usable in fetching a new copy of it. + + * bundled dependencies: Regardless of source, this is a version number + that is purely for informational purposes. + * registry sources: This is a version number. (eg, `1.2.3`) + * git sources: This is a git specifier with resolved committish. (eg, + `git+https://example.com/foo/bar#115311855adb0789a0466714ed48a1499ffea97e`) + * http tarball sources: This is the URL of the tarball. (eg, + `https://example.com/example-1.3.0.tgz`) + * local tarball sources: This is the file URL of the tarball. (eg + `file:///opt/storage/example-1.3.0.tgz`) + * local link sources: This is the file URL of the link. (eg + `file:libs/our-module`) + +* 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. For git + dependencies, this is the commit sha. + +* resolved: For registry sources this is path of the tarball relative to + the registry URL. If the tarball URL isn't on the same server as the + registry URL then this is a complete URL. + +* bundled: If true, this is the bundled dependency and will be installed + by the parent module. When installing, this module will be extracted + from the parent module during the extract phase, not installed as a + separate dependency. + +* dev: If true then this dependency is either a development dependency ONLY + of the top level module or a transitive dependency of one. 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. + +* optional: If true then this dependency is either an optional dependency + ONLY of the top level module or a transitive dependency of one. 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. + +* requires: This is a mapping of module name to version. This is a list of + everything this module requires, regardless of where it will be + installed. The version should match via normal matching rules a + dependency either in our `dependencies` or in a level higher than us. + +* dependencies: The dependencies of this dependency, exactly as at the top + level. ### See also * [npm shrinkwrap](/commands/npm-shrinkwrap) -* [shrinkwrap.json](/configuring-npm/shrinkwrap-json) -* [package-locks](/configuring-npm/package-locks) +* [npm-shrinkwrap.json](/configuring-npm/npm-shrinkwrap-json) * [package.json](/configuring-npm/package-json) * [npm install](/commands/npm-install) diff --git a/docs/content/configuring-npm/package-locks.md b/docs/content/configuring-npm/package-locks.md deleted file mode 100644 index a456ef8936645..0000000000000 --- a/docs/content/configuring-npm/package-locks.md +++ /dev/null @@ -1,178 +0,0 @@ ---- -title: package-locks -section: 5 -description: An explanation of npm lockfiles ---- - -### Description - -Conceptually, the "input" to [`npm install`](/commands/npm-install) is a [package.json](/configuring-npm/package-json), while its -"output" is a fully-formed `node_modules` tree: a representation of the -dependencies you declared. In an ideal world, npm would work like a pure -function: the same `package.json` should produce the exact same `node_modules` -tree, any time. In some cases, this is indeed true. But in many others, npm is -unable to do this. There are multiple reasons for this: - -* different versions of npm (or other package managers) may have been used to install a package, each using slightly different installation algorithms. - -* a new version of a direct semver-range package may have been published since the last time your packages were installed, and thus a newer version will be used. - -* A dependency of one of your dependencies may have published a new version, which will update even if you used pinned dependency specifiers (`1.2.3` instead of `^1.2.3`) - -* The registry you installed from is no longer available, or allows mutation of versions (unlike the primary npm registry), and a different version of a package exists under the same version number now. - -As an example, consider package A: - -```json -{ - "name": "A", - "version": "0.1.0", - "dependencies": { - "B": "<0.1.0" - } -} -``` - -package B: - -```json -{ - "name": "B", - "version": "0.0.1", - "dependencies": { - "C": "<0.1.0" - } -} -``` - -and package C: -```json -{ - "name": "C", - "version": "0.0.1" -} -``` - -If these are the only versions of A, B, and C available in the -registry, then a normal `npm install A` will install: - -```json -A@0.1.0 -`-- B@0.0.1 - `-- C@0.0.1 -``` - -However, if B@0.0.2 is published, then a fresh `npm install A` will -install: - -```bash -A@0.1.0 -`-- B@0.0.2 - `-- C@0.0.1 -``` - -assuming the new version did not modify B's dependencies. Of course, -the new version of B could include a new version of C and any number -of new dependencies. If such changes are undesirable, the author of A -could specify a dependency on B@0.0.1. However, if A's author and B's -author are not the same person, there's no way for A's author to say -that he or she does not want to pull in newly published versions of C -when B hasn't changed at all. - -To prevent this potential issue, npm uses [package-lock.json](/configuring-npm/package-lock-json) or, if present, [npm-shrinkwrap.json](/configuring-npm/shrinkwrap-json). These files are called package locks, or lockfiles. - -Whenever you run `npm install`, npm generates or updates your package lock, -which will look something like this: - -```json -{ - "name": "A", - "version": "0.1.0", - ...metadata fields... - "dependencies": { - "B": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/B/-/B-0.0.1.tgz", - "integrity": "sha512-DeAdb33F+" - "dependencies": { - "C": { - "version": "git://github.com/org/C.git#5c380ae319fc4efe9e7f2d9c78b0faa588fd99b4" - } - } - } - } -} -``` - -This file describes an *exact*, and more importantly *reproducible* -`node_modules` tree. Once it's present, any future installation will base its -work off this file, instead of recalculating dependency versions off -[package.json](/configuring-npm/package-json). - -The presence of a package lock changes the installation behavior such that: - -1. The module tree described by the package lock is reproduced. This means -reproducing the structure described in the file, using the specific files -referenced in "resolved" if available, falling back to normal package resolution -using "version" if one isn't. - -2. The tree is walked and any missing dependencies are installed in the usual -fashion. - -If `preshrinkwrap`, `shrinkwrap` or `postshrinkwrap` are in the `scripts` -property of the `package.json`, they will be executed in order. `preshrinkwrap` -and `shrinkwrap` are executed before the shrinkwrap, `postshrinkwrap` is -executed afterwards. These scripts run for both `package-lock.json` and -`npm-shrinkwrap.json`. For example to run some postprocessing on the generated -file: - -```json - "scripts": { - "postshrinkwrap": "json -I -e \"this.myMetadata = $MY_APP_METADATA\"" - } -``` - -#### Using locked packages - -Using a locked package is no different than using any package without a package -lock: any commands that update `node_modules` and/or `package.json`'s -dependencies will automatically sync the existing lockfile. This includes `npm -install`, `npm rm`, `npm update`, etc. To prevent this update from happening, -you can use the `--no-save` option to prevent saving altogether, or -`--no-shrinkwrap` to allow `package.json` to be updated while leaving -`package-lock.json` or `npm-shrinkwrap.json` intact. - -It is highly recommended you commit the generated package lock to source -control: this will allow anyone else on your team, your deployments, your -CI/continuous integration, and anyone else who runs `npm install` in your -package source to get the exact same dependency tree that you were developing -on. Additionally, the diffs from these changes are human-readable and will -inform you of any changes npm has made to your `node_modules`, so you can notice -if any transitive dependencies were updated, hoisted, etc. - -#### Resolving lockfile conflicts - -Occasionally, two separate npm install will create package locks that cause -merge conflicts in source control systems. As of `npm@5.7.0`, these conflicts -can be resolved by manually fixing any `package.json` conflicts, and then -running `npm install [--package-lock-only]` again. npm will automatically -resolve any conflicts for you and write a merged package lock that includes all -the dependencies from both branches in a reasonable tree. If -`--package-lock-only` is provided, it will do this without also modifying your -local `node_modules/`. - -To make this process seamless on git, consider installing -[`npm-merge-driver`](https://npm.im/npm-merge-driver), which will teach git how -to do this itself without any user interaction. In short: `$ npx -npm-merge-driver install -g` will let you do this, and even works with -pre-`npm@5.7.0` versions of npm 5, albeit a bit more noisily. Note that if -`package.json` itself conflicts, you will have to resolve that by hand and run -`npm install` manually, even with the merge driver. - -### See Also - -* https://medium.com/@sdboyer/so-you-want-to-write-a-package-manager-4ae9c17d9527 -* [package.json](/configuring-npm/package-json) -* [package-lock.json](/configuring-npm/package-lock-json) -* [shrinkwrap.json](/configuring-npm/shrinkwrap-json) -* [npm shrinkwrap](/commands/npm-shrinkwrap)