Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

npm manages JavaScript packages and project tasks; npx runs package executables. For a named task defined in your project’s package.json, use npm run. For a package command you want to invoke directly, use npx or the closely related npm exec.

What’s the difference between npm and npx?

npm is the package manager and command-line interface (CLI) used to install and manage dependencies, maintain project metadata, and run project scripts. npx is a command for invoking package executables. It can use a locally available executable or arrange to run a package command from another source; it does not mean “always download a fresh copy.” The exact resolution depends on the command, package availability, cache, npm configuration, and installed npm version.

In modern npm, npx is closely tied to npm exec, rather than being a wholly separate package runner. npm’s documentation says the npx binary was rewritten in npm v7.0.0 to use npm exec, and the standalone npx package was deprecated then. The current command retains compatibility behavior, so older examples and assumptions may not match the npm version on your machine. See npm’s current npm exec documentation.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Which command should you use?

What you want to do Use Why
Install, update, or manage project dependencies npm commands, such as npm install These are package-management operations.
Run a named task defined in package.json npm run <script> Runs the project’s script and adds local node_modules/.bin executables to the script’s PATH.
Invoke a package executable directly npx <command> or npm exec -- <command> Runs a package command without requiring you to define a named script first.
Run a differently named executable supplied by a package npm exec --package=<package> -- <command> The package is made available to the executed command; the executable name need not match the package name.

How do you run a package executable?

Use npx for direct invocation

For a direct package command, the familiar form is npx <command> [args...]. npm documents that npx can use local and remote package commands, with cache and install behavior involved in resolution. If npm needs to install a package for the invocation, it prompts before installing to reduce mistakes caused by mistyped package names. Check the package identity and command before accepting.

Use npm exec when you want explicit argument forwarding

The clearest form for passing flags to the executable is npm exec -- <command> [args...]. The -- separator tells npm to stop parsing options and pass what follows to the program. For example, if the executable accepts --help, write npm exec -- tool --help so that the flag goes to tool.

If the package name and executable name differ, specify the package and then the command: npm exec --package=<package> -- <command> [args...]. npm documents --package as making that package available on the executed command’s PATH. The versioned npm exec reference describes this pattern.

How do argument separators differ in npx and npm exec?

Both commands are related, but their option parsing is not perfectly interchangeable. With npx, put npm options before positional arguments. After the command name, an option may be passed to the invoked executable instead. With npm exec, use -- to clearly separate npm’s options from the command and its arguments.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For example, npm’s documentation demonstrates that npx foo@latest bar --package=@npmcli/foo and npm exec foo@latest bar --package=@npmcli/foo can be parsed differently. Avoid relying on a trailing option’s meaning: put npm options in the npm-options position, or use the explicit npm exec --package=... -- command form when you need to control which executable runs. npm documents the parsing differences and separator behavior.

When should you use npm run?

Use npm run <script> when the task is part of the project’s workflow and is named in its package.json scripts object. For example, if a project defines a build script, run npm run build. This runs the project’s declared command rather than asking you to invoke a package executable ad hoc.

npm adds the project’s node_modules/.bin directory to PATH while running a script. As a result, a script can call a locally installed binary by its short executable name. To pass arguments through to a script, use the separator after the script name, such as npm run build -- --watch; the arguments after -- are forwarded to the script. npm’s npm run documentation covers scripts, local binaries, and argument forwarding.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What should you check before running a package command?

  • Confirm the package and executable. A typo can point to a different package than intended; review the name and the command before proceeding with an installation prompt.
  • Understand install-time behavior. One-off execution may involve package installation, and npm’s current exec documentation includes install-script policy controls for one-off contexts. This does not mean every invocation downloads a package or runs install scripts.
  • Use the policy appropriate to your environment. npm documents allow-scripts controls and a --dangerously-allow-all-scripts option that bypasses policy; npm strongly discourages that bypass. Avoid broad script permission unless you understand the consequences.
  • Check your npm version and configuration. Compatibility behavior and policy settings can affect how a command is resolved and run.

The npm exec reference documents prompts, cache behavior, and install-script policy settings.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A quick rule of thumb

  • Managing dependencies or project metadata? Use npm.
  • Running a named task in the project? Use npm run.
  • Invoking a package executable directly? Use npx or, for clear option forwarding, npm exec --.

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.