Skip to content

Latest commit

 

History

History
105 lines (79 loc) · 3.88 KB

environment_variables.md

File metadata and controls

105 lines (79 loc) · 3.88 KB

💻 Environment variables

Get / set

Unix shells and cmd.exe use different syntaxes to list, reference and pass environment variables. cross-env can be used to do this in a cross-platform way.

This is only an issue when executing shell commands outside Node.js, for example in npm scripts. In Node.js, process.env and the env option of child_process / execa should be used instead.

Listing

To list the current environment variables:

  • env must be used on Unix.
  • set on Windows.

Referencing

The syntax to reference environment variables is:

  • $VARIABLE on Unix.
  • %VARIABLE% on Windows.

Also if the variable is missing, its value will be:

  • '' on Unix and in Windows batch files.
  • '%VARIABLE%' in cmd.exe.

Passing

To pass environment variables to a command:

  • on Unix, it must be prepended with VARIABLE=value ...
  • on Windows, one must use Set VARIABLE=value or setx VARIABLE value as separate statements.

Case sensitivity

Environment variables are case sensitive on Unix. However this is not the case on Windows (except inside msys): if the same environment variable is defined twice but with different cases, the last defined prevails. This includes the env option passed to child_process methods such as spawn().

path-key can be used to solve this for the PATH environment variable.

Available variables

Most environment variables names are OS-specific:

The project osenv can be used to retrieve OS-specific environment variables names.

Summary

Outside Node.js (e.g. in npm scripts), environment variables should be referenced and passed using cross-env.

Use os methods or osenv to rerieve specific environment variables.


Next (🔒 Security)
Previous (💻 Package binaries)
Top