From 37a153b672ca0a712cc845df616139103907fb2c Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 13 Aug 2024 08:56:12 +0200 Subject: [PATCH 1/4] 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 --- ...ence-between-development-and-production.md | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md b/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md index 86ccdf80ab3bf..c7fcf8228ce80 100644 --- a/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md +++ b/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md @@ -1,15 +1,23 @@ --- title: Node.js, the difference between development and production layout: learn -authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, RenanTKN +authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, RenanTKN, mcollina --- # Node.js, the difference between development and production -You can have different configurations for production and development environments. +__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. +However, a few libraries in the npm registry recognize using the `NODE_ENV` variable and default it to a `development` setting. +Always run your Node.js with the `NODE_ENV=production` set. -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. +A popular way of configuring your application is by using the [twelve factor methodology](https://12factor.net/). + +## NODE_ENV in Express + +In the wildly popular [express](https://expressjs.com/) framework, setting the `NODE_ENV` to `production` generally ensures that: + +- logging is kept to a minimum, essential level +- more caching levels take place to optimize performance This is usually done by executing the command @@ -25,16 +33,32 @@ You can also apply the environment variable by prepending it to your application NODE_ENV=production node app.js ``` -This environment variable is a convention that is widely used in external libraries as well. +For example, in an Express app, you can use this to set different error handlers per environment: -Setting the environment to `production` generally ensures that +```js +if (process.env.NODE_ENV === 'development') { + app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); +} -- logging is kept to a minimum, essential level -- more caching levels take place to optimize performance +if (process.env.NODE_ENV === 'production') { + app.use(express.errorHandler()); +} +``` 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. -You can use conditional statements to execute code in different environments: +__This environment variable is a convention widely used in external libraries, but not within Node.js itself__. + +## Why is NODE_ENV considered an antipattern? + +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: + +* Development +* Testing +* Staging +* Production + +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: ```js if (process.env.NODE_ENV === 'development') { @@ -50,14 +74,5 @@ if (['production', 'staging'].includes(process.env.NODE_ENV)) { } ``` -For example, in an Express app, you can use this to set different error handlers per environment: - -```js -if (process.env.NODE_ENV === 'development') { - app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); -} - -if (process.env.NODE_ENV === 'production') { - app.use(express.errorHandler()); -} -``` +While this might look harmless, it makes the production and stanging environments different, making testing impossible. +Therefore, setting `NODE_ENV` to anything but `production` is considered an _antipattern_. From 9ed71ecd8efbb51a9d08c54343b16bbcb4427619 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 13 Aug 2024 09:10:50 +0200 Subject: [PATCH 2/4] Update apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md Co-authored-by: Aras Abbasi Signed-off-by: Matteo Collina --- .../nodejs-the-difference-between-development-and-production.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md b/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md index c7fcf8228ce80..2a65cb3d800c7 100644 --- a/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md +++ b/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md @@ -74,5 +74,5 @@ if (['production', 'staging'].includes(process.env.NODE_ENV)) { } ``` -While this might look harmless, it makes the production and stanging environments different, making testing impossible. +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`. Therefore, setting `NODE_ENV` to anything but `production` is considered an _antipattern_. From 130d2bdca58a99ce1ff435645427c84e1c4d8c5f Mon Sep 17 00:00:00 2001 From: Brian Muenzenmeyer Date: Wed, 14 Aug 2024 21:45:32 -0500 Subject: [PATCH 3/4] Update apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md Signed-off-by: Brian Muenzenmeyer --- .../nodejs-the-difference-between-development-and-production.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md b/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md index 2a65cb3d800c7..283a025b36d10 100644 --- a/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md +++ b/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md @@ -74,5 +74,5 @@ if (['production', 'staging'].includes(process.env.NODE_ENV)) { } ``` -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`. +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`. Therefore, setting `NODE_ENV` to anything but `production` is considered an _antipattern_. From 44e8d50dcf5cbe06f7d94fc90cc3cc7d3f743bc6 Mon Sep 17 00:00:00 2001 From: Brian Muenzenmeyer Date: Wed, 14 Aug 2024 21:55:23 -0500 Subject: [PATCH 4/4] fix: address content lints --- ...-difference-between-development-and-production.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md b/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md index 283a025b36d10..2d815be587a9a 100644 --- a/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md +++ b/apps/site/pages/en/learn/getting-started/nodejs-the-difference-between-development-and-production.md @@ -6,7 +6,7 @@ authors: flaviocopes, MylesBorins, fhemberger, LaRuaNa, ahmadawais, RenanTKN, mc # Node.js, the difference between development and production -__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. +**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. However, a few libraries in the npm registry recognize using the `NODE_ENV` variable and default it to a `development` setting. Always run your Node.js with the `NODE_ENV=production` set. @@ -47,16 +47,16 @@ if (process.env.NODE_ENV === 'production') { 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. -__This environment variable is a convention widely used in external libraries, but not within Node.js itself__. +**This environment variable is a convention widely used in external libraries, but not within Node.js itself**. ## Why is NODE_ENV considered an antipattern? 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: -* Development -* Testing -* Staging -* Production +- Development +- Testing +- Staging +- Production 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: