How to automatically restart a node.js script daily using nodemon? - node.js

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

Related

kill process on port when starting node.js app?

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

how do start cron in nextjs after server restart , we not using custom server

how do start cron in the next js after server restart?
NOTE: we not using a custom server.
we are using the next API routes.
how do trigger the function after the npm run start command. or else npm run dev.
in your package.json you can create your custom "dev" function or modify which scripts will be run. Example:
"scripts": {
"web:dev": "env-cmd -f environments/.env.development node server.js",
"build": "next build ",
"sitemap": "next-sitemap --config next-sitemap.config.js",
"start": "next start"
},
So, you can add the name of your file (need to create your nodejs file with functions needed) an it will be triggered

Typescript package.json scripts run build and start concurrently: port already in use

I am having an interesting problem building my typescript server using nodemon. I have a script for building out the ts files, and then starting the server. However, when I run these two concurrently, it starts at first fine, then after it is done building, it restarts, but gives me an error that the port is already in use. Is there a way to somehow kill the port each time before it starts?
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start:dev": "nodemon dist/index.js",
"build:dev": "tsc --watch --preserveWatchOutput",
"dev": "concurrently \"npm:build:dev\" \"npm:start:dev\""
},
I have tried adding "npx kill-port 8080 && nodemon dist/index.js" to the start:dev, but I am still getting that error. I have also tried "npx kill-port 8080; nodemon dist/index.js" Is there a solution to this issue? Thanks.
Edit: It seems that this is actually working as I expected it too however, for some reason the terminal is still showing an error message and therefore, anything my server logs to the console is hidden. Is there any way to fix this? Thanks.
I am not sure why exactly you get a port error, but you can improve your setup. Nodemon can run typescript with ts-node help.
Just install ts-node and run nodemon with --exec 'ts-node' property.
Example from my package.json:
{
"dev": "nodemon --watch 'src/**/*' -e 'ts' --exec 'ts-node' src/index.ts"
}

How to wait for server start complete from an npm script?

I'm trying to run a functional test for a node app.
In my package.json I have the following scripts:
"scripts": {
"web-server": "NODE_ENV=test node app.js &",
"test": "npm run web-server && mocha ./tests/functional/*.js --exit",
"posttest": "pkill -f node"
}
But when running it, tests run before the server completes starting.
How can I wait for the server?
I found wait-on today and like its approach. It only does the wait, not other things like command launching.
Using it with concurrently, like so:
"scripts": {
"xxx": "concurrently -n server,mocha \"npm run web-server\" \"npx wait-on http://localhost:8080 && npx mocha ...\"",
Wanted to mention to new visitors. I think wait-on is currently the best fitting answer to the title's question.
As far as I understand,
you'd like to run your local server, once the server is up tests cycle should be triggered.
I suggest to use the package "start-server-and-test" sounds suite for your solution, the NPM package page is here
Let's take your current package.json script object, and rewrite them.
The start and test scripts are the two basic scripts you need to maintain your app easily.
start - to start your app (I suggest to use nodemon or pm2)
test - call your test script
Notes:
To dev tests you will need to handle two terminals, each for the above.
I'm assuming you're running on port 8080
The package is also handling the termination of both processes (node and mocha) in both cases success and failure so no need (posttest:ci, --exit, etc..)
There is no need to use child process (the &) that mentioned at the end of your web-server package.json's script.
Here is the new script object, from my POV
"scripts": {
"start": "node app.js",
"test": "NODE_ENV=test mocha ./tests/functional/*.js",
"test:ci": "NODE_ENV=test start-server-and-test start \"http://localhost:8080\" test"
}
Now, from your CLI:
npm run test:ci
The ci suffix mentions this process is fully automated
It's expected that you'll have to define CI=true for a real CI environment,
just as all CI tools do and it's not necessary for local usage.

Is it possible to start a development server with npm pre hooks?

I currently have something like this which runs both my development server and my React app.
"scripts": {
"dev": "run-p yarn proxy:start react-scripts start",
"proxy:start": "nodemon test-proxy.js"
},
I was hoping to to do something like this:
"scripts": {
"predev": "nodemon test-proxy.js",
"dev": "react-scripts start"
},
But due to nodemon never exiting it keeps watching files until you exit the process yourself, the dev script never gets fired.
Is it possible to run predev, let it run in the background (keep running nodemon to watch file changes and preferably keep logging any I/O events) and also run my React start script?

Resources