Difference between Development and Production Environments in Node.js

When you interact with the language documentation, learn about error handling, or configure applications, you may come across the terms “development” and “production” environments. And you must be wondering what exactly is this environment and what its use is, how can we switch the environment. To answer all these questions, here comes this article.

Knowing what these environments are, especially in the field of website and software development, is very important when you are planning to take your application live. In this article, we will understand the difference between these environments, and how to switch between them.

What is an Environment in Programming?

An environment in programming refers to the context or configuration of a system. It is usually determined by the programs that are running and other basic software and hardware characteristics. The environment encloses the various tools, resources and functionality 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, the software development life cycle requires more than just separate development, testing, and production environments.

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

Here are some common environments:

  • Development environment
  • Beta or Testing Environment
  • Staging Environment
  • Deployment/Production Environment
  • Integration and Continuous Integration Environment
  • User Acceptance Testing Environment

Since not all of these environments are so important to know and can be confusing for beginners, we will only recommend and talk about two essential environments: development and production environments in this article. Let’s look at them one by one.

The Development Environment

Every time you are writing some code, you are in the development environment. The development environment is your local computer environment. 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 has reflected the users when they use your application or website unless you push it to the production or beta stage.

This is your and your team’s environment for the development and testing of 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 end 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.

The production environment refers to the final stage 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 for quality assurance. Some developers, like to roll out updates bit by bit which makes testing easier.

Switching Between Development and Production Environments in Node.js

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.

Configurations for Deployment

When deploying (making the server 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

Error Handling

Error handling and Debugging bugs are important here, ensuring that the application responds properly when the environment changes.

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());
}

If you are using EJS for templating in your Express app, you can also set up your error handlers on a web page like this:

<div class="alert alert-danger" role="alert">
    <h4 class="alert-heading">
        <!-- heading here -->
    </h4>
    <p>
        <!-- additional details here -->
    </p>
</div>

Read More: Installing NodeJS Development Environment in Amazon AWS EC2

Frequently Asked Questions (FAQs)

What is the difference between production and dev build?

A production build is the final version of an application. It is created after all testing has been completed to ensure that it is free of issues and bugs. A dev build, on the other hand, is an ongoing version of the application that is used to test new features and fix bugs and errors. Once a dev build is successful and stable, it is then transferred to production.

What does production mean in development?

While developing an application, you are in a development environment to produce the application, test it, add new features, remove bad features, etc. But once all of that is done, you put your application in production, which means the changes you have made in a development environment are now usable by end users.

Why separate dev and prod?

Suppose there is no dev environment and you create a functionality and push it to the production environment. Later, you decide to make some changes in that and then push it to production again, then at some point, you want to remove it, it might be annoying for users who use the prod environment as it can disturb their overall experience. That’s where the necessity of separate dev and prod comes in. You can make changes or remove the code in the dev environment until you’re satisfied with it, and then only push it to the prod environment.

What is the dev vs test vs prod environment?

Dev is the development environment for creating new features, the test is the testing environment for testing those features for any issues or bugs, and the prod is the production environment to which users have access to enjoy the newly created features.

What is stage vs production?

The stage is considered a mirror of the production environment that is only limited to some people for testing features before finally putting them live.

Is development part of production?

No, the development is a local environment, usually within an organisation, and is separate from the production environment. Its effects are not directly affect what end users experience.

Conclusion

In this article, we have taken a quick look at the development vs production environment. The production environment is not used by beginners yet it is important to learn and be aware of it. We have also seen how to switch environments in Node.js and take the necessary steps for error handling for a smooth transition. We hope you enjoy reading the content.

Reference

https://nodejs.dev/en/learn/nodejs-the-difference-between-development-and-production

Aneesha S
Aneesha S
Articles: 172