Languages
[Edit]
EN

Bash - pass environment variables to Node.js process (during command running)

3 points
Created by:
Aisha
418

In this article, we would like to show you how to pass environment variables to Node.js process using Bash.

Quick solution (run in Bash):

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.

 

Hint: we can use spaces and special characters in variable value by using VARIABLE_NAME="VARIABLE NAME" node script.js construction.

 

Practical example

Edit

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:

Bash command:

Script output:

 

See also

Edit
  1. Node.js - common way to pass environment variables to Node.js process (during command running)

  2. cmd.exe - pass environment variables to Node.js process (during command running)

References

Edit
  1. Process | Node.js v19.1.0 Documentation

Alternative titles

  1. Bash - pass env variables to Node.js process (during command running)
  2. Bash - pass env variables to Node.js process (via command line)
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join