What Is Node_Env and How to Use It in Express

NODE_ENV variable in Node.js

NODE_ENV is the name of an environment variable, and you can access and change it in your shell Ex: export NODE_ENV=development , you can change it when running your process, Ex: NODE_ENV=production node application.js, or you can change it in one of your shell's configuration files.

When is process.env set to NODE_ENV?

if condition is there to check if NODE_ENV has been set or not (e.g., "" will evaluate to false).

* inside app.get() is a wildcard that matches any route a user could type in a browser for this server (i.e., http://localhost/*).

So what it does is that if the NODE_ENV is set to something (not necessarily dev or production environment), then for any route (or any web page on the site) requested, you are going to send the client/build/index.html file back - basically render an index (main) page of the website.

This environment variable (i.e., NODE_ENV) is defined, well, as an environment variable in some computer (e.g., VM somewhere on the cloud like Heroku). On a cloud, it would probably have "production" set as a value. On your laptop, it may not have anything as you are just testing it out, playing with it, etc. But production environment is quite important because this is where users of your app can see and use your app.

According to Express.js docs, "Setting NODE_ENV to “production” makes Express cache view templates; cache CSS files generated from CSS extensions; and generate less verbose error messages." Ref: https://expressjs.com/en/advanced/best-practice-performance.html

My guess is that it sends this index.html file while on Heroku, and does something else (depending on defined routes) when it is not.

This link may also help: https://dev.to/flippedcoding/difference-between-development-stage-and-production-d0p

P.S. Natalie noted that Heroku sets Node.js applications to use NODE_ENV=production by default since 2015. Ref: https://devcenter.heroku.com/changelog-items/688

How to set NODE_ENV to production/development in OS X

Before running your app, you can do this in console,

export NODE_ENV=production

Or if you are in windows you could try this:

SET NODE_ENV=production

for PowerShell:

$env:NODE_ENV="production"

or you can run your app like this:

NODE_ENV=production node app.js

You can also set it in your js file:

process.env.NODE_ENV = 'production';

But I don't suggest to do it in your runtime file, since it's not easy to open up VIM in your server and change it to production. You can make a config.json file in your directory and everytime your app runs, it reads from it and sets the configuration.

Using NODE_ENV with multiple environments in JavaScript projects

NODE_ENV is used to differentiate between development and production instances. It is not a good idea to run production code without NODE_ENV=production. NODE_ENV=development is usually not that important, because libraries usually just check to see if NODE_ENV !== 'production'. So if you want to have multiple production node environments, or production-like environments, each of them should set NODE_ENV=production. That said, you can definitely set other environment variables to whatever values you desire, and read them back from node at runtime.

A reasonable example would be to have a local staging and production versions of your configuration. In this case, I would recommend having NODE_ENV be just one of the parameters you set up for each environment. For instance, you might want three different databases for each of local, staging and production but set NODE_ENV to development on local, and production for both staging and production.

Since the variables will be shell variables, you will need a way of loading certain environment variables on the target operating system prior to running the server. Modules like https://www.npmjs.com/package/dotenv look promising for this purpose.

How can I set NODE_ENV=production on Windows?

Current versions of Windows use Powershell as the default shell, so use:

$env:NODE_ENV="production"

Per @jsalonen's answer below. If you're in CMD (which is no longer maintained), use

set NODE_ENV=production

This should be executed in the command prompt where you intend to run your Node.js application.

The above line would set the environment variable NODE_ENV for the command prompt where you execute the command.

To set environment variables globally so they persist beyond just the single command prompt, you can find the tool from System in Control Panel (or by typing 'environment' into the search box in the start menu).

How can I recognize NODE_ENV in Node.js?

I have found the answer

process.env.NODE_ENV

How to use NODE_ENV to choose which database is connected?

Seems your code is fine. I think you made a typo in your .env file.

Here is a sample for your .env:

NODE_ENV=test
TEST_DATABASE=mongodb://localhost:27017
DEVELOPMENT_DATABASE=mongodb://localhost:27018
PASSWORD=abcxyz
HOST=localhost
PORT=8000

Don't use NODE_ENV="test", spaces, or anything like this.



Related Topics



Leave a reply



Submit