Search code examples
node.jsnodemonconcurrently

Gracefully shutdown when using npm-run-all


In my development environment, I am using npm-run-all (run-p) to run two npm scripts in parallel: the typescript compiler in wach mode; and my server with nodemon. I am trying to gracefully shutdown my server when I hit CTRL+C, but it seems that the neither SIGINT or SIGTERM are being captured. Can anyone identify what is the problem?

I am using Node v18.3.0 on Windows, npm-run-all@^4.1.5 and nodemon@^2.0.20.

(I also tried using concurrently, but had the same issue...)

package.json:

  "scripts": {
    "build": "tsc",
    "start": "node --enable-source-maps ./dist/index.js",
    "dev": "npm run build && run-p dev:*",
    "dev:build": "tsc -w",
    "dev:run": "nodemon ./dist/index.js"
  }

./dist/index.js:

const shutdown = () => {
    httpServer.stop()
    socketServer.stop()
}

process.once('SIGINT', shutdown)
process.once('SIGTERM', shutdown)

Solution

  • NPM swallows the SIGTERM/SIGINT signals, so to gracefuly shutdown your app do not use npm run but node itself