Skip to content

NodeJS Setup for Production (work in progress)

Adrian Matei edited this page May 25, 2017 · 8 revisions

Set NODE_ENV to “production”

The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production). One of the simplest things you can do to improve performance is to set NODE_ENV to “production.”

Setting NODE_ENV to “production” makes Express:

  • Cache view templates.
  • Cache CSS files generated from CSS extensions.
  • Generate less verbose error messages.

While the first two points make no difference for us, as we serve just a back-end REST API, we still don't want verbose error messages and production, there are some environment specific configurations like Keycloak setup...

Because we have environment-specific code, we can check the value of NODE_ENV with process.env.NODE_ENV.

Be aware that checking the value of any environment variable incurs a performance penalty, and so should be done sparingly.

For the production server we want the NODE_ENV permanently set to production. In Ubuntu, the underlying server for #codingmarks, we can set a system-wide environment variable in the /etc/environment file:

$ sudo vim /etc/environment

Append the following at the end of the file:

NODE_ENV=production

This is set for the whole system, rather for just a particular user.

Now logout and login again and now we can see the system wide environment variable:

$ printenv | grep NODE_ENV
NODE_ENV=production

References

Clone this wiki locally