.TH "NPM\-RUN\-SCRIPT" "1" "November 2023" "9.2.0" .SH "NAME" \fBnpm-run-script\fR .SH Synopsis .SH Description .P This runs an arbitrary command from a package's \fB"scripts"\fP object\. If no .br \fB"command"\fP is provided, it will list the available scripts\. .P \fBrun[\-script]\fP is used by the test, start, restart, and stop commands, but .br can be called directly, as well\. When the scripts in the package are .br printed out, they're separated into lifecycle (test, start, restart) and .br directly\-run scripts\. .P Any positional arguments are passed to the specified script\. Use \fB\-\-\fP to .br pass \fB\-\fP\-prefixed flags and options which would otherwise be parsed by npm\. .P For example: .RS 2 .nf npm run test \-\- \-\-grep="pattern" .fi .RE .P The arguments will only be passed to the script specified after \fBnpm run\fP .br and not to any \fBpre\fP or \fBpost\fP script\. .P The \fBenv\fP script is a special built\-in command that can be used to list .br environment variables that will be available to the script at runtime\. If an .br "env" command is defined in your package, it will take precedence over the .br built\-in\. .P In addition to the shell's pre\-existing \fBPATH\fP, \fBnpm run\fP adds .br \fBnode_modules/\.bin\fP to the \fBPATH\fP provided to scripts\. Any binaries .br provided by locally\-installed dependencies can be used without the .br \fBnode_modules/\.bin\fP prefix\. For example, if there is a \fBdevDependency\fP on .br \fBtap\fP in your package, you should write: .RS 2 .nf "scripts": {"test": "tap test/*\.js"} .fi .RE .P instead of .RS 2 .nf "scripts": {"test": "node_modules/\.bin/tap test/*\.js"} .fi .RE .P The actual shell your script is run within is platform dependent\. By default, .br on Unix\-like systems it is the \fB/bin/sh\fP command, on Windows it is .br \fBcmd\.exe\fP\|\. .br The actual shell referred to by \fB/bin/sh\fP also depends on the system\. .br You can customize the shell with the .br \fBscript\-shell\fP config\. .P Scripts are run from the root of the package folder, regardless of what the .br current working directory is when \fBnpm run\fP is called\. If you want your .br script to use different behavior based on what subdirectory you're in, you .br can use the \fBINIT_CWD\fP environment variable, which holds the full path you .br were in when you ran \fBnpm run\fP\|\. .P \fBnpm run\fP sets the \fBNODE\fP environment variable to the \fBnode\fP executable .br with which \fBnpm\fP is executed\. .P If you try to run a script without having a \fBnode_modules\fP directory and it .br fails, you will be given a warning to run \fBnpm install\fP, just in case you've .br forgotten\. .SH Workspaces support .P You may use the \fBworkspace\fP or .br \fBworkspaces\fP configs in order to run an .br arbitrary command from a package's \fB"scripts"\fP object in the context of the .br specified workspaces\. If no \fB"command"\fP is provided, it will list the available .br scripts for each of these configured workspaces\. .P Given a project with configured workspaces, e\.g: .RS 2 .nf \|\. +\-\- package\.json `\-\- packages +\-\- a | `\-\- package\.json +\-\- b | `\-\- package\.json `\-\- c `\-\- package\.json .fi .RE .P Assuming the workspace configuration is properly set up at the root level .br \fBpackage\.json\fP file\. e\.g: .RS 2 .nf { "workspaces": [ "\./packages/*" ] } .fi .RE .P And that each of the configured workspaces has a configured \fBtest\fP script, .br we can run tests in all of them using the .br \fBworkspaces\fP config: .RS 2 .nf npm test \-\-workspaces .fi .RE .SS Filtering workspaces .P It's also possible to run a script in a single workspace using the \fBworkspace\fP .br config along with a name or directory path: .RS 2 .nf npm test \-\-workspace=a .fi .RE .P The \fBworkspace\fP config can also be specified multiple times in order to run a .br specific script in the context of multiple workspaces\. When defining values for .br the \fBworkspace\fP config in the command line, it also possible to use \fB\-w\fP as a .br shorthand, e\.g: .RS 2 .nf npm test \-w a \-w b .fi .RE .P This last command will run \fBtest\fP in both \fB\|\./packages/a\fP and \fB\|\./packages/b\fP .br packages\. .SH Configuration .SH See Also .RS 1 .IP \(bu 2 npm scripts .IP \(bu 2 npm test .IP \(bu 2 npm start .IP \(bu 2 npm restart .IP \(bu 2 npm stop .IP \(bu 2 npm config .IP \(bu 2 npm workspaces .RE