Nodemon is not working : [nodemon] restarting due to changes - node.js

I use nodemon as a dev-dependency in node. Suddenly, in CLI it shows:
[nodemon] restarting due to changes...
when I am changing the code. Then I have globally installed nodemon but it still isn't working. It is not working on any project-- not a single project.
Any help? I have attached a screenshot of my CLI:

It's probably because starting updates a file that the server is watching, therefore triggering a restart event in an infinite loop.
Nodemon supports the --watch and --ignore commands. Play around with them to only watch a certain set of files. Example:
# Only watch the "server" directory
nodemon --watch server server/main.js
# Watch both the "server" and "client" directory
nodemon --watch server --watch client server/main.js
# Watch the "server" directory, but ignore "server/logs"
nodemon --watch server --ignore server/logs server/main.js

Related

Error: connect ECONNREFUSED 127.0.0.1:3000 when starting my app with nodemon

I'm very new to Node.js and I installed nodemon globally, but while when I start my app with npm start command in terminal I do get the message:
vinnytwice#Vinnys-iMac fixit_server_node % npm start
> fixit_server_node#1.0.0 start
> node server.js
App executing to port 3000
while if I use the command nodemon start in terminal I get:
vinnytwice#Vinnys-iMac fixit_server_node % nodemon start
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node start index.js`
[nodemon] clean exit - waiting for changes before restart
not showing any execution on any port, and in fact when hitting an endpoint I get Error: connect ECONNREFUSED 127.0.0.1:3000.
If instead I start it with the command nodemon server.js then it does work.
Why npm start fired the server.js file and nodemon start doesn't? Is there something I shaven't set properly?
Notice this line
starting `node start index.js`
The output of your command nodemon start states that the file index.js is executed instead of server.js. You need to check your package.json file to see what is the main file of the program.
From the npm page of nodemon here
If you have a package.json file for your app, you can omit the main
script entirely and nodemon will read the package.json for the main
property and use that value as the app.
nodemon will also search for the scripts.start property in
package.json (as of nodemon 1.1.x).
nodemon isn't a replacement of npm, they are totally different.
You can crudely think of nodemon being a drop in for node.
nodemon is a replacement wrapper for node. To use nodemon, replace the word node on the command line when executing your script.
Source: https://www.npmjs.com/package/nodemon
Hence if your npm script has,
"start": "node server.js"
Add another npm script,
"dev": "nodemon server.js"
You would want to use npm start in production environment and use npm run dev in development environment
You are seeing the ECONNREFUSED error because in your package.json there is a field,
"main": "index.js"
when you execute nodemon start, nodemon picks up this value and it actually runs node start index.js as shown in your logs.
If no script is given, nodemon will test for a package.json file and if found, will run the file associated with the main property
Source: https://www.npmjs.com/package/nodemon
Your index.js doesn't setup the server and hence when you try to access 127.0.0.1:3000 to hit your endpoints nothing is running there and hence you get ECONNREFUSED error

Why does the node inspector not start when I am using nodemon and ts-node?

I have a simple node server written in typescript. My package.json is configured as:
"scripts": {
"build": "tsc",
"dev": "nodemon --watch src/**/* -e ts,json --exec ts-node ./src/server.ts",
"debug": "nodemon --verbose --watch src/**/* -e ts,json --exec ts-node --inspect ./src/server.ts"
},
When I run npm run dev nodemon will launch the server and restart it when any changes are made.
[02/28/18 20:45:53] npm run dev
> pq-api#1.0.0 dev C:\Users\joe\pq\pq-api
> nodemon --watch src/**/* -e ts,json --exec ts-node ./src/server.ts
[nodemon] 1.15.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: src/**/*
[nodemon] starting `ts-node ./src/server.ts`
initializing config to development
info: PQ-API running on port 3000
However, when I run npm run debug (so I can attach a debugger) It looks like it begins to start, but just hangs forever
[02/28/18 20:39:30] npm run debug
> pq-api#1.0.0 debug C:\Users\joe\pq\pq-api
> nodemon --verbose --watch src/**/* -e ts,json --exec ts-node --inspect ./src/server.ts
[nodemon] 1.15.1
[nodemon] to restart at any time, enter `rs`
[nodemon] or send SIGHUP to 10156 to restart
[nodemon] watching: src/**/*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node --inspect ./src/server.ts`
[nodemon] spawning
[nodemon] child pid: 13344
[nodemon] watching 12 files
That is all the output has. The script is never executed; the server never starts up, and the inspector is never available to connect to.
node 8.94
nodemon 1.15.1
ts-node 5.0.0
typescript 2.7.2
With ts-node 5.0.0 you no longer pass the --inspect flag the same way. The suggested way is node --inspect -r ts-node/register path/to/ts. For example:
nodemon --watch src/**/* -e ts,json --exec node --inspect-brk -r ts-node/register src/app.ts
see https://github.com/TypeStrong/ts-node/issues/537
Provide location and port to the inspect option like:
--inspect=0.0.0.0:9200
I am using PHP Storm and the previous answer of #user60456 works like a charm for me.
With some changes, I was able to run debug mode using dotenv with multiple env files in PHP Storm as well.
package.json
"start:dev": "nodemon --watch src/**/* -e ts,json --exec node --inspect-brk -r ts-node/register -r dotenv/config local.ts dotenv_config_path=./.env.development",
where local.ts is the file where my app.listen() is.
Then, I had to create a new Run configuration (Attach to Node.js/Chrome) with:
host: localhost
port: 9229
Attach to: Chrome or Node.js > 6.3 started with --inspect
Then select the root folder in Remote URLs of local files tab and set the Remote URL to /usr/src/app.
Now, you can run npm run start:dev. You will see the console output:
Debugger listening on ws://127.0.0.1:9229/...
You have to run the Nodej.js debug configuration now and wait, until you see the console output:
Debugger attached.
The application is now running in debug mode.
I made changes using some of the info above as mine just didn't seem to work and the change I made did resolve the problem.
From "start": "tsnd --inspect -- src/app.local.ts"
To: "start": "node --inspect -r ts-node/register src/app.local.ts"
I just fixed this problem by writing nodemon.json file like this :
{
"restartable": "rs",
"ignore": [".git", "node_modules/**/node_modules"],
"verbose": true,
"execMap": { // [A]
"ts": "node --require ts-node/register"
},
"watch": ["src/"],
"env": {
"NODE_ENV": "development"
},
"ext": "js,json,ts"
}
ref : https://dev.to/oieduardorabelo/nodejs-with-typescript-debug-inside-vscode-and-nodemon-23o7
I had a different issue resulting in the debugger never running. I was running this command,
node index.js --inspect=5005
instead of this,
node --inspect=5005 index.js
The flag is supposed to be before the source file.

(nodemon) app crashed - waiting for file changes before starting

I cloned this repo from gitHub so, while installing in the last step I try to run this command:
~/mqtt-gateway$ WEB_API_URL=http://localhost:3000 npm start
And I get this error:
[nodemon] app crashed - waiting for file changes before starting...
I've read that possibly is because of a process that haven't been terminated but I've been unable to solve it.
Hope you can help. Thank you ;)
i had that issue myself, when i was building a server .. on command npm run dev:server, it was always rejecting me with that message "[nodemon] app crashed - waiting for file changes before starting"
my package.json was:
"scripts": {
"dev:server": "nodemon --watch build --exec node \"build/bundle.js\"",
"dev:build:server": "webpack --config webpack.server.js --watch"
},
so i just removed those escaping backslashes and everything worked fine.
"scripts": {
"dev:server": "nodemon --watch build --exec node build/bundle.js",
"dev:build:server": "webpack --config webpack.server.js --watch"
}
In the error output it said:
Error: listen EADDRINUSE :::1883
The port used for this program is 1883 and I was running mosquitto on that same port so I killed it:
netstat -plten |grep mosquitto
and then
kill -9 PID
like in this question.
Just in case anyone faces a similar problem.

Running Nodemon on IBM Power System i

I am new to node development on power system i, was trying to get nodemon to work but errors out with following:
# nodemon.js
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node server.js`
[nodemon] Internal watch failed: watch ENOSYS
My application uses express, ejs and body-parser, works well outside of nodemon. First off, I couldn't get nodemon to be installed globally so I put that on my application node_modules directory and set the path, which is not what I usually do in other environments. How does npm install -g suppose to work on system i?
Any help on this greatly appreciated!
Thanks!
My environment is:
(node: v6.9.1)
(npm:3.10.8)
(OS: V7R1)
Path: /QOpenSys/QIBM/ProdData/OPS/Node6/bin:/QOpenSys/usr/bin:/usr/ccs/bin:/QOpenSys/usr/bin/X11:/usr/sbin:.:/usr/bin:/home/QSECOFR/projects/mytasklist/node_modules/nodemon/bin
LIBPath: /QOpenSys/QIBM/ProdData/OPS/Node6/bin:/home/QSECOFR/projects/mytasklist/node_modules/nodemon/bin
After days of searching, I think I found a solution.
I wasn't able to run directly Nodemon by typing
nodemon src/app.js
So on Node14 I found a workaround, adding a script line in package.json
... ,
"main": "./src/app.js",
"scripts": {
...,
"serve": "nodemon -L"
},
Then run your application by typing
npm run serve
And voilĂ  !
It seems to run in legacy mode.
Note : In node 10 this worked too (but not in node 14) even if my main file is src/app.js
"serve": "nodemon server.js"

nodemon recursive watch issue?

I have installed nodemon using command:
npm install nodemon -g
Having done that I changed "start": "node ./bin/www" to "start": "nodemon ./bin/www"
Output in Console on running npm start :
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `nodemon ./bin/www ./bin/www`
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `nodemon ./bin/www ./bin/www ./bin/www`
[nodemon] 1.11.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `nodemon ./bin/www ./bin/www ./bin/www ./bin/www`
and so on....
Where am I going wrong? Please bear in mind that this is my 3rd day on nodejs so keep it simple.
Edit
"scripts": {
"start": "nodemon app.js"
},
"nodemon app.js" seems to work since the console does not show any errors but then I am unable to run the application. However if I change it back to node ./bin/www it would work.
Try to downgrade requirements.
This helped me.
npm install nodemon#1.0.0 -g
I reproduced this issue in docker image (node:alpine) with nodemon >1.2.0 100%. And v1.1.0 and v1.2.0 have some strange issues too.
I checked only minor releases (w/o checking patchlevels e.g. 1.2.1, 1.2.2 and others)
So. 1.0.0 is old, but works well.
On my host Mac I have nodemon#1.11.0 installed globally.
Sometimes I have this issue and sometimes I don't.
This is somehow connected with pwd and nodemon.json file. But I'm not sure.
I found a bug report related to this problem.
You are watching a file that doesn't exist and this will cause an infinite loop.
In your case, you are running ./bin/www and looking at you IDE screenshot you have a ./bin/www.js
That's why nodemon app.js doesn't error. Although I can't guaranty it's going to work try nodemon ./bin/www.js
Then, without looking at the code, I'm not sure why you can't connect your application that way.
For more info check #matt answer and nodemon docs.
You don't need to tell nodemon what folder to watch. You only need to tell it to run your main application and it will automatically watch all the nested folders and files.
eg. nodemon app.js (if app.js is your application)
Also. You can optionally create a nodemon.json file in the main body of your application to house config information for nodemon. Not really related to your queestion. But good to have :)
Here is what mine looks like:
{
"ignore": ["data/*.json", "/node_modules/", "README.md"]
}
Try with this :
npm i -D nodemon
in package.json add:
"scripts": {
"start": "nodemon index.js",
}
This executes the file index.js that is in the root of your app node.
For example:
app/
node_modules
index.js
try again:
npm start

Resources