How can I set an environmental variable in node.js?

node.js

node.js Problem Overview


How can I set an environmental variable in node.js?

I would prefer not to rely on anything platform specific, such as running export or cmd.exe's set.

node.js Solutions


Solution 1 - node.js

You can set your environment variables in process.env:

process.env['VARIABLE'] = 'value';

-OR-

process.env.VARIABLE = 'value';

Node should take care of the platform specifics.

Solution 2 - node.js

First you should install this package :- https://github.com/motdotla/dotenv [npm install dotenv]

Then you need to create a .env file in your project's root directory, and there you can add variables like below:-

NODE_ENV=PRODUCTION
DATABASE_HOST=localhost

Now you can easily access these variables in your code like below:-

require('dotenv').config()
console.log(process.env.NODE_ENV);

It worked for me, hopefully that helps.

Solution 3 - node.js

node v14.2.0 To set env variable first create a file name config.env in your project home directory and then write all the variables you need, for example

config.env

NODE_ENV=development
PORT=3000
DATABASE=mongodb+srv://lord:<PASSWORD>@cluster0-eeev8.mongodb.net/tour-guide?retryWrites=true&w=majority
DATABASE_LOCAL=mongodb://localhost:27017/tours-test
DATABASE_PASSWORD=UDJUKXJSSJPWMxw

now install dotenv from npm, dotenv will offload your work

npm i dotenv

now in your server starter script, in my case it is server.js use doenv to load env variables.

const dotenv = require('dotenv');
dotenv.config({ path: './config.env' });
const app = require('./app'); // must be after loading env vars using dotenv

//starting server
const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`app running on port ${port}...`);
});

I am using express, all my express code in app.js, writing here for your reference

const express = require('express');
const tourRouter = require('./route/tourRouter');
const userRouter = require('./route/userRouter');

if (process.env.NODE_ENV === 'development') {
  console.log('mode development');
}
app.use(express.json());

app.use('/api/v1/tours', tourRouter);
app.use('/api/v1/users', userRouter);

module.exports = app;

now start your server using the console, I am using nodemon, you can install it from npm;

nodemon server.js

Solution 4 - node.js

Well what I do is I first install dotenv package from npm

npm install dotenv

Then I import it in my file.

require('dotenv').config();

Then You are good to go. :)

You can read variables by using:

console.log(process.env.MY_VARIABLE);

While you can SET a Variable by using:

process.env.MY_OTHER_VARIABLE = 'helloworld;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionabrknView Question on Stackoverflow
Solution 1 - node.jslanzzView Answer on Stackoverflow
Solution 2 - node.jsjagjeetView Answer on Stackoverflow
Solution 3 - node.jsRafiqView Answer on Stackoverflow
Solution 4 - node.jsspadletskysView Answer on Stackoverflow