npm-run-script.1 8.4 KB

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