npm (Node.js) Commands Cheat Sheet
A quick reference for the Node Package Manager. Learn how to install dependencies, run scripts, and manage your package.json file.
npm init
Initializes a new Node.js project and creates a package.json file.
Syntax
npm init
npm init -yExample
npm init -yPro Tip
Use the `-y` flag to skip the interactive questionnaire and immediately generate a default `package.json`.
Common Mistake
Running this inside an existing project directory, accidentally overwriting your package.json.
npm install
Installs dependencies defined in the package.json file. Also used to install specific new packages.
Syntax
npm install
npm install <package-name>Example
npm install expressPro Tip
You can use `npm i` as a shortcut instead of typing the full `npm install`.
Common Mistake
Forgetting to add `--save-dev` when installing testing libraries or build tools like Webpack.
npm install -D
Installs a package as a 'devDependency'. This means the package is only needed during development, not in the final production app.
Syntax
npm install -D <package-name>Example
npm install -D jestPro Tip
Use this for testing frameworks (Jest), linters (ESLint), and TypeScript.
Common Mistake
Installing core runtime libraries (like React or Express) as dev dependencies.
npm run
Executes a custom script defined in the 'scripts' section of your package.json.
Syntax
npm run <script-name>Example
npm run dev
npm run buildPro Tip
For the 'start' and 'test' scripts, you can omit the word 'run' (e.g., `npm start` works).
Common Mistake
Trying to run `npm run start` without actually defining a 'start' script in your package.json.
npm update
Updates all packages to their latest minor/patch versions based on the semantic versioning rules in package.json.
Syntax
npm updateExample
npm updatePro Tip
To force upgrade everything to the absolute latest major version, use the `npm-check-updates` utility package.
Common Mistake
Assuming this command will upgrade major versions (e.g., React 17 to React 18). It won't. It respects the `^` or `~` prefixes.
npx
Executes a Node package binary without needing to install it globally on your computer.
Syntax
npx <package-name>Example
npx create-react-app my-appPro Tip
npx is the modern standard for bootstrapping new projects (like `npx create-next-app`).
Common Mistake
Using `npm` when you meant to use `npx`. `npm` installs things. `npx` *executes* things.