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.
To list the current environment variables:
envmust be used on Unix.seton Windows.
The syntax to reference environment variables is:
$VARIABLEon Unix.%VARIABLE%on Windows.
Also if the variable is missing, its value will be:
''on Unix and in Windows batch files.'%VARIABLE%'incmd.exe.
To pass environment variables to a command:
- on Unix, it must be prepended with
VARIABLE=value ... - on Windows, one must use
Set VARIABLE=valueorsetx VARIABLE valueas separate statements.
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.
Most environment variables names are OS-specific:
SHELLon Unix isComSpecon Windows.os.userInfo().shellreturnsnullon Windows.PS1on Unix isPROMPTon Windows.PWDon Unix isCDon Windows.process.cwd()andprocess.chdir()should be used instead.HOMEon Unix isUSERPROFILEon Windows.os.homedir()(faster)os.userInfo().homedir(more accurate) should be used instead.TMPDIRin Unix isTMPorTEMPon Windows.os.tmpdir()should be used instead.USERorLOGNAMEon Unix isUSERDOMAINandUSERNAMEon Windows.usernameoros.userInfo().usernameshould be used instead.HOSTNAMEon Unix isCOMPUTERNAMEon Windows.os.hostname()should be used instead.
The project osenv can be used to retrieve
OS-specific environment variables names.
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.