Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Explain environment variables from package.json, .npmrc and npm #7856

Open
wants to merge 3 commits into
base: latest
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 55 additions & 18 deletions docs/lib/content/using-npm/scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,32 +259,69 @@ executing the scripts. So, if your package.json has this:
then you could run `npm start` to execute the `bar` script, which is
exported into the `node_modules/.bin` directory on `npm install`.

#### package.json vars
#### Environment variables from `package.json`

The package.json fields are tacked onto the `npm_package_` prefix. So,
for instance, if you had `{"name":"foo", "version":"1.2.5"}` in your
package.json file, then your package scripts would have the
`npm_package_name` environment variable set to "foo", and the
`npm_package_version` set to "1.2.5". You can access these variables
in your code with `process.env.npm_package_name` and
`process.env.npm_package_version`, and so on for other fields.
Several fields of the `package.json` file are converted into environment variables prefixed with `npm_package_`. For example, if your `package.json` contains this:
```bash
{
"name": "foo",
"version": "1.2.5",
"engines": {
"node": ">20"
},
"config": {
"port": "3000"
},
"bin": {
"cli": "path/to/cli"
}
}
```
then these fields can be accessed in your code like so:
```js
console.log(process.env.npm_package_name) // prints "foo"
console.log(process.env.npm_package_version) // prints "1.2.5"
console.log(process.env.npm_package_engines_node) // prints ">20"
console.log(process.env.npm_package_config_port) // prints "3000"
console.log(process.env.npm_package_bin_cli) // prints "path/to/cli"
```
Note that the keys in a nested field (such as `engines`, `bin` or `config`) are flattened using `_` as a separator.

See [`package.json`](/configuring-npm/package-json) for more on package configs.
#### Environment variables from `npm`

#### current lifecycle event
The current state of npm is propagated to environment variables via:
- `npm_command`: the npm command being run. E.g. `run-script` when run using `npm run <name-of-script>`.
- `npm_lifecycle_script`: the current script being executed.
- `npm_lifecycle_event`: the name of the script being executed.
- `npm_execpath`: the path to the npm cli file running this script.
- `npm_node_execpath`: the path to the node executable running this script.
- `npm_package_json`: the path to the `package.json` file containing this script.

Lastly, the `npm_lifecycle_event` environment variable is set to
whichever stage of the cycle is being executed. So, you could have a
single script used for different parts of the process which switches
based on what's currently happening.
For example if your `package.json` is
```bash
{
"scripts": {
"start": "node index.js"
}
}
```
then these fields can be accessed in your code like so:
```js
console.log(process.env.npm_lifecycle_event) // prints "start"
console.log(process.env.npm_lifecycle_script) // prints "node index.js"
```

Objects are flattened following this format, so if you had
`{"scripts":{"install":"foo.js"}}` in your package.json, then you'd
see this in the script:
#### Environment variables from `.npmrc`

Variables from the [`.npmrc` file](/cli/v10/using-npm/config#npmrc-files) (if used) are populated with the prefix `npm_config_`. For example, if your `.npmrc` file contains this:
```bash
process.env.npm_package_scripts_install === "foo.js"
script-shell="/bin/bash"
```
then these variables can be accessed in your code like so:
```js
console.log(process.env.npm_config_script_shell) // prints "/bin/bash"
```
Note that `-` is converted to `_` in the environment variable names.

### Examples

Expand Down