nodemon recursive watch issue? - node.js

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

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 nodemon fail when using -- exec babel-node?

I have installed the bable-cli and the babel-preset-env, after installing nodemon the npm start fail to load my application when using the following in the package.json file:
"scripts": {
"start": "nodemon src/index.js --exec babel-node"
}
Showing the following error:
[nodemon] 2.0.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `babel-node src/index.js`
'"node"' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...
But everything works fine when using the following:
"scripts": {
"start": "babel-node src/index.js"
}
Thanks!
I ran into the same problem and solved it this way:
"scripts": {
"start": "babel-node src/index.js"
"dev" : "nodemon --exec npm start"
}
The reason is because you are not sending anything to babel to transpire and run. nodemon just looks for file changes.
Try this instead.
"serve":nodemon --exec babel-node src/index.js
If that doesn't work make sure you have the proper DEV dependencies.
#babel/core
#babel/node
#babel/cli
#babel/preset-env
configure .babelrc to have the following
{
"presets" : ["#babel/preset-env"]
}

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.

single quotes not working in package.json

I'd like to get my package.json to be able to run using the command npm run test-watch on Windows 10 with npm 5.5.1. In my package.json:
"scripts": {
"test": "mocha server/**/*.test.js",
"test-watch": "nodemon --exec 'npm test'"
}
However, I this interpretes the code strangely to have a single quote in there. I'm actually following a Udemy course so it appears to work for the instructor. However, here is the output I get:
PS D:\courses\node-course\node-todo-api> npm run test-watch
> todo-api#1.0.0 test-watch D:\courses\node-course\node-todo-api
> nodemon --exec 'npm test'
[nodemon] 1.14.7
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `'npm test'`
''npm' is not recognized as an internal or external command,
operable program or batch file.
[nodemon] app crashed - waiting for file changes before starting...
What do I need to change to get this to work? It appears to be keeping the quotes on the string. I can't seem to get around it though. When I run the command directly, it works:
PS D:\courses\node-course\node-todo-api> nodemon --exec 'npm test'
[nodemon] 1.12.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `npm test`
> todo-api#1.0.0 test D:\courses\node-course\node-todo-api
> mocha server/**/*.test.js
started on port 3000
Post /todos
√ should create a new todo (50ms)
1 passing (1s)
Unfortunately, the operating system and shell can cause a massive headache when using npm. Some things work on one computer and some on another.
Both of these should work on Windows 10 though:
"test-watch": "nodemon --exec \"npm test\""
"test-watch": "nodemon --exec npm test"

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"

Resources