npm Scripts & Workspaces

Scripts, lifecycle hooks, npm exec, and workspace workflows.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Scripts and lifecycle

Run custom package scripts and understand lifecycle behavior.

Run a named script

Run a package.json script.

bashANYnpmrunscripts
bash
npm run <script>
Notes

Run a package.json script.

Run a script quietly

Reduce npm log noise when running scripts.

bashANYnpmrunscripts
bash
npm run --silent <script>
Notes

Reduce npm log noise when running scripts.

Pass args to a script

Pass additional arguments after `--`.

bashANYnpmrunscripts
bash
npm run build -- --watch
Notes

Pass additional arguments after `--`.

Run test script

Shortcut for `npm run test`.

bashANYnpmtestscripts
bash
npm test
Notes

Shortcut for `npm run test`.

Run start script

Shortcut for `npm run start`.

bashANYnpmstartscripts
bash
npm start
Notes

Shortcut for `npm run start`.

Run stop script

Shortcut for `npm run stop`.

bashANYnpmstopscripts
bash
npm stop
Notes

Shortcut for `npm run stop`.

Rebuild native modules

Rebuild packages with install scripts or native addons.

bashANYnpmrebuild
bash
npm rebuild
Notes

Rebuild packages with install scripts or native addons.

Workspace basics

Initialize and inspect npm workspaces.

Create a new workspace

Create a workspace and update the root workspaces list.

bashANYnpmworkspaceinit
bash
npm init -w packages/app -y
Notes

Create a workspace and update the root workspaces list.

Install all workspace dependencies

Run install across all configured workspaces.

bashANYnpmworkspacesinstall
bash
npm install --workspaces
Notes

Run install across all configured workspaces.

Run script in all workspaces

Run a script in all workspaces.

bashANYnpmworkspacesscripts
bash
npm run build --workspaces
Notes

Run a script in all workspaces.

Run script in one workspace

Run a script for one specific workspace.

bashANYnpmworkspacescripts
bash
npm run test --workspace=@acme/app
Notes

Run a script for one specific workspace.

Include workspace root

Run scripts in workspaces and the root package.

bashANYnpmworkspaceroot
bash
npm run build --workspaces --include-workspace-root
Notes

Run scripts in workspaces and the root package.

Execution helpers

Use npm exec and package binaries in mono-repos.

Run a binary from a package

Run a local binary inside the project context.

bashANYnpmexecbin
bash
npm exec eslint .
Notes

Run a local binary inside the project context.

Run a package temporarily

Download a package temporarily and run its binary.

bashANYnpmexectemporary
bash
npm exec --package cowsay -- cowsay hello
Notes

Download a package temporarily and run its binary.

Run a create-* initializer

Use create-* scaffolding through npm.

bashANYnpmcreatescaffold
bash
npm create vite@latest my-app
Notes

Use create-* scaffolding through npm.

Lifecycle patterns

Commands related to lifecycle hooks and script troubleshooting.

Run scripts in foreground

Surface script output directly in the terminal.

bashANYnpmscriptsdebug
bash
npm install --foreground-scripts
Notes

Surface script output directly in the terminal.

Run script only if it exists

Avoid failing when a script is missing.

bashANYnpmscripts
bash
npm run lint --if-present
Notes

Avoid failing when a script is missing.

Disable lifecycle scripts

Turn off lifecycle scripts globally or locally.

bashANYnpmconfigscripts
bash
npm config set ignore-scripts true
Notes

Turn off lifecycle scripts globally or locally.

Recommended next

No recommendations yet.