1NPM-RUN-SCRIPT(1) NPM-RUN-SCRIPT(1)
2
3
4
6 npm-run-script - Run arbitrary package scripts
7
8 Synopsis
9 npm run-script <command> [-- <args>]
10
11 aliases: run, rum, urn
12
13 Description
14 This runs an arbitrary command from a package's "scripts" object. If
15 no "command" is provided, it will list the available scripts.
16
17 run[-script] is used by the test, start, restart, and stop commands,
18 but can be called directly, as well. When the scripts in the package
19 are printed out, they're separated into lifecycle (test, start,
20 restart) and directly-run scripts.
21
22 Any positional arguments are passed to the specified script. Use -- to
23 pass --prefixed flags and options which would otherwise be parsed by
24 npm.
25
26 For example:
27
28 npm run test -- --grep="pattern"
29
30 The arguments will only be passed to the script specified after npm run
31 and not to any pre or post script.
32
33 The env script is a special built-in command that can be used to list
34 environment variables that will be available to the script at runtime.
35 If an "env" command is defined in your package, it will take precedence
36 over the built-in.
37
38 In addition to the shell's pre-existing PATH, npm run adds node_mod‐
39 ules/.bin to the PATH provided to scripts. Any binaries provided by lo‐
40 cally-installed dependencies can be used without the node_modules/.bin
41 prefix. For example, if there is a devDependency on tap in your pack‐
42 age, you should write:
43
44 "scripts": {"test": "tap test/*.js"}
45
46 instead of
47
48 "scripts": {"test": "node_modules/.bin/tap test/*.js"}
49
50 The actual shell your script is run within is platform dependent. By
51 default, on Unix-like systems it is the /bin/sh command, on Windows it
52 is cmd.exe. The actual shell referred to by /bin/sh also depends on
53 the system. You can customize the shell with the script-shell configu‐
54 ration.
55
56 Scripts are run from the root of the package folder, regardless of what
57 the current working directory is when npm run is called. If you want
58 your script to use different behavior based on what subdirectory you're
59 in, you can use the INIT_CWD environment variable, which holds the full
60 path you were in when you ran npm run.
61
62 npm run sets the NODE environment variable to the node executable with
63 which npm is executed.
64
65 If you try to run a script without having a node_modules directory and
66 it fails, you will be given a warning to run npm install, just in case
67 you've forgotten.
68
69 Workspaces support
70 You may use the workspace or workspaces configs in order to run an ar‐
71 bitrary command from a package's "scripts" object in the context of the
72 specified workspaces. If no "command" is provided, it will list the
73 available scripts for each of these configured workspaces.
74
75 Given a project with configured workspaces, e.g:
76
77 .
78 +-- package.json
79 `-- packages
80 +-- a
81 | `-- package.json
82 +-- b
83 | `-- package.json
84 `-- c
85 `-- package.json
86
87 Assuming the workspace configuration is properly set up at the root
88 level package.json file. e.g:
89
90 {
91 "workspaces": [ "./packages/*" ]
92 }
93
94 And that each of the configured workspaces has a configured test
95 script, we can run tests in all of them using the workspaces config:
96
97 npm test --workspaces
98
99 Filtering workspaces
100 It's also possible to run a script in a single workspace using the
101 workspace config along with a name or directory path:
102
103 npm test --workspace=a
104
105 The workspace config can also be specified multiple times in order to
106 run a specific script in the context of multiple workspaces. When
107 defining values for the workspace config in the command line, it also
108 possible to use -w as a shorthand, e.g:
109
110 npm test -w a -w b
111
112 This last command will run test in both ./packages/a and ./packages/b
113 packages.
114
115 Configuration
116 workspace
117 • Default:
118
119 • Type: String (can be set multiple times)
120
121
122 Enable running a command in the context of the configured workspaces of
123 the current project while filtering by running only the workspaces de‐
124 fined by this configuration option.
125
126 Valid values for the workspace config are either:
127
128 • Workspace names
129
130 • Path to a workspace directory
131
132 • Path to a parent workspace directory (will result in selecting all
133 workspaces within that folder)
134
135
136 When set for the npm init command, this may be set to the folder of a
137 workspace which does not yet exist, to create the folder and set it up
138 as a brand new workspace within the project.
139
140 This value is not exported to the environment for child processes.
141
142 workspaces
143 • Default: null
144
145 • Type: null or Boolean
146
147
148 Set to true to run the command in the context of all configured
149 workspaces.
150
151 Explicitly setting this to false will cause commands like install to
152 ignore workspaces altogether. When not set explicitly:
153
154 • Commands that operate on the node_modules tree (install, update,
155 etc.) will link workspaces into the node_modules folder. - Commands
156 that do other things (test, exec, publish, etc.) will operate on the
157 root project, unless one or more workspaces are specified in the
158 workspace config.
159
160
161 This value is not exported to the environment for child processes.
162
163 include-workspace-root
164 • Default: false
165
166 • Type: Boolean
167
168
169 Include the workspace root when workspaces are enabled for a command.
170
171 When false, specifying individual workspaces via the workspace config,
172 or all workspaces via the workspaces flag, will cause npm to operate
173 only on the specified workspaces, and not on the root project.
174
175 This value is not exported to the environment for child processes.
176
177 if-present
178 • Default: false
179
180 • Type: Boolean
181
182
183 If true, npm will not exit with an error code when run-script is in‐
184 voked for a script that isn't defined in the scripts section of pack‐
185 age.json. This option can be used when it's desirable to optionally
186 run a script when it's present and fail if the script fails. This is
187 useful, for example, when running scripts that may only apply for some
188 builds in an otherwise generic CI setup.
189
190 This value is not exported to the environment for child processes.
191
192 ignore-scripts
193 • Default: false
194
195 • Type: Boolean
196
197
198 If true, npm does not run scripts specified in package.json files.
199
200 Note that commands explicitly intended to run a particular script, such
201 as npm start, npm stop, npm restart, npm test, and npm run-script will
202 still run their intended script if ignore-scripts is set, but they will
203 not run any pre- or post-scripts.
204
205 foreground-scripts
206 • Default: false
207
208 • Type: Boolean
209
210
211 Run all build scripts (ie, preinstall, install, and postinstall)
212 scripts for installed packages in the foreground process, sharing stan‐
213 dard input, output, and error with the main npm process.
214
215 Note that this will generally make installs run slower, and be much
216 noisier, but can be useful for debugging.
217
218 script-shell
219 • Default: '/bin/sh' on POSIX systems, 'cmd.exe' on Windows
220
221 • Type: null or String
222
223
224 The shell to use for scripts run with the npm exec, npm run and npm
225 init <package-spec> commands.
226
227 See Also
228 • npm help scripts
229
230 • npm help test
231
232 • npm help start
233
234 • npm help restart
235
236 • npm help stop
237
238 • npm help config
239
240 • npm help workspaces
241
242
243
244
245 September 2022 NPM-RUN-SCRIPT(1)