Dev-dependencies, nodemon & environment variables
In a node application, developers use various packages. Some of these packages are vital for applications to function and are known as application packages. While other packages installed might help speed up the development process and are known as development packages. To separate these two, development packages information is appended under dev-dependencies instead of dependencies.
Note: All packages, applications or development gets stored inside the node_modules folder.
As you saw in the previous article, after making changes to a node app, you have to stop the app by pressing
control + c and re-run the app to see changes. In the development of large applications, this can be tedious and frustrating. But fortunately, there is a package called nodemon that automatically restarts the node application when file changes in the directory are detected.
As you can see here, you only require this package while developing the app because, in the production environment, you will not make any changes and will not be in need to restart the app. So, let’s append the previous example and install this package under dev-dependencies by passing a special flag like this:
npm i nodemon --save-dev
Your package.json file should look something like this:
Instead of starting your application using
node index.js, start it with
nodemon index.js. Make any change to the index.js file, and in the terminal, you will see that the application restarted.
One might say, what difference did it make? You will see the difference when you move your code from the development environment to production and do
npm install to install all the packages.
This article will not show you how to put your app in the production environment. But teach you how to simulate the production environment using the environment variables. If you are already familiar with environment variables, you can skip the next section.
The variables available on a system/environment are environment variables. Open up the terminal and type
env. The listed output is all the environment variables available on your machine. Notice that each variable has a name and a value. Name by convention is all uppercase.
Important: Environment variables hide configuration values like API access tokens or other valuable information from users. The value of an environment variable gets declared outside of the application.
Set environment variable
Run the following command in the terminal to set node environment to production:
You can double-check that the variable got added to the environment by typing
env. Now, head over to the project folder and delete the node_modules folder (so it simulates that you cloned code from GitHub). Say
npm install to re-install all packages. Go inside the node_modules folder, and you will see no nodemon package installed because the app is in the production environment.
Unset environment variable
Run the following command in the terminal to unset the new environment variable:
Similarly, you can set the port number by saying
export PORT=3000 in the terminal, and get its value inside the index.js file using
const port = process.env.PORT.