Use npm to manage packages and project scripts; use npx or npm exec to invoke a package executable. If the task is already named in your project’s package.json, use npm run. The right choice depends on whether you are managing dependencies, running a one-off command, or executing a project task.
What’s the difference between npm and npx?
npm is the Node.js package manager and command-line interface. Its commands install and manage packages, inspect package information, and run scripts defined by a project. npx is for invoking executable commands supplied by npm packages; it is not a separate package-management workflow.
As an Amazon Associate I earn from qualifying purchases.
In modern npm, npx is implemented using npm exec. npm’s documentation says the npx binary was rewritten in npm v7.0.0 and the standalone npx package was deprecated at that time. The current command still retains compatibility behavior, so older instructions and flags may not work exactly as they once did. See the npm exec documentation for the current reference.
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Which command fits your task?
| What you want to do | Use | Why |
|---|---|---|
| Install or manage project dependencies | npm install or another appropriate npm command |
These are package-management tasks. |
Run a task named in your project’s package.json |
npm run <script> |
Runs the project script and makes local binaries available on PATH. |
| Invoke a package executable directly, often for an ad hoc task | npm exec -- <command> [args...] or npx |
Runs a command provided by a local or remote npm package. |
When should you use npm run?
Use npm run <script> when the command is a project task declared under scripts in package.json. For example, if the file contains "scripts": { "build": "some-tool build" }, run:
#1 Best Overall
npm run build
npm adds the project’s node_modules/.bin directory to PATH while running a script. That lets the script call locally installed command-line tools by their short executable names without requiring a global installation. For arguments intended for the script, place -- after the script name:
npm run build -- --watch
Here, --watch is passed to the script’s command rather than interpreted as an option to npm. The npm run documentation describes script execution and argument forwarding.
When should you use npm exec or npx?
Use npm exec when you want npm to run a package executable rather than a named project script. To make the boundary between npm’s options and the executable’s arguments explicit, use --:
npm exec -- <command> [args...]
For example, npm exec -- tool-name --help tells npm to run tool-name and pass --help to that command. This is especially useful when the executable accepts options that npm might otherwise parse.
Rank #3
You can also make a package available to an alternate executable name with --package:
npm exec --package=<package-name> -- <command> [args...]
This form is useful when the package name and executable name differ. npm’s versioned npm exec reference describes running local or remote package commands and the --package option.
Rank #4
How do npx and npm exec handle arguments differently?
Although modern npx uses npm exec, their command-line parsing is not identical. With npx, put npm options before positional arguments. An option that follows the command name may be treated as an argument for the invoked executable instead.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsWith npm exec, use -- to mark the end of npm’s options and the beginning of the executable command and its arguments. The npm documentation illustrates the distinction with npx foo@latest bar --package=@npmcli/foo and npm exec foo@latest bar --package=@npmcli/foo: the placement of --package can change how the command is parsed. When unsure, the explicit npm exec -- <command> [args...] form makes the intended boundary clear.
Best Value
Does npx always download a package?
No. npm documents execution involving local or remote package commands, local executables on PATH, and cache use. Whether a package is installed or fetched depends on the command, package availability, cache, npm configuration, and npm version. Do not assume every npx command downloads a fresh copy.
When an invocation requires installing a package, current npx behavior includes a prompt before installation to help catch mistyped package names. The exact experience can depend on your npm version and configuration; consult the current npm exec reference for the behavior documented for that CLI.
What should you check before running a package command?
A package executable is code, so check the package identity and understand the command before proceeding. One-off execution can involve installation, and installation may involve lifecycle scripts. npm documents allow-scripts controls for one-off contexts and strongly discourages bypassing policy with --dangerously-allow-all-scripts. This does not mean every invocation downloads a package or runs install scripts; the relevant behavior depends on the execution context and npm settings. See npm’s install-script policy documentation before changing those controls.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




