npm-restart
Restart Node.js applications managed by npm
TLDR
View documentation for the original command
SYNOPSIS
npm restart [--
PARAMETERS
--
Any arguments following '--' are passed directly to the start script executed during the restart process. These are not options for npm restart itself, but for the underlying script.
DESCRIPTION
npm restart is an npm command that executes a sequence of lifecycle scripts defined in a Node.js project's package.json file. It effectively combines the actions of npm stop and npm start. When npm restart is invoked, it first attempts to run the stop script (if defined), preceded by prestop and followed by poststop. After the stop sequence completes, it then executes the start script (if defined), preceded by prestart and followed by poststart. This provides a convenient way to gracefully shut down and then re-launch a Node.js application or service, making it ideal for deployments or development workflows requiring a full refresh. It relies entirely on the scripts section within package.json to define the actual commands for stopping and starting the application.
CAVEATS
- npm restart requires stop and start scripts (or their pre/post counterparts) to be defined in the scripts section of your package.json for it to perform meaningful actions. If these scripts are not defined, npm restart will complete without error but without performing any specific stop or start operations.
- Errors in prestop, stop, poststop, prestart, start, or poststart scripts will cause the entire npm restart process to terminate.
- This command is part of the npm CLI tool for Node.js projects, not a standalone operating system command.
LIFECYCLE SCRIPT SEQUENCE
When npm restart is invoked, it sequentially executes a series of lifecycle scripts defined in your package.json:
1. prestop (runs before stop)
2. stop (main stopping script)
3. poststop (runs after stop)
4. prestart (runs before start)
5. start (main starting script)
6. poststart (runs after start)
All these scripts are optional. If a script is not defined, npm simply skips it. If any executed script returns a non-zero exit code, the entire npm restart command fails.
<I>PACKAGE.JSON</I> CONFIGURATION
The commands executed by npm restart are configured in the "scripts" section of your project's package.json file. For instance, you might have:
"scripts": {
"stop": "node ./scripts/graceful-shutdown.js",
"start": "node app.js --port 3000"
}
This allows for custom logic to be executed for stopping and starting your application.
HISTORY
npm (Node Package Manager) was created by Isaac Schlueter and first released in 2010. The concept of lifecycle scripts, including start and stop, has been a core feature of npm since its early days, allowing developers to standardize project operations. npm restart was introduced as a convenience command to bundle the common stop and start sequence, streamlining development and deployment workflows for Node.js applications.