6

How do I set the value of process.env.SOME_KEY=some value in the operating system and then read it in my node application? I am running on Windows.

I tried these steps:

SET SOME_KEY=abc 
npm start  

But when I try to read process.env.SOME_KEY, I am getting "undefined".

How do I set a env value and how do i read that in my code?

Graham
  • 7,431
  • 18
  • 59
  • 84
BGeorge
  • 139
  • 1
  • 4
  • 11

5 Answers5

7

Try this: https://www.npmjs.com/package/dotenv

All you need to do is add a .env with your environment variables, and require('dotenv').config() as soon as you can in your application (the script called by npm start would be a good place)

Rick
  • 897
  • 6
  • 14
1
SOME_KEY=`${abc}` Fixed it for me.
0

The npm module cross-env was made exactly for this purpose. The way you set an environment variable is not same across all the OSes. cross-env solves this.

Else, you can also try dotenv that solves this with a .env file.

You can also do this at the very basic level:

process.env.SOME_KEY = "somevalue";
Faizuddin Mohammed
  • 4,118
  • 5
  • 27
  • 51
0

Add this in index file

require('dotenv').config({path: path.join(__dirname, '.env')});
Dharman
  • 30,962
  • 25
  • 85
  • 135
0
const path = require('path');
require('dotenv').config( { path: path.join(__dirname, 'variables.env') });
Milind Morey
  • 2,100
  • 19
  • 15