npx.1 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. .TH "NPX" "1" "September 2021" "" ""
  2. .SH "NAME"
  3. \fBnpx\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. npx <pkg>[@<specifier>] [args\.\.\.]
  13. npx \-p <pkg>[@<specifier>] <cmd> [args\.\.\.]
  14. npx \-c '<cmd> [args\.\.\.]'
  15. npx \-p <pkg>[@<specifier>] \-c '<cmd> [args\.\.\.]'
  16. alias: npm x, npx
  17. \-\-package=<pkg> (may be specified multiple times)
  18. \-p is a shorthand for \-\-package only when using npx executable
  19. \-c <cmd> \-\-call=<cmd> (may not be mixed with positional arguments)
  20. .fi
  21. .RE
  22. .SS Description
  23. .P
  24. This command allows you to run an arbitrary command from an npm package
  25. (either one installed locally, or fetched remotely), in a similar context
  26. as running it via \fBnpm run\fP\|\.
  27. .P
  28. Whatever packages are specified by the \fB\-\-package\fP option will be
  29. provided in the \fBPATH\fP of the executed command, along with any locally
  30. installed package executables\. The \fB\-\-package\fP option may be
  31. specified multiple times, to execute the supplied command in an environment
  32. where all specified packages are available\.
  33. .P
  34. If any requested packages are not present in the local project
  35. dependencies, then they are installed to a folder in the npm cache, which
  36. is added to the \fBPATH\fP environment variable in the executed process\. A
  37. prompt is printed (which can be suppressed by providing either \fB\-\-yes\fP or
  38. \fB\-\-no\fP)\.
  39. .P
  40. Package names provided without a specifier will be matched with whatever
  41. version exists in the local project\. Package names with a specifier will
  42. only be considered a match if they have the exact same name and version as
  43. the local dependency\.
  44. .P
  45. If no \fB\-c\fP or \fB\-\-call\fP option is provided, then the positional arguments
  46. are used to generate the command string\. If no \fB\-\-package\fP options
  47. are provided, then npm will attempt to determine the executable name from
  48. the package specifier provided as the first positional argument according
  49. to the following heuristic:
  50. .RS 0
  51. .IP \(bu 2
  52. If the package has a single entry in its \fBbin\fP field in \fBpackage\.json\fP,
  53. or if all entries are aliases of the same command, then that command
  54. will be used\.
  55. .IP \(bu 2
  56. If the package has multiple \fBbin\fP entries, and one of them matches the
  57. unscoped portion of the \fBname\fP field, then that command will be used\.
  58. .IP \(bu 2
  59. If this does not result in exactly one option (either because there are
  60. no bin entries, or none of them match the \fBname\fP of the package), then
  61. \fBnpm exec\fP exits with an error\.
  62. .RE
  63. .P
  64. To run a binary \fIother than\fR the named binary, specify one or more
  65. \fB\-\-package\fP options, which will prevent npm from inferring the package from
  66. the first command argument\.
  67. .SS \fBnpx\fP vs \fBnpm exec\fP
  68. .P
  69. When run via the \fBnpx\fP binary, all flags and options \fImust\fR be set prior to
  70. any positional arguments\. When run via \fBnpm exec\fP, a double\-hyphen \fB\-\-\fP
  71. flag can be used to suppress npm's parsing of switches and options that
  72. should be sent to the executed command\.
  73. .P
  74. For example:
  75. .P
  76. .RS 2
  77. .nf
  78. $ npx foo@latest bar \-\-package=@npmcli/foo
  79. .fi
  80. .RE
  81. .P
  82. In this case, npm will resolve the \fBfoo\fP package name, and run the
  83. following command:
  84. .P
  85. .RS 2
  86. .nf
  87. $ foo bar \-\-package=@npmcli/foo
  88. .fi
  89. .RE
  90. .P
  91. Since the \fB\-\-package\fP option comes \fIafter\fR the positional arguments, it is
  92. treated as an argument to the executed command\.
  93. .P
  94. In contrast, due to npm's argument parsing logic, running this command is
  95. different:
  96. .P
  97. .RS 2
  98. .nf
  99. $ npm exec foo@latest bar \-\-package=@npmcli/foo
  100. .fi
  101. .RE
  102. .P
  103. In this case, npm will parse the \fB\-\-package\fP option first, resolving the
  104. \fB@npmcli/foo\fP package\. Then, it will execute the following command in that
  105. context:
  106. .P
  107. .RS 2
  108. .nf
  109. $ foo@latest bar
  110. .fi
  111. .RE
  112. .P
  113. The double\-hyphen character is recommended to explicitly tell npm to stop
  114. parsing command line options and switches\. The following command would
  115. thus be equivalent to the \fBnpx\fP command above:
  116. .P
  117. .RS 2
  118. .nf
  119. $ npm exec \-\- foo@latest bar \-\-package=@npmcli/foo
  120. .fi
  121. .RE
  122. .SS Examples
  123. .P
  124. Run the version of \fBtap\fP in the local dependencies, with the provided
  125. arguments:
  126. .P
  127. .RS 2
  128. .nf
  129. $ npm exec \-\- tap \-\-bail test/foo\.js
  130. $ npx tap \-\-bail test/foo\.js
  131. .fi
  132. .RE
  133. .P
  134. Run a command \fIother than\fR the command whose name matches the package name
  135. by specifying a \fB\-\-package\fP option:
  136. .P
  137. .RS 2
  138. .nf
  139. $ npm exec \-\-package=foo \-\- bar \-\-bar\-argument
  140. # ~ or ~
  141. $ npx \-\-package=foo bar \-\-bar\-argument
  142. .fi
  143. .RE
  144. .P
  145. Run an arbitrary shell script, in the context of the current project:
  146. .P
  147. .RS 2
  148. .nf
  149. $ npm x \-c 'eslint && say "hooray, lint passed"'
  150. $ npx \-c 'eslint && say "hooray, lint passed"'
  151. .fi
  152. .RE
  153. .SS Compatibility with Older npx Versions
  154. .P
  155. The \fBnpx\fP binary was rewritten in npm v7\.0\.0, and the standalone \fBnpx\fP
  156. package deprecated at that time\. \fBnpx\fP uses the \fBnpm exec\fP
  157. command instead of a separate argument parser and install process, with
  158. some affordances to maintain backwards compatibility with the arguments it
  159. accepted in previous versions\.
  160. .P
  161. This resulted in some shifts in its functionality:
  162. .RS 0
  163. .IP \(bu 2
  164. Any \fBnpm\fP config value may be provided\.
  165. .IP \(bu 2
  166. To prevent security and user\-experience problems from mistyping package
  167. names, \fBnpx\fP prompts before installing anything\. Suppress this
  168. prompt with the \fB\-y\fP or \fB\-\-yes\fP option\.
  169. .IP \(bu 2
  170. The \fB\-\-no\-install\fP option is deprecated, and will be converted to \fB\-\-no\fP\|\.
  171. .IP \(bu 2
  172. Shell fallback functionality is removed, as it is not advisable\.
  173. .IP \(bu 2
  174. The \fB\-p\fP argument is a shorthand for \fB\-\-parseable\fP in npm, but shorthand
  175. for \fB\-\-package\fP in npx\. This is maintained, but only for the \fBnpx\fP
  176. executable\.
  177. .IP \(bu 2
  178. The \fB\-\-ignore\-existing\fP option is removed\. Locally installed bins are
  179. always present in the executed process \fBPATH\fP\|\.
  180. .IP \(bu 2
  181. The \fB\-\-npm\fP option is removed\. \fBnpx\fP will always use the \fBnpm\fP it ships
  182. with\.
  183. .IP \(bu 2
  184. The \fB\-\-node\-arg\fP and \fB\-n\fP options are removed\.
  185. .IP \(bu 2
  186. The \fB\-\-always\-spawn\fP option is redundant, and thus removed\.
  187. .IP \(bu 2
  188. The \fB\-\-shell\fP option is replaced with \fB\-\-script\-shell\fP, but maintained
  189. in the \fBnpx\fP executable for backwards compatibility\.
  190. .RE
  191. .SS See Also
  192. .RS 0
  193. .IP \(bu 2
  194. npm help run\-script
  195. .IP \(bu 2
  196. npm help scripts
  197. .IP \(bu 2
  198. npm help test
  199. .IP \(bu 2
  200. npm help start
  201. .IP \(bu 2
  202. npm help restart
  203. .IP \(bu 2
  204. npm help stop
  205. .IP \(bu 2
  206. npm help config
  207. .RE