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:
env
must be used on Unix.set
on Windows.
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%'
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=value
orsetx VARIABLE value
as 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:
SHELL
on Unix isComSpec
on Windows.os.userInfo().shell
returnsnull
on Windows.PS1
on Unix isPROMPT
on Windows.PWD
on Unix isCD
on Windows.process.cwd()
andprocess.chdir()
should be used instead.HOME
on Unix isUSERPROFILE
on Windows.os.homedir()
(faster)os.userInfo().homedir
(more accurate) should be used instead.TMPDIR
in Unix isTMP
orTEMP
on Windows.os.tmpdir()
should be used instead.USER
orLOGNAME
on Unix isUSERDOMAIN
andUSERNAME
on Windows.username
oros.userInfo().username
should be used instead.HOSTNAME
on Unix isCOMPUTERNAME
on 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.