You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
# Node.js, the difference between development and production
8
8
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.
10
12
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
13
21
14
22
This is usually done by executing the command
15
23
@@ -25,16 +33,32 @@ You can also apply the environment variable by prepending it to your application
25
33
NODE_ENV=production node app.js
26
34
```
27
35
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:
29
37
30
-
Setting the environment to `production` generally ensures that
- more caching levels take place to optimize performance
43
+
if (process.env.NODE_ENV==='production') {
44
+
app.use(express.errorHandler());
45
+
}
46
+
```
34
47
35
48
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.
36
49
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:
38
62
39
63
```js
40
64
if (process.env.NODE_ENV==='development') {
@@ -50,14 +74,5 @@ if (['production', 'staging'].includes(process.env.NODE_ENV)) {
50
74
}
51
75
```
52
76
53
-
For example, in an Express app, you can use this to set different error handlers per environment:
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