npm-stop
Stop a running Node.js application
TLDR
View documentation for the original command
SYNOPSIS
npm stop [-- script_args...]
PARAMETERS
script_args...
Optional arguments that will be passed directly to the custom "stop" script being executed. These arguments are typically separated from npm's own options by a double-hyphen (--) for clarity, though npm often passes subsequent arguments to the script even without it for lifecycle commands.
--
A conventional separator used to distinguish options intended for the npm command itself from arguments intended for the underlying "stop" script. While often optional for lifecycle scripts, its use is recommended for clarity and robustness.
DESCRIPTION
The npm stop command is a convenience alias that executes the "stop" script defined in the scripts section of a project's package.json file.
Unlike system-level commands like systemctl stop or kill, npm stop does not inherently know how to stop a running application or process. Instead, it relies entirely on a user-defined script that encapsulates the specific logic required to shut down the application or perform any necessary cleanup tasks.
If a "stop" script is not defined in package.json, running npm stop will typically result in a warning or an error indicating that the script was not found, and no action will be performed.
CAVEATS
Dependency on package.json: The command's functionality is entirely dependent on the presence and content of a stop script within the scripts section of the project's package.json file.
No built-in process management: npm stop does not inherently know how to stop a running application process. It merely executes a user-defined command that *should* handle the stopping logic (e.g., using kill, pkill, sending signals, or gracefully shutting down a server).
Error if script missing: If no stop script is defined, npm will typically exit with a warning or an error, indicating the script was not found, and no action will be performed.
Security Risk: As the script content is user-defined, poorly written or malicious scripts could cause unintended side effects, data loss, or security vulnerabilities.
DEFINING THE `STOP` SCRIPT
To make npm stop functional, you must define a stop script in your project's package.json file. Here's an example:
{
"name": "my-app",
"version": "1.0.0",
"scripts": {
"start": "node index.js",
"stop": "kill $(lsof -t -i:3000 || echo -1) || echo 'No process on port 3000 to stop'"
}
}
In this example, the stop script attempts to find and kill a process running on port 3000. The specific command will vary greatly depending on your application's architecture and how it's started.
COMMON USE CASES
npm stop is commonly used for:
- Shutting down a running web server (e.g., Node.js server, Express app).
- Terminating background processes launched by npm start or other scripts.
- Performing cleanup tasks like removing temporary files or database connections before the application fully shuts down.
- As part of a npm restart command, where stop is called before start.
HISTORY
npm (Node Package Manager) introduced the concept of lifecycle scripts early in its development to standardize common project tasks. Commands like start, stop, test, and install are convenient aliases for npm run