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 mul‐
22 tiple 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 com‐
44 mand will be used.
45
46 • If the package has multiple bin entries, and one of them matches
47 the unscoped portion of the name field, then that command will be
48 used.
49
50 • If this does not result in exactly one option (either because there
51 are no bin entries, or none of them match the name of the package),
52 then npm exec exits with an error.
53
54
55 To run a binary other than the named binary, specify one or more
56 --package options, which will prevent npm from inferring the package
57 from the first command argument.
58
59 npx vs npm exec
60 When run via the npx binary, all flags and options must be set prior to
61 any positional arguments. When run via npm exec, a double-hyphen --
62 flag can be used to suppress npm's parsing of switches and options that
63 should be sent to the executed command.
64
65 For example:
66
67 $ npx foo@latest bar --package=@npmcli/foo
68
69 In this case, npm will resolve the foo package name, and run the fol‐
70 lowing command:
71
72 $ foo bar --package=@npmcli/foo
73
74 Since the --package option comes after the positional arguments, it is
75 treated as an argument to the executed command.
76
77 In contrast, due to npm's argument parsing logic, running this command
78 is different:
79
80 $ npm exec foo@latest bar --package=@npmcli/foo
81
82 In this case, npm will parse the --package option first, resolving the
83 @npmcli/foo package. Then, it will execute the following command in
84 that context:
85
86 $ foo@latest bar
87
88 The double-hyphen character is recommended to explicitly tell npm to
89 stop parsing command line options and switches. The following command
90 would thus be equivalent to the npx command above:
91
92 $ npm exec -- foo@latest bar --package=@npmcli/foo
93
94 Examples
95 Run the version of tap in the local dependencies, with the provided ar‐
96 guments:
97
98 $ npm exec -- tap --bail test/foo.js
99 $ npx tap --bail test/foo.js
100
101 Run a command other than the command whose name matches the package
102 name by specifying a --package option:
103
104 $ npm exec --package=foo -- bar --bar-argument
105 # ~ or ~
106 $ npx --package=foo bar --bar-argument
107
108 Run an arbitrary shell script, in the context of the current project:
109
110 $ npm x -c 'eslint && say "hooray, lint passed"'
111 $ npx -c 'eslint && say "hooray, lint passed"'
112
113 Compatibility with Older npx Versions
114 The npx binary was rewritten in npm v7.0.0, and the standalone npx
115 package deprecated at that time. npx uses the npm exec command instead
116 of a separate argument parser and install process, with some affor‐
117 dances to maintain backwards compatibility with the arguments it ac‐
118 cepted in previous versions.
119
120 This resulted in some shifts in its functionality:
121
122 • Any npm config value may be provided.
123
124 • To prevent security and user-experience problems from mistyping
125 package names, npx prompts before installing anything. Suppress
126 this prompt with the -y or --yes option.
127
128 • The --no-install option is deprecated, and will be converted to
129 --no.
130
131 • Shell fallback functionality is removed, as it is not advisable.
132
133 • The -p argument is a shorthand for --parseable in npm, but short‐
134 hand for --package in npx. This is maintained, but only for the npx
135 executable.
136
137 • The --ignore-existing option is removed. Locally installed bins are
138 always present in the executed process PATH.
139
140 • The --npm option is removed. npx will always use the npm it ships
141 with.
142
143 • The --node-arg and -n options are removed.
144
145 • The --always-spawn option is redundant, and thus removed.
146
147 • The --shell option is replaced with --script-shell, but maintained
148 in the npx executable for backwards compatibility.
149
150
151 See Also
152 • npm help run-script
153
154 • npm help scripts
155
156 • npm help test
157
158 • npm help start
159
160 • npm help restart
161
162 • npm help stop
163
164 • npm help config
165
166 • npm help exec
167
168
169
170 November 2023 NPX(1)