6

I am working with npm on a web app and I found an issue when using some packages that requires terminal commands to run such like nodemon and concurrently

I installed it via

sudo npm install --save-dev nodemon

and when I try to use it via:

nodemon ./server.js

I get an error

nodemon command not found

and the same when I used concurrently

I tried also with

sudo npm install --save nodemon 

and it doesn't work.

it only work if I installed it globally

sudo npm install -g nodemon

Why I can't use it when install locally?

Note: I can found the executable file at node_modules/.bin

but this following not working as well

node_modules/.bin/nodemon ./server.js
Peter Wilson
  • 4,150
  • 3
  • 35
  • 62
  • Possible duplicate of [How to use package installed locally in node\_modules?](http://stackoverflow.com/questions/9679932/how-to-use-package-installed-locally-in-node-modules) – heylookltsme Apr 23 '17 at 12:59

4 Answers4

13

Global packages can be launched directly because they are saved in your PATH directory by default. If you saved a package locally you can see it on node_modules/.bin/ as you mentioned. So there are 2 ways to achieve what you want if you want to run an executable package if installed locally:

  • You can run it via terminal as ./node_modules/.bin/nodemon yourscript.js
  • Or via npm scripts in your package.json file, you do this:

    {
      "scripts": {
        "nodemon": "nodemon yourscript.js"
      }
    }  
    

    and execute npm run nodemon.

The 2nd approach works for both packages installed globally or locally.

I prefer installing packages locally, so my other apps won't get affected especially if I'm using different package versions per project.

UPDATE

On npm@5.2.0 onwards, it comes with a binary called npx. So you can run specific packages on the terminal just by npx [package] and it executes either your local or global npm package. In your case it should be something like npx nodemon server.js.

RobC
  • 22,977
  • 20
  • 73
  • 80
JohnnyQ
  • 4,839
  • 6
  • 47
  • 65
  • @PeterWilson try adding `./` before `node_modules` Example: `./node_modules/.bin/nodemon server.js` you don't have to put `./` on your `server.js` script. – JohnnyQ Apr 23 '17 at 13:13
  • still command not found .. the main issue is to access the `nodemon` command itself not in the path of my script file – Peter Wilson Apr 23 '17 at 13:14
  • @PeterWilson Did you make sure you have `nodemon` installed locally? try `npm ls --depth=0` and see if `nodemon` is listed. Otherwise you have to install it `npm install --save-dev nodemon` – JohnnyQ Apr 23 '17 at 13:17
  • yes its installed correctly and I tried your command and I found it – Peter Wilson Apr 23 '17 at 13:18
  • @PeterWilson any particular reason why you are using `sudo` when installing the package? try uninstalling `nodemon` and then installing the package again without `sudo` and see if it can be found. – JohnnyQ Apr 23 '17 at 13:22
  • I appreciate your effort, but unfortunately it didn't work too – Peter Wilson Apr 23 '17 at 13:25
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/142400/discussion-between-johnnyq-and-peter-wilson). – JohnnyQ Apr 23 '17 at 13:29
4

Because it's in your node_modules/.bin folder, not your PATH.

You can either use ./node_modules/.bin/nodemon or $(npm bin)/nodemon to call nodemon.

Tonni
  • 94
  • 2
  • 7
1

To run any locally installed npm module (Mocha, Eslint, Nodemon, etc.), you can now use npx. Try npx nodemon server.js.

enter image description here

I also recommend setting main within your package.json to point to the script you want to run (index.js by default), so you could just run npx nodemon or nodemon (if globally installed) and it will know which script to run.

JBallin
  • 8,481
  • 4
  • 46
  • 51
-1

This is because the local node_modules folder is not in your PATH. See the link to the duplicate question for more details.

heylookltsme
  • 124
  • 6