Node. js exposes the current process's environment variables to the script as an object called process. env. From there, the Express web server framework popularized using an environment variable called NODE_ENV as a flag to indicate whether the server should be running in “development” mode vs “production” mode.
Node. js assumes it's always running in a development environment. You can signal Node. js that you are running in production by setting the NODE_ENV=production environment variable. This is usually done by executing the command.
NODE_ENV . When you are using Create React App, the react-scripts will set the value of NODE_ENV to development when npm start is executed and to production when you run npm run build .
The process. env global variable is injected by the Node at runtime for your application to use and it represents the state of the system environment your application is in when it starts. For example, if the system has a PATH variable set, this will be made accessible to you through process. env.
NODE_ENV
is an environment variable made popular by the express web server framework. When a node application is run, it can check the value of the environment variable and do different things based on the value. NODE_ENV
specifically is used (by convention) to state whether a particular environment is a production or a development environment. A common use-case is running additional debugging or logging code if running in a development environment.
You can use the following code to access the environment variable yourself so that you can perform your own checks and logic:
var environment = process.env.NODE_ENV
Assume production if you don't recognise the value:
var isDevelopment = environment === 'development'
if (isDevelopment) {
setUpMoreVerboseLogging()
}
You can alternatively using express' app.get('env')
function, but note that this is NOT RECOMMENDED as it defaults to "development"
, which may result in development code being accidentally run in a production environment - it's much safer if your app throws an error if this important value is not set (or if preferred, defaults to production logic as above).
Be aware that if you haven't explicitly set NODE_ENV
for your environment, it will be undefined
if you access it from process.env
, there is no default.
How to actually set the environment variable varies from operating system to operating system, and also depends on your user setup.
If you want to set the environment variable as a one-off, you can do so from the command line:
export NODE_ENV=production
$env:NODE_ENV = 'production'
In the long term, you should persist this so that it isn't unset if you reboot - rather than list all the possible methods to do this, I'll let you search how to do that yourself!
Convention has dictated that there are two 'main' values you should use for NODE_ENV
, either production
or development
, all lowercase. There's nothing to stop you from using other values, (test
, for example, if you wish to use some different logic when running automated tests), but be aware that if you are using third-party modules, they may explicitly compare with 'production'
or 'development'
to determine what to do, so there may be side effects that aren't immediately obvious.
Finally, note that it's a really bad idea to try to set NODE_ENV
from within a node application itself - if you do, it will only be applied to the process from which it was set, so things probably won't work like you'd expect them to. Don't do it - you'll regret it.
NODE_ENV is an environmental variable that stands for node environment in express server.
It's how we set and detect which environment we are in.
It's very common using production
and development
.
Set:
export NODE_ENV=production
Get:
You can get it using app.get('env')
I assume the original question included how does Express use this environment variable.
Express uses NODE_ENV to alter its own default behavior. For example, in development mode, the default error handler will send back a stacktrace to the browser. In production mode, the response is simply Internal Server Error
, to avoid leaking implementation details to the world.
Typically, you'd use the NODE_ENV
variable to take special actions when you develop, test and debug your code. For example to produce detailed logging and debug output which you don't want in production. Express itself behaves differently depending on whether NODE_ENV
is set to production
or not. You can see this if you put these lines in an Express app, and then make a HTTP GET request to /error
:
app.get('/error', function(req, res) {
if ('production' !== app.get('env')) {
console.log("Forcing an error!");
}
throw new Error('TestError');
});
app.use(function (req, res, next) {
res.status(501).send("Error!")
})
Note that the latter app.use()
must be last, after all other method handlers!
If you set NODE_ENV
to production
before you start your server, and then send a GET /error
request to it, you should not see the text Forcing an error!
in the console, and the response should not contain a stack trace in the HTML body (which origins from Express).
If, instead, you set NODE_ENV
to something else before starting your server, the opposite should happen.
In Linux, set the environment variable NODE_ENV like this:
export NODE_ENV='value'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With