NPM-RUN-SCRIPT(1)

πŸ’– NAME πŸ’–</p>

npm-run-script β€” run arbitrary package scripts, bestie. That's it, that's the whole vibe. This page is the NPM-RUN-SCRIPT(1) manual, and it's lowkey one of the most useful ones in the whole npm family. 🫢

✨ Synopsis ✨

npm run-script <command> [-- <args>]

aliases: run, rum, urn πŸ‘‘


🌸 Description 🌸

okay so. This runs an arbitrary command out of a package's "scripts" object. That's the entire point of it. If you don't hand it a "command", it will just list the available scripts for you β€” which, honestly, is the move when you roll up to a new repo and need the lay of the land. πŸ’…

run[-script] is what test, start, restart, and stop use under the hood, but you can also just call it directly yourself. When npm prints out the scripts in the package, it separates them into lifecycle (test, start, restart) and directly-run scripts. So you can see which ones are ~special~. ✨

Any positional arguments get passed straight to the script you specified. And here's the trick: use -- to pass --prefixed flags and options that npm would otherwise gobble up and parse for itself. Not me being dramatic, it's genuinely important. πŸ™ˆ

For example:

npm run test -- --grep="pattern"

And listen: those arguments only go to the script specified after npm run. They do not get passed to any pre or post script. Just the one. periodt. πŸ’€

The env script is a special built-in command that lists environment variables that will be available to the script at runtime. And if you've defined your own "env" command in your package, it takes precedence over the built-in one β€” your version wins, queen. πŸ‘‘

In addition to the shell's pre-existing PATH, npm run adds node_modules/.bin to the PATH provided to scripts. So any binaries from locally-installed dependencies can be used without the node_modules/.bin prefix. Like, if you have a devDependency on tap in your package, you should write:

"scripts": {"test": "tap test/*.js"}

instead of

"scripts": {"test": "node_modules/.bin/tap test/*.js"}

so much cleaner, we love a shorter path. 🌈

The actual shell your script runs inside is platform dependent. By default, on Unix-like systems it's the /bin/sh command, and on Windows it's cmd.exe. And plot twist: the actual shell that /bin/sh refers to also depends on the system. You can customize the shell with the script-shell config ⟨/using-npm/config#script-shell⟩. 🐚

Scripts are run from the root of the package folder, no matter what your current working directory is when you call npm run. If you want your script to behave differently depending on what subdirectory you're in, use the INIT_CWD environment variable β€” it holds the full path you were in when you ran npm run. 🧚

npm run sets the NODE environment variable to the node executable that npm itself is executed with. cute, right?

And one more thing: if you try to run a script without having a node_modules directory and it fails, you'll get a warning telling you to run npm install β€” just in case you forgot. which, bestie, you probably did. 😭


πŸ¦‹ Workspaces support πŸ¦‹

You can use the workspace ⟨/using-npm/config#workspace⟩ or workspaces ⟨/using-npm/config#workspaces⟩ configs to run an arbitrary command from a package's "scripts" object in the context of the specified workspaces. And if no "command" is provided, it will list the available scripts for each of those configured workspaces. multi-slaying. πŸ”₯

Given a project with configured workspaces, e.g:

+-- package.json
`-- packages
   +-- a
   |   `-- package.json
   +-- b
   |   `-- package.json
   `-- c
       `-- package.json

Assuming the workspace configuration is properly set up at the root level package.json file. e.g:

{
    "workspaces": [ "./packages/*" ]
}

And assuming each of the configured workspaces has a configured test script, we can run tests in all of them using the workspaces config ⟨/using-npm/config#workspaces⟩:

npm test --workspaces

πŸŽ€ Filtering workspaces πŸŽ€

It's also possible to run a script in a single workspace using the workspace config along with a name or directory path:

npm test --workspace=a

The workspace config can also be specified multiple times, so you can run a specific script in the context of multiple workspaces at once. And when you're defining values for the workspace config on the command line, you can also use -w as a shorthand, e.g:

npm test -w a -w b

That last command runs test in both the ./packages/a and ./packages/b packages. no cap. πŸ’–


πŸ’œ Configuration πŸ’œ

workspace

  • Default: (empty)
  • Type: String (can be set multiple times)

Enable running a command in the context of the configured workspaces of the current project, while filtering down to only the workspaces defined by this configuration option.

Valid values for the workspace config are either:

  • Workspace names
  • Path to a workspace directory
  • Path to a parent workspace directory (this will result in selecting all workspaces within that folder)

When it's set for the npm init command, this may be set to the folder of a workspace that doesn't exist yet β€” so npm creates the folder and sets it up as a brand new workspace within the project. glow-up in real time. ✨

This value is not exported to the environment for child processes.

workspaces

  • Default: null
  • Type: null or Boolean

Set it to true to run the command in the context of all configured workspaces.

Explicitly setting this to false will cause commands like install to ignore workspaces altogether. When it's not set explicitly:

  • Commands that operate on the node_modules tree (install, update, etc.) will link workspaces into the node_modules folder.
  • Commands that do other things (test, exec, publish, etc.) will operate on the root project, unless one or more workspaces are specified in the workspace config.

This value is not exported to the environment for child processes.

include-workspace-root

  • Default: false
  • Type: Boolean

Include the workspace root when workspaces are enabled for a command.

When it's false, specifying individual workspaces via the workspace config β€” or all workspaces via the workspaces flag β€” causes npm to operate only on the specified workspaces, and not on the root project. sorry root, you're sitting this one out. πŸ’€

This value is not exported to the environment for child processes.

if-present

  • Default: false
  • Type: Boolean

If true, npm will not exit with an error code when run-script is invoked for a script that isn't defined in the scripts section of package.json. This option is your girl when it's desirable to optionally run a script when it's present, and fail if the script fails. Super useful, for example, when running scripts that may only apply for some builds in an otherwise generic CI setup. 🧚

This value is not exported to the environment for child processes.

ignore-scripts

  • Default: false
  • Type: Boolean

If true, npm does not run scripts specified in package.json files.

Note that commands explicitly intended to run a particular script β€” such as npm start, npm stop, npm restart, npm test, and npm run-script β€” will still run their intended script if ignore-scripts is set, but they will not run any pre- or post-scripts. so like, the main character still shows up, the entourage does not. 😭

foreground-scripts

  • Default: false unless when using npm pack or npm publish where it defaults to true
  • Type: Boolean

Run all build scripts (ie, preinstall, install, and postinstall) scripts for installed packages in the foreground process, sharing standard input, output, and error with the main npm process.

Heads up: this will generally make installs run slower, and be much noisier β€” but it can be useful for debugging. chaotic but informative, honestly iconic. πŸ”₯

script-shell

  • Default: '/bin/sh' on POSIX systems, 'cmd.exe' on Windows
  • Type: null or String

The shell to use for scripts run with the npm exec, npm run and npm init <package-spec> commands. 🐚


⭐ See Also ⭐

  • npm help scripts
  • npm help test
  • npm help start
  • npm help restart
  • npm help stop
  • npm help config
  • npm help workspaces

and that's the whole page, bestie!! πŸŽ€ this was NPM@10.9.8, dated March 2026 β€” go forth and npm run something, you've got this. πŸ’…βœ¨πŸ¦‹

πŸ“¦ where this page comes from