9

When working locally on a NodeJS project, nodemon is required in order to make the coding easier. I frequently see the cases when it's installed as a dev-dependency only, so I wonder: what is the correct approach when it comes to deployment? Should we include it as a dev-dependency only, or should we include it into the server as well?

In this project I have, I see nodemon installed as regular dependency and then in the package.json configs:

  "scripts": {
    "start": "nodemon src/app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  }, 

But I am thinking to install it as a dev-dependency only and then to rework the config like:

  "scripts": {
    "start-prod": "node src/app.js",
    "start-dev": "nodemon src/app.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  }, 

So I wonder if this will be a correct approach? I don't see a reason why on the server I would watch the file changes with nodemon, so I wonder if I got it right? In case sometimes it is needed, what are the possible cases when that will be required?

delux
  • 1,694
  • 10
  • 33
  • 64
  • 4
    You don't need nodemon in production, nodemon it's only used for development purposes. The suggestion that you added with`start-dev` / `start-prod` should be the correct way. – Nicolae Maties May 27 '21 at 09:27

2 Answers2

8

Short answer: You don't require nodemon in production.

According to nodemon on npm:

nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.

It is a tool to help in development, mainly restart the app server on file changes. You can add it to dev dependencies if you want to run nodemon via scripts. Otherwise you can install it globally.

npm install -g nodemon # or using yarn: yarn global add nodemon

And nodemon will be installed globally to your system path. You can also install nodemon as a development dependency:

npm install --save-dev nodemon # or using yarn: yarn add nodemon -D

Edit:

If you want to keep your app alive even in case of any crash, you should consider using pm2.

PM2 is a production process manager for Node.js applications with a built-in load balancer. It allows you to keep applications alive forever, to reload them without downtime and to facilitate common system admin tasks.

Mohit Yadav
  • 145
  • 1
  • 12
1

When working locally on a NodeJS project, nodemon is required in order to make the coding easier.

With the release of the --watch mode feature in Node.js 18 or higher, you can skip using nodemon and just add the --watch mode instead:

node --watch index.js
Stanley Ulili
  • 702
  • 5
  • 7