Node.js: Difference between Development and Production Environments

You are a new programmer and have recently begun with Node.js and building web applications. You are frequently coming across these words, Development and Production environments, and your mind just refuses to register any of that!

Well, do not worry! I have you covered.

Today, I am discussing with you all the differences between the two environments, or situations so to say. By the end of this post, you will have had a better idea of what the two are and their differences.

What is an Environment in Programming?

An environment in programming refers to the state of a computer. It is usually determined by the programs that are running and other basic software and hardware characteristics.

It is basically the hardware and software tools a developer has access to while developing software or application.

Almost every piece of software starts with the development stage and ends with the ‘production’ stage. However, there are different software staging standards in the industry.

Since every staging environment has its own purposes and policies, it depends on the organization or the developer what all environments their applications undergo.

To list out a few:

  • Development environment
  • Beta Environment (Testing)
  • Deployment/Production Environment

Since we are interested only in the Development and Production environments, let’s talk a little about it before we can set up our code for the environments.

The Development Environment

Every time you are writing some code, you are in the development environment. This one is on your computer. Most of the work is facilitated by a Git repository. This stage is configured differently from the environment your users use your application.

And the database used is also different than that of the one that’s probably live. It might be a local database or a dummy database so you don’t mess up the real data while making changes.

None of the changes made in this environment have reflected the users when they use your application or website unless you push it to the production or beta stage.

This environment is for you and your co-workers to try and test out updates and new features. Moreover, other developers too can know how your new updates will work.

The Production Environment

Every time you talk about going live, your app moves to the production environment. Well, this is what your users see, and hence beginner developers take time to get to this stage. At organizations, where there are a few experienced developers, it doesn’t take as much time.

It is the final environment in your development process. it is going to be live publicly and hence, only the thoroughly tested updates end up here. Here is also where the company mints some money, so you cannot have crippling mistakes here.

Not all your updates need to be live at the same time. Some developers, like to roll out updates bit by bit which makes testing easier.

Telling Node.js to Switch Environments

Node.js always assumes you are in the development environment. To signal Node, you want to switch to the production environment, you can run this command in the shell.

export NODE_ENV=production

It is advisable to set up your shell configuration for example, .bash_profile for Bash shell as otherwise, the setting does not reflect after system restart.

Or, when deploying (going live), you can set up a .env file where you can set up your configurations. You can choose what resources must be used and what must not, in production or development.

if (process.env.NODE_ENV !== 'production') {
    require('dotenv').config();
}

Setting your environment to production generally ensures:

  • Minimum logging
  • More caching for optimization

If you are building an Express app, you can set up your error handlers like this:

if (process.env.NODE_ENV === "development") {
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }))
})

if (process.env.NODE_ENV === "production") {
  app.use(express.errorHandler())
})

Code snippet source: Official Node.js Docs

If you are using ejs for templating in your Express app, you can set up your error handlers on a web page as I have done:

<div class="alert alert-danger" role="alert">
        <h4 class="alert-heading">
            
        </h4>
        <p>
            
                
                    
        </p>
    </div>

Read More: Installing NodeJS Development Environment in Amazon AWS EC2

Conclusion

In this article, I have thrown some light on the difference between a Development environment and a Production environment. I have also covered how you can signal Node.js what environment you want to switch to.

Notable References

Aneesha S
Aneesha S
Articles: 175