Learning Node.js? Download my free Node Handbook 🔥
process core module of Node provides the
env property which hosts all the environment variables that were set at the moment the process was started.
Here is an example that accesses the NODE_ENV environment variable, which is set to
development by default.
processdoes not require a “require”, it’s automatically available.
process.env.NODE_ENV // "development"
Setting it to “production” before the script runs will tell Node that this is a production environment.
In the same way you can access any custom environment variable you set.
Here we set 2 variables for API_KEY and API_SECRET
API_KEY=123123 API_SECRET=456456 node app.js
We can get them in Node.js by running
process.env.API_KEY // "123123" process.env.API_SECRET // "456456"
Here's my latest YouTube video. I talk about why I think that dogs are a great help for developers working remotely: