npx
npm
JavaScript
Node.js
Programming Tools

Difference between npx and npm?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

npm and npx are related, but they solve different problems in the Node.js toolchain. npm installs and manages packages, while npx runs package binaries without requiring you to manage a long-lived global install first.

Core Sections

What npm is responsible for

npm is the package manager that ships with Node.js. Its main jobs are downloading packages from the registry, recording dependency versions in package.json, and running project scripts.

Typical npm work falls into three categories:

  • install a dependency into the current project
  • install a package globally when you truly need a machine-wide tool
  • run a script that your project defines
bash
npm install eslint --save-dev
npm install react
npm run test

When you run npm install, the package is placed in node_modules and, unless you suppress it, the dependency metadata is updated in your project files. That makes npm part of the project’s dependency lifecycle rather than a one-off command runner.

What npx does differently

npx is focused on executing package binaries. If a tool is already installed in your local project, npx can find it and run it. If the tool is not installed locally, npx can download an appropriate package version, execute it, and avoid leaving a permanent global install behind.

bash
npx eslint src
npx create-react-app my-app
npx cowsay "hello"

This is useful for generators, CLIs, and migration tools that you do not want to keep installed globally forever. It also makes it easier to run the version that belongs to the current project instead of whatever version happens to be installed on your machine.

Local binaries and why npx is convenient

Suppose your project has typescript as a development dependency. The compiler binary exists under the project’s local node_modules/.bin directory. You could call that path directly, but npx gives you a cleaner command.

bash
npm install --save-dev typescript
npx tsc --version
npx tsc src/index.ts

This is one of the most practical differences between the two tools. npm installs the package. npx runs its binary.

npm run versus npx

npm run and npx can look similar because both can end up invoking CLI tools. The distinction is that npm run executes a script defined in package.json, while npx executes a binary package command directly.

json
1{
2  "scripts": {
3    "lint": "eslint src",
4    "build": "tsc"
5  }
6}
bash
1npm run lint
2npm run build
3npx eslint src
4npx tsc

If the task is a standard project workflow, npm run is usually the cleaner interface because the team can use one shared command. If the task is a temporary CLI invocation, npx is often simpler.

When to prefer each tool

Use npm when you are managing dependency state:

  • adding libraries to a project
  • upgrading or removing packages
  • installing development tools that the project needs repeatedly
  • running named scripts that the team shares

Use npx when you are executing a package binary:

  • scaffolding a new project
  • running a migration or code generation tool once
  • invoking the project-local version of a CLI
  • testing a package command without a permanent global install

There is also a modern detail worth knowing: newer npm versions provide npm exec, and npx is effectively the convenience command people still recognize for that style of execution. In practice, the mental model remains the same.

A common real-world workflow

A project setup often uses both tools together.

bash
npm init -y
npm install --save-dev prettier
npx prettier --write .

Here, npm creates the project and adds the formatter as a dependency. Then npx runs the formatter binary. The tools are complementary, not competing.

Common Pitfalls

  • Installing every CLI globally with npm install -g, which makes versions drift across projects and machines.
  • Using npx repeatedly for tools that should actually be declared as project dependencies and versioned with the codebase.
  • Confusing npm run with npx, even though one runs package scripts and the other runs binaries directly.
  • Assuming npx is a package manager; it is an execution helper, not the tool that manages dependency state.
  • Forgetting that command behavior can change depending on whether a local project binary exists or whether npx has to fetch one temporarily.

Summary

  • 'npm manages packages, dependencies, and project scripts.'
  • 'npx runs package binaries, often without requiring a permanent global install.'
  • 'npm run is for named project workflows, while npx is for direct CLI execution.'
  • Use project-local dependencies for tools the codebase depends on regularly.
  • Treat npm and npx as complementary parts of the same workflow, not interchangeable commands.

Course illustration
Course illustration

All Rights Reserved.