36

I am running my nodejs app by npm start

I just installed nodemon by sudo npm install -g nodemon so that i can get my server restarted when i save changes to files.

But when i try to start the server, something like this

nodemon ./app.js localhost 3000 or nodemon start localhost 3000

I get this as output

LM-SJC-00871929:webapp gdeep$ nodemon ./app.js localhost 3000
28 May 23:34:30 - [nodemon] v1.1.1
28 May 23:34:30 - [nodemon] to restart at any time, enter `rs`
28 May 23:34:30 - [nodemon] watching: *.*
28 May 23:34:30 - [nodemon] starting `node ./app.js localhost 3000`

but when i go to my webpage, i get

Oops! Google Chrome could not connect to localhost:3000. What am i doing wrong?

App.js here http://collabedit.com/t35dy

johnnyRose
  • 7,310
  • 17
  • 40
  • 61
bruceparker
  • 1,235
  • 1
  • 17
  • 33

20 Answers20

55

You're running express 4, which has the app.listen call in a different file than app.js. The command you're looking for is nodemon bin/www (localhost and 3000 are not needed in this scenario).

In fact, you can even run nodemon with no args, and it'll read what command needs to be run from scripts.start in package.json (which express generates automatically).

SomeKittens
  • 38,868
  • 19
  • 114
  • 143
  • 3
    If you try to run the express app with just node and not nodemon, you'll get the same error. Since express 4, the script that runs the server is actually in bin/www (note there's no .js extension). This www file reads your app.js *config* and runs the server. – Remy Sharp May 29 '14 at 21:12
  • 2
    Also worth adding that you can run express 4 based apps just using `nodemon` with no args, because it'll red from the package.json file and run the `scripts.start` option. – Remy Sharp May 29 '14 at 21:14
23

Here's what I did to make nodemon update correctly:

nodemon index.js -L

The -L flag stands for legacyWatch, here's an explanation from the official doc:

In some networked environments (such as a container running nodemon reading across a mounted drive), you will need to use the legacyWatch: true which enables Chokidar's polling.

https://www.npmjs.com/package/nodemon#application-isnt-restarting

Skoua
  • 3,373
  • 3
  • 38
  • 51
10

In my case, I had to install nodemon globally. Use this command to do so..

npm install -g nodemon

If you're using Linux you may need to prefix the command with the sudo keyword for administration access..

sudo npm install -g nodemon
Garth
  • 3,237
  • 2
  • 18
  • 28
8

try running nodemon ./app.js 3000 or nodemon start 3000

user3607134
  • 108
  • 1
  • 7
5

For Express.js 4,
use nodemon
or
nodemon bin/www

Dinesh
  • 499
  • 5
  • 6
5

Add following code in your code

  1. app.js

    app.listen(3000, function(){
        console.log("info",'Server is running at port : ' + 3000);
    });
    
  2. package.json

    nodemon app.js 
    

Then run npm start from the command line.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
BHUVNESH KUMAR
  • 391
  • 4
  • 15
5
  1. npm i nodemon
  2. Edit your Package.json file:- set start to nodemon:
"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon server.js"
}
  1. Run npm start to try the server
mousetail
  • 7,009
  • 4
  • 25
  • 45
ujjwal jain
  • 61
  • 1
  • 3
2

If you are using express4, the easiest way is to navigate to package.json and change "scripts": { "start": "node ./bin/www" }

to "scripts": { "start": "nodemon ./bin/www" }

Yue Yin
  • 166
  • 4
0

For Express 4; Just run

nodemon

command (with out any args) on the directory; this works for me.

Jackal
  • 2,591
  • 3
  • 25
  • 29
0

thanks you need to type this after to enter in the folder application with

cd your_project_folder

sudo nodemon bin/www

0

Certain child processes related to your parent process may not be closed. Try to kill all the child processes.

Ref: https://github.com/remy/pstree

0

Use single quotation for multi-value args like `--exec'.

e.g. I changed "nodemon --exec yarn build-langs" to "nodemon --exec 'yarn build-langs'" and worked.

Masih Jahangiri
  • 9,489
  • 3
  • 45
  • 51
0
 "scripts": {
    "start": "nodemon app.js"
  },

check it, if it's true or not

hilmidemirtas
  • 40
  • 1
  • 6
0

Try this:

First install nodemon by npm install nodemon or any command from the official website (https://www.npmjs.com/package/nodemon) Then start your nodemon by npx nodemon filename.js or if you have your package.json with you and the entry point is the file you would like to open then you can also go with the command npx nodemon Check this once

Hope it works!!

Happy Coding

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

You can directly use the command nodemon start to start your server.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

Try this command on terminal if you are using Nodejs & TypeScript

npm i --dev nodemon ts-node
Khaled Ayed
  • 1,121
  • 3
  • 12
  • 29
0

If the solutions don't work for you and you have Node.js 18 or higher installed, you no longer need to use nodemon. Instead, you can pass the --watch flag and Node will automatically restart the server for you:

node --watch index.js

Note: the --watch feature is experimental.

Stanley Ulili
  • 702
  • 5
  • 7
-1

You might also run into the issue of having an empty .nodemonignore.

czerasz
  • 13,682
  • 9
  • 53
  • 63
-1

try

npm install --save-dev nodemon

and then in package.json file keep like this

"scripts": {
    "start": "nodemon",
    "test": "echo \"Error: no test specified\" && exit 1"
  },

instead of npx nodemon, which takes more time

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • There are other answers and a accepted one that provide the OP's question, and they were posted some time ago. When posting an answer [see: How do I write a good answer?](https://stackoverflow.com/help/how-to-answer), please make sure you add either a new solution, or a substantially better explanation, especially when answering older questions. – help-info.de Nov 02 '19 at 18:01
-1

First of all, uninstall nodemon:

npm uninstall nodemon

After uninstalling the nodemon then install nodemon globally using:

npm i -g nodemon

It will install nodemon globally then run the following command:

nodemon index.js or app.js

nima
  • 7,796
  • 12
  • 36
  • 53