npm-exec.1 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. .TH "NPM\-EXEC" "1" "October 2021" "" ""
  2. .SH "NAME"
  3. \fBnpm-exec\fR \- Run a command from a local or remote npm package
  4. .SS Synopsis
  5. .P
  6. .RS 2
  7. .nf
  8. npm exec \-\- <pkg>[@<version>] [args\.\.\.]
  9. npm exec \-\-package=<pkg>[@<version>] \-\- <cmd> [args\.\.\.]
  10. npm exec \-c '<cmd> [args\.\.\.]'
  11. npm exec \-\-package=foo \-c '<cmd> [args\.\.\.]'
  12. npm exec [\-\-ws] [\-w <workspace\-name] [args\.\.\.]
  13. npx <pkg>[@<specifier>] [args\.\.\.]
  14. npx \-p <pkg>[@<specifier>] <cmd> [args\.\.\.]
  15. npx \-c '<cmd> [args\.\.\.]'
  16. npx \-p <pkg>[@<specifier>] \-c '<cmd> [args\.\.\.]'
  17. Run without \-\-call or positional args to open interactive subshell
  18. alias: npm x, npx
  19. common options:
  20. \-\-package=<pkg> (may be specified multiple times)
  21. \-p is a shorthand for \-\-package only when using npx executable
  22. \-c <cmd> \-\-call=<cmd> (may not be mixed with positional arguments)
  23. .fi
  24. .RE
  25. .SS Description
  26. .P
  27. This command allows you to run an arbitrary command from an npm package
  28. (either one installed locally, or fetched remotely), in a similar context
  29. as running it via \fBnpm run\fP\|\.
  30. .P
  31. Run without positional arguments or \fB\-\-call\fP, this allows you to
  32. interactively run commands in the same sort of shell environment that
  33. \fBpackage\.json\fP scripts are run\. Interactive mode is not supported in CI
  34. environments when standard input is a TTY, to prevent hangs\.
  35. .P
  36. Whatever packages are specified by the \fB\-\-package\fP option will be
  37. provided in the \fBPATH\fP of the executed command, along with any locally
  38. installed package executables\. The \fB\-\-package\fP option may be
  39. specified multiple times, to execute the supplied command in an environment
  40. where all specified packages are available\.
  41. .P
  42. If any requested packages are not present in the local project
  43. dependencies, then they are installed to a folder in the npm cache, which
  44. is added to the \fBPATH\fP environment variable in the executed process\. A
  45. prompt is printed (which can be suppressed by providing either \fB\-\-yes\fP or
  46. \fB\-\-no\fP)\.
  47. .P
  48. Package names provided without a specifier will be matched with whatever
  49. version exists in the local project\. Package names with a specifier will
  50. only be considered a match if they have the exact same name and version as
  51. the local dependency\.
  52. .P
  53. If no \fB\-c\fP or \fB\-\-call\fP option is provided, then the positional arguments
  54. are used to generate the command string\. If no \fB\-\-package\fP options
  55. are provided, then npm will attempt to determine the executable name from
  56. the package specifier provided as the first positional argument according
  57. to the following heuristic:
  58. .RS 0
  59. .IP \(bu 2
  60. If the package has a single entry in its \fBbin\fP field in \fBpackage\.json\fP,
  61. or if all entries are aliases of the same command, then that command
  62. will be used\.
  63. .IP \(bu 2
  64. If the package has multiple \fBbin\fP entries, and one of them matches the
  65. unscoped portion of the \fBname\fP field, then that command will be used\.
  66. .IP \(bu 2
  67. If this does not result in exactly one option (either because there are
  68. no bin entries, or none of them match the \fBname\fP of the package), then
  69. \fBnpm exec\fP exits with an error\.
  70. .RE
  71. .P
  72. To run a binary \fIother than\fR the named binary, specify one or more
  73. \fB\-\-package\fP options, which will prevent npm from inferring the package from
  74. the first command argument\.
  75. .SS \fBnpx\fP vs \fBnpm exec\fP
  76. .P
  77. When run via the \fBnpx\fP binary, all flags and options \fImust\fR be set prior to
  78. any positional arguments\. When run via \fBnpm exec\fP, a double\-hyphen \fB\-\-\fP
  79. flag can be used to suppress npm's parsing of switches and options that
  80. should be sent to the executed command\.
  81. .P
  82. For example:
  83. .P
  84. .RS 2
  85. .nf
  86. $ npx foo@latest bar \-\-package=@npmcli/foo
  87. .fi
  88. .RE
  89. .P
  90. In this case, npm will resolve the \fBfoo\fP package name, and run the
  91. following command:
  92. .P
  93. .RS 2
  94. .nf
  95. $ foo bar \-\-package=@npmcli/foo
  96. .fi
  97. .RE
  98. .P
  99. Since the \fB\-\-package\fP option comes \fIafter\fR the positional arguments, it is
  100. treated as an argument to the executed command\.
  101. .P
  102. In contrast, due to npm's argument parsing logic, running this command is
  103. different:
  104. .P
  105. .RS 2
  106. .nf
  107. $ npm exec foo@latest bar \-\-package=@npmcli/foo
  108. .fi
  109. .RE
  110. .P
  111. In this case, npm will parse the \fB\-\-package\fP option first, resolving the
  112. \fB@npmcli/foo\fP package\. Then, it will execute the following command in that
  113. context:
  114. .P
  115. .RS 2
  116. .nf
  117. $ foo@latest bar
  118. .fi
  119. .RE
  120. .P
  121. The double\-hyphen character is recommended to explicitly tell npm to stop
  122. parsing command line options and switches\. The following command would
  123. thus be equivalent to the \fBnpx\fP command above:
  124. .P
  125. .RS 2
  126. .nf
  127. $ npm exec \-\- foo@latest bar \-\-package=@npmcli/foo
  128. .fi
  129. .RE
  130. .SS Configuration
  131. <!\-\- AUTOGENERATED CONFIG DESCRIPTIONS START \-\->
  132. <!\-\- automatically generated, do not edit manually \-\->
  133. <!\-\- see lib/utils/config/definitions\.js \-\->
  134. .SS \fBpackage\fP
  135. .RS 0
  136. .IP \(bu 2
  137. Default:
  138. .IP \(bu 2
  139. Type: String (can be set multiple times)
  140. .RE
  141. .P
  142. The package to install for npm help \fBexec\fP
  143. <!\-\- automatically generated, do not edit manually \-\->
  144. <!\-\- see lib/utils/config/definitions\.js \-\->
  145. .SS \fBcall\fP
  146. .RS 0
  147. .IP \(bu 2
  148. Default: ""
  149. .IP \(bu 2
  150. Type: String
  151. .RE
  152. .P
  153. Optional companion option for \fBnpm exec\fP, \fBnpx\fP that allows for specifying a
  154. custom command to be run along with the installed packages\.
  155. .P
  156. .RS 2
  157. .nf
  158. npm exec \-\-package yo \-\-package generator\-node \-\-call "yo node"
  159. .fi
  160. .RE
  161. <!\-\- automatically generated, do not edit manually \-\->
  162. <!\-\- see lib/utils/config/definitions\.js \-\->
  163. .SS \fBworkspace\fP
  164. .RS 0
  165. .IP \(bu 2
  166. Default:
  167. .IP \(bu 2
  168. Type: String (can be set multiple times)
  169. .RE
  170. .P
  171. Enable running a command in the context of the configured workspaces of the
  172. current project while filtering by running only the workspaces defined by
  173. this configuration option\.
  174. .P
  175. Valid values for the \fBworkspace\fP config are either:
  176. .RS 0
  177. .IP \(bu 2
  178. Workspace names
  179. .IP \(bu 2
  180. Path to a workspace directory
  181. .IP \(bu 2
  182. Path to a parent workspace directory (will result in selecting all
  183. workspaces within that folder)
  184. .RE
  185. .P
  186. When set for the \fBnpm init\fP command, this may be set to the folder of a
  187. workspace which does not yet exist, to create the folder and set it up as a
  188. brand new workspace within the project\.
  189. .P
  190. This value is not exported to the environment for child processes\.
  191. <!\-\- automatically generated, do not edit manually \-\->
  192. <!\-\- see lib/utils/config/definitions\.js \-\->
  193. .SS \fBworkspaces\fP
  194. .RS 0
  195. .IP \(bu 2
  196. Default: null
  197. .IP \(bu 2
  198. Type: null or Boolean
  199. .RE
  200. .P
  201. Set to true to run the command in the context of \fBall\fR configured
  202. workspaces\.
  203. .P
  204. Explicitly setting this to false will cause commands like \fBinstall\fP to
  205. ignore workspaces altogether\. When not set explicitly:
  206. .RS 0
  207. .IP \(bu 2
  208. Commands that operate on the \fBnode_modules\fP tree (install, update, etc\.)
  209. will link workspaces into the \fBnode_modules\fP folder\. \- Commands that do
  210. other things (test, exec, publish, etc\.) will operate on the root project,
  211. \fIunless\fR one or more workspaces are specified in the \fBworkspace\fP config\.
  212. .RE
  213. .P
  214. This value is not exported to the environment for child processes\.
  215. <!\-\- automatically generated, do not edit manually \-\->
  216. <!\-\- see lib/utils/config/definitions\.js \-\->
  217. .SS \fBinclude\-workspace\-root\fP
  218. .RS 0
  219. .IP \(bu 2
  220. Default: false
  221. .IP \(bu 2
  222. Type: Boolean
  223. .RE
  224. .P
  225. Include the workspace root when workspaces are enabled for a command\.
  226. .P
  227. When false, specifying individual workspaces via the \fBworkspace\fP config, or
  228. all workspaces via the \fBworkspaces\fP flag, will cause npm to operate only on
  229. the specified workspaces, and not on the root project\.
  230. <!\-\- automatically generated, do not edit manually \-\->
  231. <!\-\- see lib/utils/config/definitions\.js \-\->
  232. <!\-\- AUTOGENERATED CONFIG DESCRIPTIONS END \-\->
  233. .SS Examples
  234. .P
  235. Run the version of \fBtap\fP in the local dependencies, with the provided
  236. arguments:
  237. .P
  238. .RS 2
  239. .nf
  240. $ npm exec \-\- tap \-\-bail test/foo\.js
  241. $ npx tap \-\-bail test/foo\.js
  242. .fi
  243. .RE
  244. .P
  245. Run a command \fIother than\fR the command whose name matches the package name
  246. by specifying a \fB\-\-package\fP option:
  247. .P
  248. .RS 2
  249. .nf
  250. $ npm exec \-\-package=foo \-\- bar \-\-bar\-argument
  251. # ~ or ~
  252. $ npx \-\-package=foo bar \-\-bar\-argument
  253. .fi
  254. .RE
  255. .P
  256. Run an arbitrary shell script, in the context of the current project:
  257. .P
  258. .RS 2
  259. .nf
  260. $ npm x \-c 'eslint && say "hooray, lint passed"'
  261. $ npx \-c 'eslint && say "hooray, lint passed"'
  262. .fi
  263. .RE
  264. .SS Workspaces support
  265. .P
  266. You may use the \fBworkspace\fP or \fBworkspaces\fP configs in order to run an
  267. arbitrary command from an npm package (either one installed locally, or fetched
  268. remotely) in the context of the specified workspaces\.
  269. If no positional argument or \fB\-\-call\fP option is provided, it will open an
  270. interactive subshell in the context of each of these configured workspaces one
  271. at a time\.
  272. .P
  273. Given a project with configured workspaces, e\.g:
  274. .P
  275. .RS 2
  276. .nf
  277. \|\.
  278. +\-\- package\.json
  279. `\-\- packages
  280. +\-\- a
  281. | `\-\- package\.json
  282. +\-\- b
  283. | `\-\- package\.json
  284. `\-\- c
  285. `\-\- package\.json
  286. .fi
  287. .RE
  288. .P
  289. Assuming the workspace configuration is properly set up at the root level
  290. \fBpackage\.json\fP file\. e\.g:
  291. .P
  292. .RS 2
  293. .nf
  294. {
  295. "workspaces": [ "\./packages/*" ]
  296. }
  297. .fi
  298. .RE
  299. .P
  300. You can execute an arbitrary command from a package in the context of each of
  301. the configured workspaces when using the \fBworkspaces\fP configuration options,
  302. in this example we're using \fBeslint\fR to lint any js file found within each
  303. workspace folder:
  304. .P
  305. .RS 2
  306. .nf
  307. npm exec \-\-ws \-\- eslint \./*\.js
  308. .fi
  309. .RE
  310. .SS Filtering workspaces
  311. .P
  312. It's also possible to execute a command in a single workspace using the
  313. \fBworkspace\fP config along with a name or directory path:
  314. .P
  315. .RS 2
  316. .nf
  317. npm exec \-\-workspace=a \-\- eslint \./*\.js
  318. .fi
  319. .RE
  320. .P
  321. The \fBworkspace\fP config can also be specified multiple times in order to run a
  322. specific script in the context of multiple workspaces\. When defining values for
  323. the \fBworkspace\fP config in the command line, it also possible to use \fB\-w\fP as a
  324. shorthand, e\.g:
  325. .P
  326. .RS 2
  327. .nf
  328. npm exec \-w a \-w b \-\- eslint \./*\.js
  329. .fi
  330. .RE
  331. .P
  332. This last command will run the \fBeslint\fP command in both \fB\|\./packages/a\fP and
  333. \fB\|\./packages/b\fP folders\.
  334. .SS Compatibility with Older npx Versions
  335. .P
  336. The \fBnpx\fP binary was rewritten in npm v7\.0\.0, and the standalone \fBnpx\fP
  337. package deprecated at that time\. \fBnpx\fP uses the \fBnpm exec\fP
  338. command instead of a separate argument parser and install process, with
  339. some affordances to maintain backwards compatibility with the arguments it
  340. accepted in previous versions\.
  341. .P
  342. This resulted in some shifts in its functionality:
  343. .RS 0
  344. .IP \(bu 2
  345. Any \fBnpm\fP config value may be provided\.
  346. .IP \(bu 2
  347. To prevent security and user\-experience problems from mistyping package
  348. names, \fBnpx\fP prompts before installing anything\. Suppress this
  349. prompt with the \fB\-y\fP or \fB\-\-yes\fP option\.
  350. .IP \(bu 2
  351. The \fB\-\-no\-install\fP option is deprecated, and will be converted to \fB\-\-no\fP\|\.
  352. .IP \(bu 2
  353. Shell fallback functionality is removed, as it is not advisable\.
  354. .IP \(bu 2
  355. The \fB\-p\fP argument is a shorthand for \fB\-\-parseable\fP in npm, but shorthand
  356. for \fB\-\-package\fP in npx\. This is maintained, but only for the \fBnpx\fP
  357. executable\.
  358. .IP \(bu 2
  359. The \fB\-\-ignore\-existing\fP option is removed\. Locally installed bins are
  360. always present in the executed process \fBPATH\fP\|\.
  361. .IP \(bu 2
  362. The \fB\-\-npm\fP option is removed\. \fBnpx\fP will always use the \fBnpm\fP it ships
  363. with\.
  364. .IP \(bu 2
  365. The \fB\-\-node\-arg\fP and \fB\-n\fP options are removed\.
  366. .IP \(bu 2
  367. The \fB\-\-always\-spawn\fP option is redundant, and thus removed\.
  368. .IP \(bu 2
  369. The \fB\-\-shell\fP option is replaced with \fB\-\-script\-shell\fP, but maintained
  370. in the \fBnpx\fP executable for backwards compatibility\.
  371. .RE
  372. .SS A note on caching
  373. .P
  374. The npm cli utilizes its internal package cache when using the package
  375. name specified\. You can use the following to change how and when the
  376. cli uses this cache\. See npm help \fBcache\fP for more on
  377. how the cache works\.
  378. .SS prefer\-online
  379. .P
  380. Forces staleness checks for packages, making the cli look for updates
  381. immediately even if the package is already in the cache\.
  382. .SS prefer\-offline
  383. .P
  384. Bypasses staleness checks for packages\. Missing data will still be
  385. requested from the server\. To force full offline mode, use \fBoffline\fP\|\.
  386. .SS offline
  387. .P
  388. Forces full offline mode\. Any packages not locally cached will result in
  389. an error\.
  390. .SS workspace
  391. .RS 0
  392. .IP \(bu 2
  393. Default:
  394. .IP \(bu 2
  395. Type: String (can be set multiple times)
  396. .RE
  397. .P
  398. Enable running a command in the context of the configured workspaces of the
  399. current project while filtering by running only the workspaces defined by
  400. this configuration option\.
  401. .P
  402. Valid values for the \fBworkspace\fP config are either:
  403. .RS 0
  404. .IP \(bu 2
  405. Workspace names
  406. .IP \(bu 2
  407. Path to a workspace directory
  408. .IP \(bu 2
  409. Path to a parent workspace directory (will result to selecting all of the
  410. nested workspaces)
  411. .RE
  412. .P
  413. This value is not exported to the environment for child processes\.
  414. .SS workspaces
  415. .RS 0
  416. .IP \(bu 2
  417. Alias: \fB\-\-ws\fP
  418. .IP \(bu 2
  419. Type: Boolean
  420. .IP \(bu 2
  421. Default: \fBfalse\fP
  422. .RE
  423. .P
  424. Run scripts in the context of all configured workspaces for the current
  425. project\.
  426. .SS See Also
  427. .RS 0
  428. .IP \(bu 2
  429. npm help run\-script
  430. .IP \(bu 2
  431. npm help scripts
  432. .IP \(bu 2
  433. npm help test
  434. .IP \(bu 2
  435. npm help start
  436. .IP \(bu 2
  437. npm help restart
  438. .IP \(bu 2
  439. npm help stop
  440. .IP \(bu 2
  441. npm help config
  442. .IP \(bu 2
  443. npm help workspaces
  444. .RE