0

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?

zumzum
  • 17,984
  • 26
  • 111
  • 172
  • Would it work if it runs your kill command every time or just when a process is running, kill it, then start? – code Oct 13 '22 at 01:59
  • every time is fine too. I just don't know how to trigger and run that. – zumzum Oct 13 '22 at 02:14

1 Answers1

0

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
code
  • 5,690
  • 4
  • 17
  • 39
  • When I do that, I get this in the console: > B2@1.0.0 dev > kill -9 $(lsof -t -i:4080) && node app.js [nodemon] app crashed - waiting for file changes before starting... – zumzum Oct 13 '22 at 04:25