1NPX(1) NPX(1)
2
3
4
6 npx - Run a command from a local or remote npm package
7
8 Synopsis
9 npx -- <pkg>[@<version>] [args...]
10 npx --package=<pkg>[@<version>] -- <cmd> [args...]
11 npx -c '<cmd> [args...]'
12 npx --package=foo -c '<cmd> [args...]'
13
14 Description
15 This command allows you to run an arbitrary command from an npm package
16 (either one installed locally, or fetched remotely), in a similar con‐
17 text as running it via npm run.
18
19 Whatever packages are specified by the --package option will be pro‐
20 vided in the PATH of the executed command, along with any locally in‐
21 stalled package executables. The --package option may be specified
22 multiple times, to execute the supplied command in an environment where
23 all specified packages are available.
24
25 If any requested packages are not present in the local project depen‐
26 dencies, then they are installed to a folder in the npm cache, which is
27 added to the PATH environment variable in the executed process. A
28 prompt is printed (which can be suppressed by providing either --yes or
29 --no).
30
31 Package names provided without a specifier will be matched with what‐
32 ever version exists in the local project. Package names with a speci‐
33 fier will only be considered a match if they have the exact same name
34 and version as the local dependency.
35
36 If no -c or --call option is provided, then the positional arguments
37 are used to generate the command string. If no --package options are
38 provided, then npm will attempt to determine the executable name from
39 the package specifier provided as the first positional argument accord‐
40 ing to the following heuristic:
41
42 • If the package has a single entry in its bin field in package.json,
43 or if all entries are aliases of the same command, then that command
44 will be used.
45
46 • If the package has multiple bin entries, and one of them matches the
47 unscoped portion of the name field, then that command will be used.
48
49 • If this does not result in exactly one option (either because there
50 are no bin entries, or none of them match the name of the package),
51 then npm exec exits with an error.
52
53
54 To run a binary other than the named binary, specify one or more
55 --package options, which will prevent npm from inferring the package
56 from the first command argument.
57
58 npx vs npm exec
59 When run via the npx binary, all flags and options must be set prior to
60 any positional arguments. When run via npm exec, a double-hyphen --
61 flag can be used to suppress npm's parsing of switches and options that
62 should be sent to the executed command.
63
64 For example:
65
66 $ npx foo@latest bar --package=@npmcli/foo
67
68 In this case, npm will resolve the foo package name, and run the fol‐
69 lowing command:
70
71 $ foo bar --package=@npmcli/foo
72
73 Since the --package option comes after the positional arguments, it is
74 treated as an argument to the executed command.
75
76 In contrast, due to npm's argument parsing logic, running this command
77 is different:
78
79 $ npm exec foo@latest bar --package=@npmcli/foo
80
81 In this case, npm will parse the --package option first, resolving the
82 @npmcli/foo package. Then, it will execute the following command in
83 that context:
84
85 $ foo@latest bar
86
87 The double-hyphen character is recommended to explicitly tell npm to
88 stop parsing command line options and switches. The following command
89 would thus be equivalent to the npx command above:
90
91 $ npm exec -- foo@latest bar --package=@npmcli/foo
92
93 Examples
94 Run the version of tap in the local dependencies, with the provided ar‐
95 guments:
96
97 $ npm exec -- tap --bail test/foo.js
98 $ npx tap --bail test/foo.js
99
100 Run a command other than the command whose name matches the package
101 name by specifying a --package option:
102
103 $ npm exec --package=foo -- bar --bar-argument
104 # ~ or ~
105 $ npx --package=foo bar --bar-argument
106
107 Run an arbitrary shell script, in the context of the current project:
108
109 $ npm x -c 'eslint && say "hooray, lint passed"'
110 $ npx -c 'eslint && say "hooray, lint passed"'
111
112 Compatibility with Older npx Versions
113 The npx binary was rewritten in npm v7.0.0, and the standalone npx
114 package deprecated at that time. npx uses the npm exec command instead
115 of a separate argument parser and install process, with some affor‐
116 dances to maintain backwards compatibility with the arguments it ac‐
117 cepted in previous versions.
118
119 This resulted in some shifts in its functionality:
120
121 • Any npm config value may be provided.
122
123 • To prevent security and user-experience problems from mistyping pack‐
124 age names, npx prompts before installing anything. Suppress this
125 prompt with the -y or --yes option.
126
127 • The --no-install option is deprecated, and will be converted to --no.
128
129 • Shell fallback functionality is removed, as it is not advisable.
130
131 • The -p argument is a shorthand for --parseable in npm, but shorthand
132 for --package in npx. This is maintained, but only for the npx exe‐
133 cutable.
134
135 • The --ignore-existing option is removed. Locally installed bins are
136 always present in the executed process PATH.
137
138 • The --npm option is removed. npx will always use the npm it ships
139 with.
140
141 • The --node-arg and -n options are removed.
142
143 • The --always-spawn option is redundant, and thus removed.
144
145 • The --shell option is replaced with --script-shell, but maintained in
146 the npx executable for backwards compatibility.
147
148
149 See Also
150 • npm help run-script
151
152 • npm help scripts
153
154 • npm help test
155
156 • npm help start
157
158 • npm help restart
159
160 • npm help stop
161
162 • npm help config
163
164 • npm help exec
165
166
167
168
169 September 2022 NPX(1)