nodemon not working properly - node.js
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
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).
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
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
try running nodemon ./app.js 3000 or nodemon start 3000
For Express.js 4,
use nodemon
or nodemon bin/www
Add following code in your code
app.js
app.listen(3000, function(){
console.log("info",'Server is running at port : ' + 3000);
});
package.json
nodemon app.js
Then run npm start from the command line.
npm i nodemon
Edit your Package.json file:- set start to nodemon:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon server.js"
}
Run npm start to try the server
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"
}
For Express 4; Just run
nodemon
command (with out any args) on the directory; this works for me.
thanks
you need to type this after to enter in the folder application with
cd your_project_folder
sudo nodemon bin/www
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
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.
"scripts": {
"start": "nodemon app.js"
},
check it, if it's true or not
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 😀
You can directly use the command nodemon start to start your server.
Try this command on terminal if you are using Nodejs & TypeScript
npm i --dev nodemon ts-node
You might also run into the issue of having an empty .nodemonignore.
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
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
Related
Kill process started with NPM (pm2)
I installed pm2 with NPM without the global flag. Then I started the app with "npm start" that declared on package.json as "start": "pm2 start index.js" How can I stop it?
You can access locally-installed pm2 with npx: $ npx pm2 list $ npx pm2 stop 0 # first app will have ID=0 But in general, I would manage it by attaching a name to the app. Let's edit your package.json: "start": "pm2 start index.js --name my-app" "stop": "pm2 stop my-app", "logs": "pm2 logs my-app" And then: $ npm run start $ npm run logs $ npm run stop
Oh, was easy as installing it globally npm install pm2 -g and then kill the process. I thought it will be a "different" thing
keep webpack dev-server running even after terminal closes
Hi is there a way i can keep webpack devserver running even after closing terminal. "scripts": { "dev-server": "npm run templates && webpack-dev-server -d --https --port 28443", } when i run npm run dev-server it starts but after closing terminal webpack devserver also closes is there any way to keep it running with pm2 or any other method.
use nohup So if the script command is "dev-server" (according to your snippet), then go to your project root directory (where the package.json file is, which has your "scripts" section): If on unix environment, just run nohup npm run dev-server & If on windows, install git bash - it's sort of a mini unix environment for windows. And then run the nohup npm run dev-server & This will start the webpack dev server and keep it running on the background For me, my script section is "scripts": { "dev": "webpack-dev-server --open" } and the above command that I mentioned works fine
nohup did somehow not work for me, but i was able to make it work with forever. sudo npm install -g forever forever start -c "webpack serve" ./
How do I get nodemon to run a complex command that is wrapped in quotations?
Here's the situation nodemon ./app works great. nodemon "./app" does not. This results in nodemon constantly starting and restarting without ever actually running ./app. Why is this a relevant you may ask? Consider the following. webpack-dev-server --config webpack/webpack.config.js I want to run the above with nodemon but webpack-dev-server and nodemon both use the --config flag. This means that I have to place the above command in quotations resulting in the following. nodemon "webpack-dev-server --config webpack/webpack.config.js" which dunt work. Any pointers?
You can use nodemon webpack-dev-server -- --config webpack/webpack.config.js
nodeJS app, running node and webpack --watch?
brothers and sisters, I am trying to do this: "scripts": { "start": "node ./node_modules/.bin/http-server ./public", "poststart": "webpack --watch" } It is not working. The idea being node runs the app (duh) and there's a webpack --watch instance rebuilding my code as it changes. What's the answer here?
One way to do it is like this: you start webpack --watch in one terminal to watch the sources and rebuild when changes occur you use nodemon (which is a npm package) in another terminal to watch for build (NOT source) changes and restart the app Reference: https://nodemon.io/
Restart node upon changing a file
For someone who is coming from PHP background the process of killing node and starting it again after every code change, seems very tedious. Is there any flag when starting a script with node to automatically restart node when code change is saved?
A good option is Node-supervisor: npm install supervisor -g and after migrating to the root of your application use the following supervisor app.js
You should look at something like nodemon. Nodemon will watch the files in the directory in which nodemon was started, and if they change, it will automatically restart your node application. Example: nodemon ./server.js localhost 8080 or simply nodemon server
forever module has a concept of multiple node.js servers, and can start, restart, stop and list currently running servers. It can also watch for changing files and restart node as needed. Install it if you don't have it already: npm install forever -g After installing it, call the forever command: use the -w flag to watch file for changes: forever -w ./my-script.js In addition, you can watch directory and ignore patterns: forever --watch --watchDirectory ./path/to/dir --watchIgnore *.log ./start/file
Various NPM packages are available to make this task easy. For Development nodemon: most popular and actively maintained forever: second-most popular node-dev: actively maintained (as of Oct 2020) supervisor: no longer maintained For Production (with extended functionality such as clustering, remote deploy etc.) pm2: npm install -g pm2 Strong Loop Process Manager: npm install -g strongloop Comparison between Forever, pm2 and StrongLoop can be found on StrongLoop's website.
You can also try nodemon To Install Nodemon npm install -g nodemon To use Nodemon Normally we start node program like: node server.js But here you have to do like: nodemon server.js
node-dev node-dev is great alternative to both nodemon and supervisor for developers who like to get growl (or libnotify) notifications on their desktop whenever the server restarts or when there is an error or change occur in file. Installation: npm install -g node-dev Use node-dev, instead of node: node-dev app.js Notification on Changing file so server start automatically console out put
Follow the steps: npm install --save-dev nodemon Add the following two lines to "script" section of package.json: "start": "node ./bin/www", "devstart": "nodemon ./bin/www" as shown below: "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node ./bin/www", "devstart": "nodemon ./bin/www" } npm run devstart https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website
I use runjs like: runjs example.js The package is called just run npm install -g run
Nodejs supports watching mode since v18.11.0. To run it just pass --watch argument: node --watch ./index.js Note: this is an experimental feature.