Skip to content

Commit 0d30aa4

Browse files
mcollinaUzlopakbmuenzenmeyer
authored
NODE_ENV is not part of Node.js core and an antipattern (#6986)
* NODE_ENV is not part of Node.js core and an antipattern Setting NODE_ENV to anything but production is an antipattern should be avoided. Signed-off-by: Matteo Collina <[email protected]> * Update apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md Co-authored-by: Aras Abbasi <[email protected]> Signed-off-by: Matteo Collina <[email protected]> * Update apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md Signed-off-by: Brian Muenzenmeyer <[email protected]> * fix: address content lints --------- Signed-off-by: Matteo Collina <[email protected]> Signed-off-by: Matteo Collina <[email protected]> Signed-off-by: Brian Muenzenmeyer <[email protected]> Co-authored-by: Aras Abbasi <[email protected]> Co-authored-by: Brian Muenzenmeyer <[email protected]>
1 parent 1606239 commit 0d30aa4

File tree

1 file changed

+35
-20
lines changed

1 file changed

+35
-20
lines changed
Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
---
22
title: Node.js, the difference between development and production
33
layout: learn
4-
authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, RenanTKN
4+
authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, RenanTKN, mcollina
55
---
66

77
# Node.js, the difference between development and production
88

9-
You can have different configurations for production and development environments.
9+
**There is no difference between development and production in Node.js**, i.e., there are no specific settings you need to apply to make Node.js work in a production configuration.
10+
However, a few libraries in the npm registry recognize using the `NODE_ENV` variable and default it to a `development` setting.
11+
Always run your Node.js with the `NODE_ENV=production` set.
1012

11-
Node.js assumes it's always running in a development environment.
12-
You can signal Node.js that you are running in production by setting the `NODE_ENV=production` environment variable.
13+
A popular way of configuring your application is by using the [twelve factor methodology](https://12factor.net/).
14+
15+
## NODE_ENV in Express
16+
17+
In the wildly popular [express](https://expressjs.com/) framework, setting the `NODE_ENV` to `production` generally ensures that:
18+
19+
- logging is kept to a minimum, essential level
20+
- more caching levels take place to optimize performance
1321

1422
This is usually done by executing the command
1523

@@ -25,16 +33,32 @@ You can also apply the environment variable by prepending it to your application
2533
NODE_ENV=production node app.js
2634
```
2735

28-
This environment variable is a convention that is widely used in external libraries as well.
36+
For example, in an Express app, you can use this to set different error handlers per environment:
2937

30-
Setting the environment to `production` generally ensures that
38+
```js
39+
if (process.env.NODE_ENV === 'development') {
40+
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
41+
}
3142

32-
- logging is kept to a minimum, essential level
33-
- more caching levels take place to optimize performance
43+
if (process.env.NODE_ENV === 'production') {
44+
app.use(express.errorHandler());
45+
}
46+
```
3447

3548
For example [Pug](https://pugjs.org), the templating library used by [Express](https://expressjs.com), compiles in debug mode if `NODE_ENV` is not set to `production`. Express views are compiled in every request in development mode, while in production they are cached. There are many more examples.
3649

37-
You can use conditional statements to execute code in different environments:
50+
**This environment variable is a convention widely used in external libraries, but not within Node.js itself**.
51+
52+
## Why is NODE_ENV considered an antipattern?
53+
54+
An environment is a digital platform or a system where engineers can build, test, _deploy_, and manage software products. Conventionally, there are four stages or types of environments where our application is run:
55+
56+
- Development
57+
- Testing
58+
- Staging
59+
- Production
60+
61+
The fundamental problem of `NODE_ENV` stems from developers combining optimizations and software behavior with the environment their software is running on. The result is code like the following:
3862

3963
```js
4064
if (process.env.NODE_ENV === 'development') {
@@ -50,14 +74,5 @@ if (['production', 'staging'].includes(process.env.NODE_ENV)) {
5074
}
5175
```
5276

53-
For example, in an Express app, you can use this to set different error handlers per environment:
54-
55-
```js
56-
if (process.env.NODE_ENV === 'development') {
57-
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
58-
}
59-
60-
if (process.env.NODE_ENV === 'production') {
61-
app.use(express.errorHandler());
62-
}
63-
```
77+
While this might look harmless, it makes the production and staging environments different, thus making reliable testing impossible. For example a test and thus a functionality of your product could pass when `NODE_ENV` is set to `development` but fail when setting `NODE_ENV` to `production`.
78+
Therefore, setting `NODE_ENV` to anything but `production` is considered an _antipattern_.

0 commit comments

Comments
 (0)