T

TechIdea

Ecosystem

Back to Commands
npm COMMANDS

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

bash
npm init
npm init -y

Example

bash
npm init -y

Pro 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

bash
npm install
npm install <package-name>

Example

bash
npm install express

Pro 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

bash
npm install -D <package-name>

Example

bash
npm install -D jest

Pro 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

bash
npm run <script-name>

Example

bash
npm run dev
npm run build

Pro 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

bash
npm update

Example

bash
npm update

Pro 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

bash
npx <package-name>

Example

bash
npx create-react-app my-app

Pro 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.

Growth Newsletter

Get practical AI tools, SEO tips, and growth guides weekly.

Join creators, students, and businesses scaling with TechIdea.