11cb0ef41Sopenharmony_ci---
21cb0ef41Sopenharmony_cititle: npm-run-script
31cb0ef41Sopenharmony_cisection: 1
41cb0ef41Sopenharmony_cidescription: Run arbitrary package scripts
51cb0ef41Sopenharmony_ci---
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci### Synopsis
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci```bash
101cb0ef41Sopenharmony_cinpm run-script <command> [-- <args>]
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_cialiases: run, rum, urn
131cb0ef41Sopenharmony_ci```
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ci### Description
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ciThis runs an arbitrary command from a package's `"scripts"` object.  If no
181cb0ef41Sopenharmony_ci`"command"` is provided, it will list the available scripts.
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci`run[-script]` is used by the test, start, restart, and stop commands, but
211cb0ef41Sopenharmony_cican be called directly, as well. When the scripts in the package are
221cb0ef41Sopenharmony_ciprinted out, they're separated into lifecycle (test, start, restart) and
231cb0ef41Sopenharmony_cidirectly-run scripts.
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciAny positional arguments are passed to the specified script.  Use `--` to
261cb0ef41Sopenharmony_cipass `-`-prefixed flags and options which would otherwise be parsed by npm.
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_ciFor example:
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ci```bash
311cb0ef41Sopenharmony_cinpm run test -- --grep="pattern"
321cb0ef41Sopenharmony_ci```
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ciThe arguments will only be passed to the script specified after `npm run`
351cb0ef41Sopenharmony_ciand not to any `pre` or `post` script.
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciThe `env` script is a special built-in command that can be used to list
381cb0ef41Sopenharmony_cienvironment variables that will be available to the script at runtime. If an
391cb0ef41Sopenharmony_ci"env" command is defined in your package, it will take precedence over the
401cb0ef41Sopenharmony_cibuilt-in.
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_ciIn addition to the shell's pre-existing `PATH`, `npm run` adds
431cb0ef41Sopenharmony_ci`node_modules/.bin` to the `PATH` provided to scripts. Any binaries
441cb0ef41Sopenharmony_ciprovided by locally-installed dependencies can be used without the
451cb0ef41Sopenharmony_ci`node_modules/.bin` prefix. For example, if there is a `devDependency` on
461cb0ef41Sopenharmony_ci`tap` in your package, you should write:
471cb0ef41Sopenharmony_ci
481cb0ef41Sopenharmony_ci```bash
491cb0ef41Sopenharmony_ci"scripts": {"test": "tap test/*.js"}
501cb0ef41Sopenharmony_ci```
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ciinstead of
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ci```bash
551cb0ef41Sopenharmony_ci"scripts": {"test": "node_modules/.bin/tap test/*.js"}
561cb0ef41Sopenharmony_ci```
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciThe actual shell your script is run within is platform dependent. By default,
591cb0ef41Sopenharmony_cion Unix-like systems it is the `/bin/sh` command, on Windows it is
601cb0ef41Sopenharmony_ci`cmd.exe`.
611cb0ef41Sopenharmony_ciThe actual shell referred to by `/bin/sh` also depends on the system.
621cb0ef41Sopenharmony_ciYou can customize the shell with the
631cb0ef41Sopenharmony_ci[`script-shell` config](/using-npm/config#script-shell).
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ciScripts are run from the root of the package folder, regardless of what the
661cb0ef41Sopenharmony_cicurrent working directory is when `npm run` is called. If you want your
671cb0ef41Sopenharmony_ciscript to use different behavior based on what subdirectory you're in, you
681cb0ef41Sopenharmony_cican use the `INIT_CWD` environment variable, which holds the full path you
691cb0ef41Sopenharmony_ciwere in when you ran `npm run`.
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci`npm run` sets the `NODE` environment variable to the `node` executable
721cb0ef41Sopenharmony_ciwith which `npm` is executed.
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ciIf you try to run a script without having a `node_modules` directory and it
751cb0ef41Sopenharmony_cifails, you will be given a warning to run `npm install`, just in case you've
761cb0ef41Sopenharmony_ciforgotten.
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci### Workspaces support
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciYou may use the [`workspace`](/using-npm/config#workspace) or
811cb0ef41Sopenharmony_ci[`workspaces`](/using-npm/config#workspaces) configs in order to run an
821cb0ef41Sopenharmony_ciarbitrary command from a package's `"scripts"` object in the context of the
831cb0ef41Sopenharmony_cispecified workspaces. If no `"command"` is provided, it will list the available
841cb0ef41Sopenharmony_ciscripts for each of these configured workspaces.
851cb0ef41Sopenharmony_ci
861cb0ef41Sopenharmony_ciGiven a project with configured workspaces, e.g:
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci```
891cb0ef41Sopenharmony_ci.
901cb0ef41Sopenharmony_ci+-- package.json
911cb0ef41Sopenharmony_ci`-- packages
921cb0ef41Sopenharmony_ci   +-- a
931cb0ef41Sopenharmony_ci   |   `-- package.json
941cb0ef41Sopenharmony_ci   +-- b
951cb0ef41Sopenharmony_ci   |   `-- package.json
961cb0ef41Sopenharmony_ci   `-- c
971cb0ef41Sopenharmony_ci       `-- package.json
981cb0ef41Sopenharmony_ci```
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ciAssuming the workspace configuration is properly set up at the root level
1011cb0ef41Sopenharmony_ci`package.json` file. e.g:
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci```
1041cb0ef41Sopenharmony_ci{
1051cb0ef41Sopenharmony_ci    "workspaces": [ "./packages/*" ]
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ci```
1081cb0ef41Sopenharmony_ci
1091cb0ef41Sopenharmony_ciAnd that each of the configured workspaces has a configured `test` script,
1101cb0ef41Sopenharmony_ciwe can run tests in all of them using the
1111cb0ef41Sopenharmony_ci[`workspaces` config](/using-npm/config#workspaces):
1121cb0ef41Sopenharmony_ci
1131cb0ef41Sopenharmony_ci```
1141cb0ef41Sopenharmony_cinpm test --workspaces
1151cb0ef41Sopenharmony_ci```
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci#### Filtering workspaces
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ciIt's also possible to run a script in a single workspace using the `workspace`
1201cb0ef41Sopenharmony_ciconfig along with a name or directory path:
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci```
1231cb0ef41Sopenharmony_cinpm test --workspace=a
1241cb0ef41Sopenharmony_ci```
1251cb0ef41Sopenharmony_ci
1261cb0ef41Sopenharmony_ciThe `workspace` config can also be specified multiple times in order to run a
1271cb0ef41Sopenharmony_cispecific script in the context of multiple workspaces. When defining values for
1281cb0ef41Sopenharmony_cithe `workspace` config in the command line, it also possible to use `-w` as a
1291cb0ef41Sopenharmony_cishorthand, e.g:
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_ci```
1321cb0ef41Sopenharmony_cinpm test -w a -w b
1331cb0ef41Sopenharmony_ci```
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ciThis last command will run `test` in both `./packages/a` and `./packages/b`
1361cb0ef41Sopenharmony_cipackages.
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_ci### Configuration
1391cb0ef41Sopenharmony_ci
1401cb0ef41Sopenharmony_ci#### `workspace`
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ci* Default:
1431cb0ef41Sopenharmony_ci* Type: String (can be set multiple times)
1441cb0ef41Sopenharmony_ci
1451cb0ef41Sopenharmony_ciEnable running a command in the context of the configured workspaces of the
1461cb0ef41Sopenharmony_cicurrent project while filtering by running only the workspaces defined by
1471cb0ef41Sopenharmony_cithis configuration option.
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ciValid values for the `workspace` config are either:
1501cb0ef41Sopenharmony_ci
1511cb0ef41Sopenharmony_ci* Workspace names
1521cb0ef41Sopenharmony_ci* Path to a workspace directory
1531cb0ef41Sopenharmony_ci* Path to a parent workspace directory (will result in selecting all
1541cb0ef41Sopenharmony_ci  workspaces within that folder)
1551cb0ef41Sopenharmony_ci
1561cb0ef41Sopenharmony_ciWhen set for the `npm init` command, this may be set to the folder of a
1571cb0ef41Sopenharmony_ciworkspace which does not yet exist, to create the folder and set it up as a
1581cb0ef41Sopenharmony_cibrand new workspace within the project.
1591cb0ef41Sopenharmony_ci
1601cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes.
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci#### `workspaces`
1631cb0ef41Sopenharmony_ci
1641cb0ef41Sopenharmony_ci* Default: null
1651cb0ef41Sopenharmony_ci* Type: null or Boolean
1661cb0ef41Sopenharmony_ci
1671cb0ef41Sopenharmony_ciSet to true to run the command in the context of **all** configured
1681cb0ef41Sopenharmony_ciworkspaces.
1691cb0ef41Sopenharmony_ci
1701cb0ef41Sopenharmony_ciExplicitly setting this to false will cause commands like `install` to
1711cb0ef41Sopenharmony_ciignore workspaces altogether. When not set explicitly:
1721cb0ef41Sopenharmony_ci
1731cb0ef41Sopenharmony_ci- Commands that operate on the `node_modules` tree (install, update, etc.)
1741cb0ef41Sopenharmony_ciwill link workspaces into the `node_modules` folder. - Commands that do
1751cb0ef41Sopenharmony_ciother things (test, exec, publish, etc.) will operate on the root project,
1761cb0ef41Sopenharmony_ci_unless_ one or more workspaces are specified in the `workspace` config.
1771cb0ef41Sopenharmony_ci
1781cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes.
1791cb0ef41Sopenharmony_ci
1801cb0ef41Sopenharmony_ci#### `include-workspace-root`
1811cb0ef41Sopenharmony_ci
1821cb0ef41Sopenharmony_ci* Default: false
1831cb0ef41Sopenharmony_ci* Type: Boolean
1841cb0ef41Sopenharmony_ci
1851cb0ef41Sopenharmony_ciInclude the workspace root when workspaces are enabled for a command.
1861cb0ef41Sopenharmony_ci
1871cb0ef41Sopenharmony_ciWhen false, specifying individual workspaces via the `workspace` config, or
1881cb0ef41Sopenharmony_ciall workspaces via the `workspaces` flag, will cause npm to operate only on
1891cb0ef41Sopenharmony_cithe specified workspaces, and not on the root project.
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes.
1921cb0ef41Sopenharmony_ci
1931cb0ef41Sopenharmony_ci#### `if-present`
1941cb0ef41Sopenharmony_ci
1951cb0ef41Sopenharmony_ci* Default: false
1961cb0ef41Sopenharmony_ci* Type: Boolean
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ciIf true, npm will not exit with an error code when `run-script` is invoked
1991cb0ef41Sopenharmony_cifor a script that isn't defined in the `scripts` section of `package.json`.
2001cb0ef41Sopenharmony_ciThis option can be used when it's desirable to optionally run a script when
2011cb0ef41Sopenharmony_ciit's present and fail if the script fails. This is useful, for example, when
2021cb0ef41Sopenharmony_cirunning scripts that may only apply for some builds in an otherwise generic
2031cb0ef41Sopenharmony_ciCI setup.
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ciThis value is not exported to the environment for child processes.
2061cb0ef41Sopenharmony_ci
2071cb0ef41Sopenharmony_ci#### `ignore-scripts`
2081cb0ef41Sopenharmony_ci
2091cb0ef41Sopenharmony_ci* Default: false
2101cb0ef41Sopenharmony_ci* Type: Boolean
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ciIf true, npm does not run scripts specified in package.json files.
2131cb0ef41Sopenharmony_ci
2141cb0ef41Sopenharmony_ciNote that commands explicitly intended to run a particular script, such as
2151cb0ef41Sopenharmony_ci`npm start`, `npm stop`, `npm restart`, `npm test`, and `npm run-script`
2161cb0ef41Sopenharmony_ciwill still run their intended script if `ignore-scripts` is set, but they
2171cb0ef41Sopenharmony_ciwill *not* run any pre- or post-scripts.
2181cb0ef41Sopenharmony_ci
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci
2211cb0ef41Sopenharmony_ci#### `foreground-scripts`
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_ci* Default: `false` unless when using `npm pack` or `npm publish` where it
2241cb0ef41Sopenharmony_ci  defaults to `true`
2251cb0ef41Sopenharmony_ci* Type: Boolean
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ciRun all build scripts (ie, `preinstall`, `install`, and `postinstall`)
2281cb0ef41Sopenharmony_ciscripts for installed packages in the foreground process, sharing standard
2291cb0ef41Sopenharmony_ciinput, output, and error with the main npm process.
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ciNote that this will generally make installs run slower, and be much noisier,
2321cb0ef41Sopenharmony_cibut can be useful for debugging.
2331cb0ef41Sopenharmony_ci
2341cb0ef41Sopenharmony_ci
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci#### `script-shell`
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ci* Default: '/bin/sh' on POSIX systems, 'cmd.exe' on Windows
2391cb0ef41Sopenharmony_ci* Type: null or String
2401cb0ef41Sopenharmony_ci
2411cb0ef41Sopenharmony_ciThe shell to use for scripts run with the `npm exec`, `npm run` and `npm
2421cb0ef41Sopenharmony_ciinit <package-spec>` commands.
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_ci
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci### See Also
2471cb0ef41Sopenharmony_ci
2481cb0ef41Sopenharmony_ci* [npm scripts](/using-npm/scripts)
2491cb0ef41Sopenharmony_ci* [npm test](/commands/npm-test)
2501cb0ef41Sopenharmony_ci* [npm start](/commands/npm-start)
2511cb0ef41Sopenharmony_ci* [npm restart](/commands/npm-restart)
2521cb0ef41Sopenharmony_ci* [npm stop](/commands/npm-stop)
2531cb0ef41Sopenharmony_ci* [npm config](/commands/npm-config)
2541cb0ef41Sopenharmony_ci* [npm workspaces](/using-npm/workspaces)
255