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.
| Command | Purpose | Example |
|---|---|---|
| npm install pkg | Add dependency to project | npm install fastify |
| npm run script | Run package.json script | npm run content:import:interviews |
| npx tsx file.ts | Run local or fetch tsx | npx tsx src/scripts/seed.ts |
| npx create-next-app | One-shot scaffold | No 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