This example demonstrates the behavior of npm when reading an environment variable that is defined at different locations.
The documentation about npm config states the following:
Any environment variables that start with
npm_config_will be interpreted as a configuration parameter. For example, puttingnpm_config_foo=barin your environment will set thefooconfiguration parameter tobar. Any environment configurations that are not given a value will be given the value oftrue. Config values are case-insensitive, soNPM_CONFIG_FOO=barwill work the same. However, please note that inside npm-scripts npm will set it's own environment variables and Node will prefer those lowercase versions over any uppercase ones that you might set. For details see this issue.The four relevant files are:
- per-project configuration file (
/path/to/my/project/.npmrc)- per-user configuration file (defaults to
$HOME/.npmrc; configurable via CLI > option--userconfigor environment variable$NPM_CONF_USERCONFIG)- global configuration file (defaults to
$PREFIX/etc/npmrc; configurable via > CLI option--globalconfigor environment variable$NPM_CONF_GLOBALCONFIG)- npm's built-in configuration file (
/path/to/npm/npmrc)
This project contains a simple setup with different environment variable constellations. Some experiments and their result are described bellow.
In this experiment we simply pass the environment variable from the command line, one time in lower and the other time in upper case.
my_custom_variable=cli npm run verify
This results in the following variable states:
| variable | lowercase | uppercase | getEnvLower | getEnvUpper |
|---|---|---|---|---|
| 'MY_CUSTOM_VARIABLE' | 'cli' | undefined | 'cli' | undefined |
| 'npm_config_MY_CUSTOM_VARIABLE' | undefined | undefined | 'cli' | undefined |
| 'npm_package_config_MY_CUSTOM_VARIABLE' | undefined | undefined | 'cli' | undefined |
The table contains the following rows:
- variable : name as referenced for process.env, i.e.
process.env.MY_CUSTOM_VARIABLE - lowercase : value when reading the env key in lowercase, i.e.
process.env.my_custom_variable - uppercase : value when reading the env key in uppercase, i.e.
process.env.MY_CUSTOM_VARIABLE - getEnvLower: value when calling the method getEnv with key in lowercase, i.e.
getEnv('my_custom_variable') - getEnvUpper: value when calling the method getEnv with key in uppercase, i.e.
getEnv('MY_CUSTOM_VARIABLE')
MY_CUSTOM_VARIABLE=cli npm run verify
| variable | lowercase | uppercase | getEnvLower | getEnvUpper |
|---|---|---|---|---|
| 'MY_CUSTOM_VARIABLE' | undefined | 'cli' | undefined | 'cli' |
| 'npm_config_MY_CUSTOM_VARIABLE' | undefined | undefined | undefined | 'cli' |
| 'npm_package_config_MY_CUSTOM_VARIABLE' | undefined | undefined | undefined | 'cli' |
Here we set the env variable in the .npmrc file and run npm run verify.
content of .npmrc:
my_custom_variable=npmrc
| variable | lowercase | uppercase | getEnvLower | getEnvUpper |
|---|---|---|---|---|
| 'MY_CUSTOM_VARIABLE' | undefined | undefined | 'npmrc' | undefined |
| 'npm_config_MY_CUSTOM_VARIABLE' | 'npmrc' | undefined | 'npmrc' | undefined |
| 'npm_package_config_MY_CUSTOM_VARIABLE' | undefined | undefined | 'npmrc' | undefined |
content of .npmrc:
MY_CUSTOM_VARIABLE=npmrc
| variable | lowercase | uppercase | getEnvLower | getEnvUpper |
|---|---|---|---|---|
| 'MY_CUSTOM_VARIABLE' | undefined | undefined | 'npmrc' | undefined |
| 'npm_config_MY_CUSTOM_VARIABLE' | 'npmrc' | undefined | 'npmrc' | undefined |
| 'npm_package_config_MY_CUSTOM_VARIABLE' | undefined | undefined | 'npmrc' | undefined |
| variable | lowercase | uppercase | getEnvLower | getEnvUpper |
|---|---|---|---|---|
| 'MY_CUSTOM_VARIABLE' | undefined | undefined | 'npmrc' | 'npmrc' |
| 'npm_config_MY_CUSTOM_VARIABLE' | 'npmrc' | 'npmrc' | 'npmrc' | 'npmrc' |
| 'npm_package_config_MY_CUSTOM_VARIABLE' | undefined | undefined | 'npmrc' | 'npmrc' |
This result is interesting.
On a linux system, even when setting MY_CUSTOM_VARIABLE=npmrc in uppercase it appears with the lowercase key.
This has to do with the remark about how npm handles env variables inside scripts
and the linked issue.
On a windows system, the variable appears twice, once with a lowercase key and another time with the uppercase key. The definition in .npmrc seems to be case-insensitive, it doesn't matter if the key is defined in lower or upper case, the result stays the same.
Here, we include a config object in the package.json as described in the package.json doc.
"config": {
"my_custom_variable": "package"
}
| variable | lowercase | uppercase | getEnvLower | getEnvUpper |
|---|---|---|---|---|
| 'MY_CUSTOM_VARIABLE' | undefined | undefined | 'package' | undefined |
| 'npm_config_MY_CUSTOM_VARIABLE' | undefined | undefined | 'package' | undefined |
| 'npm_package_config_MY_CUSTOM_VARIABLE' | 'package' | undefined | 'package' | undefined |
"config": {
"MY_CUSTOM_VARIABLE": "package"
}
| variable | lowercase | uppercase | getEnvLower | getEnvUpper |
|---|---|---|---|---|
| 'MY_CUSTOM_VARIABLE' | undefined | undefined | undefined | 'package' |
| 'npm_config_MY_CUSTOM_VARIABLE' | undefined | undefined | undefined | 'package' |
| 'npm_package_config_MY_CUSTOM_VARIABLE' | undefined | 'package' | undefined | 'package' |