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