AceDevHub
Intermediate Node.js Interview QuestionsIntermediateConcept

Node.js · Question 40

What is the difference between npm and npx?

Direct answer

npm installs and manages package dependencies in node_modules; npx executes package binaries — locally from node_modules/.bin or temporarily downloading a package — without global install, ideal for one-off CLIs like create-* scaffolds and tsx scripts.

npm is the package manager — install, update, audit, publish. npx is the package runner — run a command from a dependency without polluting global PATH.

CommandPurposeExample
npm install pkgAdd dependency to projectnpm install fastify
npm run scriptRun package.json scriptnpm run content:import:interviews
npx tsx file.tsRun local or fetch tsxnpx tsx src/scripts/seed.ts
npx create-next-appOne-shot scaffoldNo global create-next-app needed
  • Workspaces — npm install at monorepo root hoists shared deps; --workspace targets apps/api.
  • npm ci — clean install from lockfile in CI; faster and reproducible.
  • npx vs npm exec — npm exec is the modern equivalent; npx remains widely used.
npm-npx.sh
# Install dev tool in monorepo
npm install -D tsx --workspace @acedevhub/api

# Run without adding to scripts
npx tsx apps/api/src/scripts/import-content-interviews.ts

# Workspace-scoped script (preferred in AceDevHub)
npm run content:import:interviews --workspace @acedevhub/api