I'm trying to follow a tutorial on NodeJS. I don't think I missed anything but whenever I call the process.env.NODE_ENV the only value I get back is undefined. According to my research the default value should be development. How is this value dynamically set and where is it set initially?
-
3to set NODE_ENV for heroku apps you can use: `heroku config:set NODE_ENV="production"` – Connor Leech Jun 17 '14 at 23:09
-
23Although the answers below will solve the problem temporarily by setting the environment variable, the bigger lesson here should be that you can never know whether any environment variable will be set... So write your code accordingly and test whether it's set and if so, to what. Don't make assumptions about it. – Stijn de Witt Dec 16 '15 at 01:17
-
Does this answer your question? [How to set NODE\_ENV to production/development in OS X](https://stackoverflow.com/questions/9198310/how-to-set-node-env-to-production-development-in-os-x) – Michael Freidgeim Jan 04 '21 at 11:45
20 Answers
process.env is a reference to your environment, so you have to set the variable there.
To set an environment variable in Windows:
SET NODE_ENV=development
on macOS / OS X or Linux:
export NODE_ENV=development
- 2,061
- 3
- 10
- 31
- 3,578
- 1
- 20
- 12
-
This worked, only on my OS X the word 'EXPORT' needed to be lowercase. Thanks. – basheps Jun 19 '12 at 15:26
-
7you might add that if NODE_ENV is not set the app behaves like in "development" mode – Rocco Sep 21 '13 at 20:53
-
1For Linux, vi ~/.bash_profile, then insert NODE_ENV=development and save. – stonyau Oct 12 '14 at 03:41
-
13In case anyone else struggles... there is a difference between "SET V = VAL" and "SET V=VAL". Spaces matter. – willem Mar 14 '16 at 06:39
-
5Note that the module "cross-env" to do similar to the above, and it will work on both OSX and Windows: "cross-env NODE_ENV=development". You need to install cross-env first: "npm install cross-env --save". Have that in a script in your package.json and you're good to go on both platforms. – Antonio Brandao Jun 09 '16 at 15:59
-
12Didn't work for me. I executed `export NODE_ENV=development` on my Mac terminal before doing a `react-native run-ios` from the same terminal. When debugging, the value of `process.env.NODE_ENV` is undefined. – Ash Jun 26 '19 at 05:54
-
-
@Ash You have to restart your terminal for the new env vars to take effect. – Torsten Barthel Aug 20 '20 at 23:27
-
tips
in package.json:
"scripts": {
"start": "set NODE_ENV=dev && node app.js"
}
in app.js:
console.log(process.env.NODE_ENV) // dev
console.log(process.env.NODE_ENV === 'dev') // false
console.log(process.env.NODE_ENV.length) // 4 (including a space at the end)
so, this may better:
"start": "set NODE_ENV=dev&& node app.js"
or
console.log(process.env.NODE_ENV.trim() === 'dev') // true
- 6,261
- 1
- 30
- 25
-
3What you can do is this, so you don't have to trim: `"start": "set NODE_ENV=dev&& node app.js"` – Bernardo Dal Corno Dec 19 '18 at 15:56
-
2Excellent observation, `console.log(process.env.NODE_ENV.length)` **// 4 (including a space at the end)** – gregn3 Jun 08 '20 at 14:01
-
1This is safer: `console.log(('' + process.env.NODE_ENV).trim() === 'dev') // true` , as it will not throw an error even if **process.env.NODE_ENV** is undefined. – gregn3 Jun 08 '20 at 14:14
-
3
-
3You can do this to solve the space issue: `"start": "export NODE_ENV='dev' && ..."`. So put the env between two `'`. – erkage Jul 11 '21 at 08:46
-
1
For people using *nix (Linux, OS X, etc.), there's no reason to do it via a second export command, you can chain it as part of the invoking command:
NODE_ENV=development node server.js
Easier, no? :)
- 1,656
- 2
- 21
- 17
-
4A package like cross-env will allow that to work on windows as well. – walkerrandophsmith Sep 06 '16 at 15:38
We ran into this problem when working with node on Windows.
Rather than requiring anyone who attempts to run the app to set these variables, we provided a fallback within the application.
var environment = process.env.NODE_ENV || 'development';
In a production environment, we would define it per the usual methods (SET/export).
- 626
- 5
- 4
-
6
-
What if the value of **process.env.NODE_ENV** is undefined and the user wants to change development to production in .env file? – Kiran RS Mar 17 '21 at 13:21
-
You can use the cross-env npm package. It will take care of trimming the environment variable, and will also make sure it works across different platforms.
In the project root, run:
npm install cross-env
Then in your package.json, under scripts, add:
"start": "cross-env NODE_ENV=dev node your-app-name.js"
Then in your terminal, at the project root, start your app by running:
npm start
The environment variable will then be available in your app as process.env.NODE_ENV, so you could do something like:
if (process.env.NODE_ENV === 'dev') {
// Your dev-only logic goes here
}
- 7,079
- 6
- 32
- 43
-
1I don't think this works. Tried it in my current project and NODE_ENV is still undefined. This might need some more config to work it seems. – Aakash Thakur May 05 '19 at 09:46
-
1
-
1for me somehow the accepted answer was not working, but this <3 – Chathura Buddhika Apr 06 '21 at 13:20
-
for me it is not working either on local machine windows. NODE_ENV is still undefined – rendom Sep 12 '21 at 07:29
In macOS for those who are using the express version 4.x.x and using the DOTENV plugin, need to use like this:
After installing the plugin import like the following in the file where you init the application:
require('dotenv').config({path: path.resolve(__dirname+'/.env')});In the root directory create a file '.env' and add the varaiable like:
NODE_ENV=developmentorNODE_ENV = development
- 432
- 5
- 11
in package.json we have to config like below (works in Linux and Mac OS)
the important thing is "export NODE_ENV=production" after your build commands below is an example:
"scripts": {
"start": "export NODE_ENV=production && npm run build && npm run start-server",
"dev": "export NODE_ENV=dev && npm run build && npm run start-server",
}
for dev environment, we have to hit "npm run dev" command
for a production environment, we have to hit "npm run start" command
- 2,553
- 1
- 24
- 36
-
When I do this I get the error: 'export' is not recognized as an internal or external command, operable program or batch file. – Casivio Mar 03 '21 at 19:15
-
@Casivio on Windows, use `set` instead of `export`. `"scripts": { "start": "set NODE_ENV=production && npm run build && npm run start-server" }` – cweave Jan 02 '22 at 06:08
As early as possible in your application, require and configure dotenv.
require('dotenv').config()
- 391
- 3
- 6
-
-
THIS. This is the solution. Don't stuff env variables in your package.json – Scott Jan 09 '22 at 00:51
It is due to OS
In your package.json, make sure to have your scripts(Where app.js is your main js file to be executed & NODE_ENV is declared in a .env file).Eg:
"scripts": {
"start": "node app.js",
"dev": "nodemon server.js",
"prod": "NODE_ENV=production & nodemon app.js"
}
For windows
Also set up your .env file variable having NODE_ENV=development
If your .env file is in a folder for eg.config folder make sure to specify in app.js(your main js file)
const dotenv = require('dotenv'); dotenv.config({ path: './config/config.env' });
- 19
- 1
You can use the dotenv package from npm, here is the link: https://www.npmjs.com/package/dotenv
Which allows you to place all your configuration in a .env file
- 599
- 5
- 8
-
Looks like envalid no longer ships with dotenv so you now need to manually install / call this before you invoke envalid's cleanEnv(). This caught me out so leaving this here for others – Stretch0 Sep 20 '21 at 11:23
install dotenv module ( npm i dotenv --save )
require('dotenv').config() //write inside the file where you will use the variable
console.log(process.env.NODE_ENV) // returns value stored in .env file
- 11
- 1
If you faced this probem in React, you need react-scripts@0.2.3 and higher. Also for other environment variables than NODE_ENV to work in React, they need to be prefixed with REACT_APP_.
- 3,296
- 1
- 19
- 19
For me, the issue was that I was using the pkg library to turn my app into an executable binary. In that case, the accepted solutions didn't work. However, using the following code solved my problem:
const NODE_ENV = (<any>process).pkg ? 'production' : process.env.NODE_ENV;
I found this solution here on GitHub.
- 2,017
- 20
- 25
In Electron Js
"scripts": {
"start": "NODE_ENV=development electron index.js",
},
- 2,085
- 1
- 20
- 15
If you define any function with the name process then that may also cause this issue.
- 61
- 8
Defining process.env.NODE_ENV in package.json for Windows/Mac/Linux:
Here's what worked for me on my Mac (MacBook Pro 2019, 16 inch, Big Sur):
"scripts": {
"build": "export NODE_ENV=prod || set NODE_ENV=prod&& npx eslint . && node --experimental-json-modules ./backend/app.js && gulp",
},
Using the export NODE_ENV=prod || set NODE_ENV=prod&& string may work in Windows and Linux but I haven't tested that.
If someone could confirm that would be great.
Unfortunately using the cross-env npm package did NOT work for me at all in my package.json file and I spend a long time on my Mac trying to make this work.
- 767
- 7
- 19
You can also set it by code, for example:
process.env.NODE_ENV = 'test';
- 6,128
- 2
- 31
- 50
- 143
- 1
- 5
-
79I STRONGLY discourage anyone from ever changing the environment identifier in application logic. You never want the application to suddenly think it is something else than it really is. This should only ever be changed on system-level, as many of the other answers suggest. – Kafoso Jul 07 '15 at 13:00
-
7You might want to read about 12 factor apps: https://12factor.net/config. This code would be a violation of that. There are good reasons to keep your config separate from your code. – jcollum Jul 28 '17 at 16:00
-
2It might be useful only for automated testing, but even then it could be abstracted and injected into your main code, instead of being read directly from env. – Angelos Pikoulas Nov 21 '18 at 18:21
-
1Let's not downvote this answer so much that the informative comment by @Kafoso gets diminished. Or add this information to some well-voted answer. – Vibhor Dube Jun 17 '20 at 09:56