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 no
15 "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 the
53 system. You can customize the shell with the script-shell config ⟨/us‐
54 ing-npm/config#script-shell⟩.
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 ⟨/using-npm/config#workspace⟩ or workspaces
71 ⟨/using-npm/config#workspaces⟩ configs in order to run an arbitrary
72 command from a package's "scripts" object in the context of the speci‐
73 fied workspaces. If no "command" is provided, it will list the avail‐
74 able scripts for each of these configured workspaces.
75
76 Given a project with configured workspaces, e.g:
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 ⟨/using-npm/config#workspaces⟩:
97
98 npm test --workspaces
99
100 Filtering workspaces
101 It's also possible to run a script in a single workspace using the
102 workspace config along with a name or directory path:
103
104 npm test --workspace=a
105
106 The workspace config can also be specified multiple times in order to
107 run a specific script in the context of multiple workspaces. When
108 defining values for the workspace config in the command line, it also
109 possible to use -w as a shorthand, e.g:
110
111 npm test -w a -w b
112
113 This last command will run test in both ./packages/a and ./packages/b
114 packages.
115
116 Configuration
117 workspace
118 • Default:
119
120 • Type: String (can be set multiple times)
121
122
123 Enable running a command in the context of the configured workspaces of
124 the current project while filtering by running only the workspaces de‐
125 fined by this configuration option.
126
127 Valid values for the workspace config are either:
128
129 • Workspace names
130
131 • Path to a workspace directory
132
133 • Path to a parent workspace directory (will result in selecting all
134 workspaces within that folder)
135
136
137 When set for the npm init command, this may be set to the folder of a
138 workspace which does not yet exist, to create the folder and set it up
139 as a brand new workspace within the project.
140
141 This value is not exported to the environment for child processes.
142
143 workspaces
144 • Default: null
145
146 • Type: null or Boolean
147
148
149 Set to true to run the command in the context of all configured
150 workspaces.
151
152 Explicitly setting this to false will cause commands like install to
153 ignore workspaces altogether. When not set explicitly:
154
155 • Commands that operate on the node_modules tree (install, update,
156 etc.) will link workspaces into the node_modules folder. - Commands
157 that do other things (test, exec, publish, etc.) will operate on
158 the root project, unless one or more workspaces are specified in
159 the workspace config.
160
161
162 This value is not exported to the environment for child processes.
163
164 include-workspace-root
165 • Default: false
166
167 • Type: Boolean
168
169
170 Include the workspace root when workspaces are enabled for a command.
171
172 When false, specifying individual workspaces via the workspace config,
173 or all workspaces via the workspaces flag, will cause npm to operate
174 only on the specified workspaces, and not on the root project.
175
176 This value is not exported to the environment for child processes.
177
178 if-present
179 • Default: false
180
181 • Type: Boolean
182
183
184 If true, npm will not exit with an error code when run-script is in‐
185 voked for a script that isn't defined in the scripts section of pack‐
186 age.json. This option can be used when it's desirable to optionally run
187 a script when it's present and fail if the script fails. This is use‐
188 ful, for example, when running scripts that may only apply for some
189 builds in an otherwise generic CI setup.
190
191 This value is not exported to the environment for child processes.
192
193 ignore-scripts
194 • Default: false
195
196 • Type: Boolean
197
198
199 If true, npm does not run scripts specified in package.json files.
200
201 Note that commands explicitly intended to run a particular script, such
202 as npm start, npm stop, npm restart, npm test, and npm run-script will
203 still run their intended script if ignore-scripts is set, but they will
204 not run any pre- or post-scripts.
205
206 foreground-scripts
207 • Default: false
208
209 • Type: Boolean
210
211
212 Run all build scripts (ie, preinstall, install, and postinstall)
213 scripts for installed packages in the foreground process, sharing stan‐
214 dard input, output, and error with the main npm process.
215
216 Note that this will generally make installs run slower, and be much
217 noisier, but can be useful for debugging.
218
219 script-shell
220 • Default: '/bin/sh' on POSIX systems, 'cmd.exe' on Windows
221
222 • Type: null or String
223
224
225 The shell to use for scripts run with the npm exec, npm run and npm
226 init <package-spec> commands.
227
228 See Also
229 • npm help scripts
230
231 • npm help test
232
233 • npm help start
234
235 • npm help restart
236
237 • npm help stop
238
239 • npm help config
240
241 • npm help workspaces
242
243
244
245 November 2023 NPM-RUN-SCRIPT(1)