The default value of process.env.NODE_ENV in node

Hi campers,

I am looking at the getting-started section of node. According to the tutorial the value of process.env.NODE_ENV is set to development by default but i get undefined when i run console.log(process.env.NODE_ENV);. What am i missing here?

Hello there,

Potentially, you need to install and require a package called dotenv. Use it like this, at the very top of your myApp.js (or, server.js) file:

require('dotenv').config();

All of this is assuming you have created a .env file with that variable.

Hope this helps

Hi @Sky020. I do know about the dotenv package and how to use it to conceal sensitive information like database key. However i am trying to understand the process module and this getting-started tutorial states:

The process core module of Node.js 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.

I assume from the last statement that NODE_ENV is some kind of builtin environment variable set to development all the time. Am i misunderstanding something here?

Something (you, or a lib, etc.) needs to set the property, it is (as far as I know) not a default. It will be undefined if nothing has set it.

If you log out process.env you can see what properties are on it.

1 Like

I notice process.env doesn’t have NODE_ENV property. That makes the statement below in the doc. quite misleading.

Here is an example that accesses the NODE_ENV environment variable, which is set to development by default.

Not sure why it is worded like it is, it was mention in this issue as well.