Nodemon is not working as dev dependencies

Viewed 53

I am working on a project of nodejs and I need to install nodejs. I want to install nodemon as dev dependencies, hence I've entered npm i -D nodemon. the command install nodemon and it is visible in dev dependencies as well but when I enter nodemon -v it shows

The term 'nodemon' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

What can I do ?

2 Answers

Since you installed it only in your project and not globally, you need to put the call to nodemon in an npm script (e.g. dev) and run that instead.

In your package.json, you would have an entry "dev": "nodemon" under scripts and then use npm run dev. For the equivalent of nodemon -v you'd use npm run dev -- -v.

Alternatively you can also use npx nodemon or ./node_modules/.bin/nodemon, but the script way is usually preferred because it also documents what exact command you'd use for your dev server (you can add arguments in the script as well).

Why it happened

When you installed npm i -D nodemon you installed it in the scope of your project. That means that it is accessible through the scripts in package.json. But when you input in your terminal nodemon run dev, it isn't recognized since it isn't installed globally on your PC.

Solutions

a) Install globally:

npm install -g nodemon

b) Run from the scripts of your package.json. For example:

  "scripts": {
    "dev": "nodemon index.js"
  },

and then in your terminal"

npm run dev
Related