I use nodemon to run a node.js app during development. When I save a file in the project the app restarts.
The problem is sometime the previous process on that post does not allow the app to restart.
My start script in my package.json looks like this:
"scripts": {
"start": "nodemon app.js",
"test": .....
},
When that happens I run this in terminal:
kill -9 $(lsof -t -i:4080)
Then the app works fine again.
How do I force nodemon to not try to restart until the previous process stopped and the ports is available again?
You can create an npm script that runs the kill process:
"dev": "kill -9 $(lsof -t -i:4080) && node app.js",
"start": "nodemon --exec npm run dev"
Notice in how your nodemon script you're running another script with --exec.
Start the server like you usually would:
npm start
Related
I'm using nodemon to manage a node.js process, and I would that process to auto-restart every 24h. Is there an elegant solution for this? I could also just create a cron job that runs every 24h and just do a kill but I wanted something nicer :) thanks!
Nodemon isn't the best tool to manage the node processes in production. It will not restart the app in case of a crash or provide you additional tools and analytics. You can user forever or pm2
pm2 start app.js --cron-restart="0 0 * * *"
Pm2 will auto restart your app in case of a crash and with --cron-restart it will restart at a given cron time.
blow script add yo package.json file
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"dev":"nodemon index.js"
}
and run this command:
npm run dev
For the life of me I can't get Browser Sync and Nodemon to run nicely alongside my Express server. I have tried every combination I can imagine.
My Express server runs at port 5000 which I can open and view, nodemon runs when changes are made, great but the browser still doesn't 'hot refresh' so to speak. I would like for the browser window to either refresh or open a new tab after nodemon has restarted the server.
package.json scripts
"scripts": {
"start": "node app.js",
"dev": "set NODE_ENV=DEV&& nodemon app.js 5000 browser-sync start --proxy localhost:5000 -e * --ignore=node_modules --reload-delay 10 --no-ui --no-notify",
"ui": "node app.js browser-sync start nodemon app.js --port=5001 --proxy=localhost:5000 -e * --ignore=node_modules --reload-delay 10 --no-ui --no-notify",
"ui2": "nodemon start & browser-sync start --proxy 'localhost:5000' -e * --ignore=node_modules"
},
I just want to start my express server, listen for changes with nodemon then restart, then either reload browser window or launch a new one to see the changes. Please help me understand what I am missing?
In case someone has the same problem, the aha moment was when I realized I needed to open a 2nd terminal window to run browser-sync. And using the below scripts in package.json now works beautifully!
So first run npm run start/dev depending if you want server restarts on changes or not then open a 2nd terminal window and run npm run ui`. In my case my app.js is launching on port 8000.
package.json
"scripts": {
"start": "node app.js",
"dev": "nodemon -e * app.js",
"ui": "browser-sync start --proxy localhost:8000 --files=**/* --ignore=node_modules --reload-delay 10000 --no-ui --no-inject-changes"
},
app.js - const port = process.env.PORT || '8000';
This can be run (npm start) in parallel with modules "npm-run-all" or with "concurrently" (replace start: string with start_b string).
"scripts": {
"browsersync": "browser-sync start --proxy localhost:5000 --files 'public,views'",
"nodemon": "nodemon server.js",
"start_b": "concurrently --kill-others \"npm run nodemon\" \"npm run browsersync\" ",
"start": "npm-run-all -p nodemon browsersync"
},
In my system, I had problems tracking .njk files, because the browser reloaded after changing them but without actually updating changes. I had to use a nodemon.json file to add the folder (views/) and extension .njk:
{
"watch": ["server.js", "views/"],
"ext": "js, css, njk"
}
Trying to streamline my package.json and local development with a custom script to run Nodemon. I'm currently building an app with a front and back end I need to call mongod before start and before my custom in two tabs however I'm running into an issue.
mongod will only run in the terminal if the terminal path is set to local from testing and I've read:
Correct way of starting mongodb and express?
npm starts to execute many prestart scripts
How to npm start at a different directory
How do I add a custom script to my package.json file that runs a javascript file?
I can use prestart as:
"scripts": {
"prestart": "cd && mongod",
"start": "node app",
"nodemon": "./node_modules/.bin/nodemon app"
}
but I'm not seeing how I should chain a prestart with a custom scripts. When I try to chain it with nodemon as:
"scripts": {
"prestart": "cd && mongod",
"start": "node app",
"nodemon": "cd && mongod && ./node_modules/.bin/nodemon app"
},
Nodemon is fired first than mongodb crashes in my package.json when I call Nodemon as:
npm run nodemon
How can I start mongod before starting nodemon in my development process through one command in the package.json?
How could I run node server.js -p by pm2?
Scripts of my package.json is like below,
"scripts": {
"dev": "node server.js",
"start": "node server.js -p"
},
When I execute npm start everything work truly. But I want to run this command with pm2.
To do it when I run pm2 start npm -- start, the process will add to the list of the pm2 but my app not run!
the correct command is
pm2 start server.js
or if you want to pass -p to your app and a name
pm2 start server.js --name "my-server" -- -p
I've got the following for spawning multiple node processes when user npm starts:
"scripts": {
"start": "node calculator.addition/index.js & node calculator.division/index.js & node calculator.multiply/index.js & node calculator.subtract/index.js &"
}
This works fine to start multiple node processes but when I command/ctrl c, the processes are still alive. Any ideas how I can correctly exit these processes?
You can also use pm2 to manage your services lifecycle):
pm2 start node calculator.addition/index.js --name addition
This way you can stop all processes with :
pm2 stop all
Your scripts section would look like :
"scripts": {
"start": "pm2 stop -s addition division;pm2 delete -s addition division; pm2 start node calculator.addition/index.js -n addition; pm2 start node calculator.division/index.js -n division"
}
I suggest you run it with a node_modlue called concurrently that way all processes will run at the same time and it will stop once you tell it to stop.
scripts: {
"start": "concurrently --raw --kill-others \"npm run addition\" \"npm run node2\"",
"addition": "node calculator.addition/index.js",
"division": "node calculator.division/index.js",
...etc
}