nodemon not working properly

2020-05-19 05:26发布

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

11条回答
beautiful°
2楼-- · 2020-05-19 05:50

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).

查看更多
放荡不羁爱自由
3楼-- · 2020-05-19 05:52

For Express 4; Just run

nodemon

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

查看更多
再贱就再见
4楼-- · 2020-05-19 05:54

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.

查看更多
你好瞎i
5楼-- · 2020-05-19 05:55

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

查看更多
干净又极端
6楼-- · 2020-05-19 05:55

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" }

查看更多
叼着烟拽天下
7楼-- · 2020-05-19 05:57

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

查看更多
登录 后发表回答