EN
cmd.exe - pass environment variables to Node.js process (during command running)
7 points
In this article, we would like to show you how to pass environment variables to Node.js process using Windows Command Prompt (cmd.exe).
Quick solution (run in cmd.exe
):
xxxxxxxxxx
1
set VARIABLE_NAME=VARIABLE_VALUE&&node script.js
Where:
VARIABLE_NAME
is environment variable name that we want to pass,VARIABLE_VALUE
is environment variable value that we want to pass,script.js
is file name that will be executed receiving the environment variable.
Warning: each character placed before
&&
will be treat as variable value, so avoid unnecessary spaces.
Note: to escape
&
and^
characters in variable value use^
character, e.g.:xxxxxxxxxx
1set NAMES=Tom^&Jerry&&node script.js
In this example, we pass ADMIN_USERNAME
and ADMIN_PASSWORD
environment variables into script.js
script. Then we access passed variables inside script.js
script using process.env
property.
script.js
file:
xxxxxxxxxx
1
console.log('Admin username: ' + process.env.ADMIN_USERNAME);
2
console.log('Admin password: ' + process.env.ADMIN_PASSWORD);
cmd.exe
command:
xxxxxxxxxx
1
set ADMIN_USERNAME=admin&&set ADMIN_PASSWORD=admin&&node script.js
Script output:
xxxxxxxxxx
1
Admin username: admin
2
Admin password: admin