11cb0ef41Sopenharmony_ci<!DOCTYPE html><html><head>
21cb0ef41Sopenharmony_ci<meta charset="utf-8">
31cb0ef41Sopenharmony_ci<title>npm-exec</title>
41cb0ef41Sopenharmony_ci<style>
51cb0ef41Sopenharmony_cibody {
61cb0ef41Sopenharmony_ci    background-color: #ffffff;
71cb0ef41Sopenharmony_ci    color: #24292e;
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci    margin: 0;
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci    line-height: 1.5;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
141cb0ef41Sopenharmony_ci}
151cb0ef41Sopenharmony_ci#rainbar {
161cb0ef41Sopenharmony_ci    height: 10px;
171cb0ef41Sopenharmony_ci    background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff);
181cb0ef41Sopenharmony_ci}
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_cia {
211cb0ef41Sopenharmony_ci    text-decoration: none;
221cb0ef41Sopenharmony_ci    color: #0366d6;
231cb0ef41Sopenharmony_ci}
241cb0ef41Sopenharmony_cia:hover {
251cb0ef41Sopenharmony_ci    text-decoration: underline;
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cipre {
291cb0ef41Sopenharmony_ci    margin: 1em 0px;
301cb0ef41Sopenharmony_ci    padding: 1em;
311cb0ef41Sopenharmony_ci    border: solid 1px #e1e4e8;
321cb0ef41Sopenharmony_ci    border-radius: 6px;
331cb0ef41Sopenharmony_ci
341cb0ef41Sopenharmony_ci    display: block;
351cb0ef41Sopenharmony_ci    overflow: auto;
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ci    white-space: pre;
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci    background-color: #f6f8fa;
401cb0ef41Sopenharmony_ci    color: #393a34;
411cb0ef41Sopenharmony_ci}
421cb0ef41Sopenharmony_cicode {
431cb0ef41Sopenharmony_ci    font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace;
441cb0ef41Sopenharmony_ci    font-size: 85%;
451cb0ef41Sopenharmony_ci    padding: 0.2em 0.4em;
461cb0ef41Sopenharmony_ci    background-color: #f6f8fa;
471cb0ef41Sopenharmony_ci    color: #393a34;
481cb0ef41Sopenharmony_ci}
491cb0ef41Sopenharmony_cipre > code {
501cb0ef41Sopenharmony_ci    padding: 0;
511cb0ef41Sopenharmony_ci    background-color: inherit;
521cb0ef41Sopenharmony_ci    color: inherit;
531cb0ef41Sopenharmony_ci}
541cb0ef41Sopenharmony_cih1, h2, h3 {
551cb0ef41Sopenharmony_ci    font-weight: 600;
561cb0ef41Sopenharmony_ci}
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ci#logobar {
591cb0ef41Sopenharmony_ci    background-color: #333333;
601cb0ef41Sopenharmony_ci    margin: 0 auto;
611cb0ef41Sopenharmony_ci    padding: 1em 4em;
621cb0ef41Sopenharmony_ci}
631cb0ef41Sopenharmony_ci#logobar .logo {
641cb0ef41Sopenharmony_ci    float: left;
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci#logobar .title {
671cb0ef41Sopenharmony_ci    font-weight: 600;
681cb0ef41Sopenharmony_ci    color: #dddddd;
691cb0ef41Sopenharmony_ci    float: left;
701cb0ef41Sopenharmony_ci    margin: 5px 0 0 1em;
711cb0ef41Sopenharmony_ci}
721cb0ef41Sopenharmony_ci#logobar:after {
731cb0ef41Sopenharmony_ci    content: "";
741cb0ef41Sopenharmony_ci    display: block;
751cb0ef41Sopenharmony_ci    clear: both;
761cb0ef41Sopenharmony_ci}
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci#content {
791cb0ef41Sopenharmony_ci    margin: 0 auto;
801cb0ef41Sopenharmony_ci    padding: 0 4em;
811cb0ef41Sopenharmony_ci}
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci#table_of_contents > h2 {
841cb0ef41Sopenharmony_ci    font-size: 1.17em;
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci#table_of_contents ul:first-child {
871cb0ef41Sopenharmony_ci    border: solid 1px #e1e4e8;
881cb0ef41Sopenharmony_ci    border-radius: 6px;
891cb0ef41Sopenharmony_ci    padding: 1em;
901cb0ef41Sopenharmony_ci    background-color: #f6f8fa;
911cb0ef41Sopenharmony_ci    color: #393a34;
921cb0ef41Sopenharmony_ci}
931cb0ef41Sopenharmony_ci#table_of_contents ul {
941cb0ef41Sopenharmony_ci    list-style-type: none;
951cb0ef41Sopenharmony_ci    padding-left: 1.5em;
961cb0ef41Sopenharmony_ci}
971cb0ef41Sopenharmony_ci#table_of_contents li {
981cb0ef41Sopenharmony_ci    font-size: 0.9em;
991cb0ef41Sopenharmony_ci}
1001cb0ef41Sopenharmony_ci#table_of_contents li a {
1011cb0ef41Sopenharmony_ci    color: #000000;
1021cb0ef41Sopenharmony_ci}
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_ciheader.title {
1051cb0ef41Sopenharmony_ci    border-bottom: solid 1px #e1e4e8;
1061cb0ef41Sopenharmony_ci}
1071cb0ef41Sopenharmony_ciheader.title > h1 {
1081cb0ef41Sopenharmony_ci    margin-bottom: 0.25em;
1091cb0ef41Sopenharmony_ci}
1101cb0ef41Sopenharmony_ciheader.title > .description {
1111cb0ef41Sopenharmony_ci    display: block;
1121cb0ef41Sopenharmony_ci    margin-bottom: 0.5em;
1131cb0ef41Sopenharmony_ci    line-height: 1;
1141cb0ef41Sopenharmony_ci}
1151cb0ef41Sopenharmony_ci
1161cb0ef41Sopenharmony_cifooter#edit {
1171cb0ef41Sopenharmony_ci    border-top: solid 1px #e1e4e8;
1181cb0ef41Sopenharmony_ci    margin: 3em 0 4em 0;
1191cb0ef41Sopenharmony_ci    padding-top: 2em;
1201cb0ef41Sopenharmony_ci}
1211cb0ef41Sopenharmony_ci</style>
1221cb0ef41Sopenharmony_ci</head>
1231cb0ef41Sopenharmony_ci<body>
1241cb0ef41Sopenharmony_ci<div id="banner">
1251cb0ef41Sopenharmony_ci<div id="rainbar"></div>
1261cb0ef41Sopenharmony_ci<div id="logobar">
1271cb0ef41Sopenharmony_ci<svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700">
1281cb0ef41Sopenharmony_ci<polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon>
1291cb0ef41Sopenharmony_ci<polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon>
1301cb0ef41Sopenharmony_ci</svg>
1311cb0ef41Sopenharmony_ci<div class="title">
1321cb0ef41Sopenharmony_cinpm command-line interface
1331cb0ef41Sopenharmony_ci</div>
1341cb0ef41Sopenharmony_ci</div>
1351cb0ef41Sopenharmony_ci</div>
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ci<section id="content">
1381cb0ef41Sopenharmony_ci<header class="title">
1391cb0ef41Sopenharmony_ci<h1 id="npm-exec">npm-exec</h1>
1401cb0ef41Sopenharmony_ci<span class="description">Run a command from a local or remote npm package</span>
1411cb0ef41Sopenharmony_ci</header>
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_ci<section id="table_of_contents">
1441cb0ef41Sopenharmony_ci<h2 id="table-of-contents">Table of contents</h2>
1451cb0ef41Sopenharmony_ci<div id="_table_of_contents"><ul><li><a href="#synopsis">Synopsis</a></li><li><a href="#description">Description</a></li><li><a href="#npx-vs-npm-exec"><code>npx</code> vs <code>npm exec</code></a></li><li><a href="#configuration">Configuration</a></li><ul><li><a href="#package"><code>package</code></a></li><li><a href="#call"><code>call</code></a></li><li><a href="#workspace"><code>workspace</code></a></li><li><a href="#workspaces"><code>workspaces</code></a></li><li><a href="#include-workspace-root"><code>include-workspace-root</code></a></li></ul><li><a href="#examples">Examples</a></li><li><a href="#workspaces-support">Workspaces support</a></li><ul><li><a href="#filtering-workspaces">Filtering workspaces</a></li></ul><li><a href="#compatibility-with-older-npx-versions">Compatibility with Older npx Versions</a></li><li><a href="#a-note-on-caching">A note on caching</a></li><ul><li><a href="#prefer-online">prefer-online</a></li><li><a href="#prefer-offline">prefer-offline</a></li><li><a href="#offline">offline</a></li><li><a href="#workspace2">workspace</a></li><li><a href="#workspaces2">workspaces</a></li></ul><li><a href="#see-also">See Also</a></li></ul></div>
1461cb0ef41Sopenharmony_ci</section>
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci<div id="_content"><h3 id="synopsis">Synopsis</h3>
1491cb0ef41Sopenharmony_ci<pre><code class="language-bash">npm exec -- &lt;pkg&gt;[@&lt;version&gt;] [args...]
1501cb0ef41Sopenharmony_cinpm exec --package=&lt;pkg&gt;[@&lt;version&gt;] -- &lt;cmd&gt; [args...]
1511cb0ef41Sopenharmony_cinpm exec -c '&lt;cmd&gt; [args...]'
1521cb0ef41Sopenharmony_cinpm exec --package=foo -c '&lt;cmd&gt; [args...]'
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_cialias: x
1551cb0ef41Sopenharmony_ci</code></pre>
1561cb0ef41Sopenharmony_ci<h3 id="description">Description</h3>
1571cb0ef41Sopenharmony_ci<p>This command allows you to run an arbitrary command from an npm package
1581cb0ef41Sopenharmony_ci(either one installed locally, or fetched remotely), in a similar context
1591cb0ef41Sopenharmony_cias running it via <code>npm run</code>.</p>
1601cb0ef41Sopenharmony_ci<p>Run without positional arguments or <code>--call</code>, this allows you to
1611cb0ef41Sopenharmony_ciinteractively run commands in the same sort of shell environment that
1621cb0ef41Sopenharmony_ci<code>package.json</code> scripts are run.  Interactive mode is not supported in CI
1631cb0ef41Sopenharmony_cienvironments when standard input is a TTY, to prevent hangs.</p>
1641cb0ef41Sopenharmony_ci<p>Whatever packages are specified by the <code>--package</code> option will be
1651cb0ef41Sopenharmony_ciprovided in the <code>PATH</code> of the executed command, along with any locally
1661cb0ef41Sopenharmony_ciinstalled package executables.  The <code>--package</code> option may be
1671cb0ef41Sopenharmony_cispecified multiple times, to execute the supplied command in an environment
1681cb0ef41Sopenharmony_ciwhere all specified packages are available.</p>
1691cb0ef41Sopenharmony_ci<p>If any requested packages are not present in the local project
1701cb0ef41Sopenharmony_cidependencies, then a prompt is printed, which can be suppressed by
1711cb0ef41Sopenharmony_ciproviding either <code>--yes</code> or <code>--no</code>. When standard input is not a TTY or a
1721cb0ef41Sopenharmony_ciCI environment is detected, <code>--yes</code> is assumed. The requested packages are
1731cb0ef41Sopenharmony_ciinstalled to a folder in the npm cache, which is added to the <code>PATH</code>
1741cb0ef41Sopenharmony_cienvironment variable in the executed process.</p>
1751cb0ef41Sopenharmony_ci<p>Package names provided without a specifier will be matched with whatever
1761cb0ef41Sopenharmony_civersion exists in the local project.  Package names with a specifier will
1771cb0ef41Sopenharmony_cionly be considered a match if they have the exact same name and version as
1781cb0ef41Sopenharmony_cithe local dependency.</p>
1791cb0ef41Sopenharmony_ci<p>If no <code>-c</code> or <code>--call</code> option is provided, then the positional arguments
1801cb0ef41Sopenharmony_ciare used to generate the command string.  If no <code>--package</code> options
1811cb0ef41Sopenharmony_ciare provided, then npm will attempt to determine the executable name from
1821cb0ef41Sopenharmony_cithe package specifier provided as the first positional argument according
1831cb0ef41Sopenharmony_cito the following heuristic:</p>
1841cb0ef41Sopenharmony_ci<ul>
1851cb0ef41Sopenharmony_ci<li>If the package has a single entry in its <code>bin</code> field in <code>package.json</code>,
1861cb0ef41Sopenharmony_cior if all entries are aliases of the same command, then that command
1871cb0ef41Sopenharmony_ciwill be used.</li>
1881cb0ef41Sopenharmony_ci<li>If the package has multiple <code>bin</code> entries, and one of them matches the
1891cb0ef41Sopenharmony_ciunscoped portion of the <code>name</code> field, then that command will be used.</li>
1901cb0ef41Sopenharmony_ci<li>If this does not result in exactly one option (either because there are
1911cb0ef41Sopenharmony_cino bin entries, or none of them match the <code>name</code> of the package), then
1921cb0ef41Sopenharmony_ci<code>npm exec</code> exits with an error.</li>
1931cb0ef41Sopenharmony_ci</ul>
1941cb0ef41Sopenharmony_ci<p>To run a binary <em>other than</em> the named binary, specify one or more
1951cb0ef41Sopenharmony_ci<code>--package</code> options, which will prevent npm from inferring the package from
1961cb0ef41Sopenharmony_cithe first command argument.</p>
1971cb0ef41Sopenharmony_ci<h3 id="npx-vs-npm-exec"><code>npx</code> vs <code>npm exec</code></h3>
1981cb0ef41Sopenharmony_ci<p>When run via the <code>npx</code> binary, all flags and options <em>must</em> be set prior to
1991cb0ef41Sopenharmony_ciany positional arguments.  When run via <code>npm exec</code>, a double-hyphen <code>--</code>
2001cb0ef41Sopenharmony_ciflag can be used to suppress npm's parsing of switches and options that
2011cb0ef41Sopenharmony_cishould be sent to the executed command.</p>
2021cb0ef41Sopenharmony_ci<p>For example:</p>
2031cb0ef41Sopenharmony_ci<pre><code>$ npx foo@latest bar --package=@npmcli/foo
2041cb0ef41Sopenharmony_ci</code></pre>
2051cb0ef41Sopenharmony_ci<p>In this case, npm will resolve the <code>foo</code> package name, and run the
2061cb0ef41Sopenharmony_cifollowing command:</p>
2071cb0ef41Sopenharmony_ci<pre><code>$ foo bar --package=@npmcli/foo
2081cb0ef41Sopenharmony_ci</code></pre>
2091cb0ef41Sopenharmony_ci<p>Since the <code>--package</code> option comes <em>after</em> the positional arguments, it is
2101cb0ef41Sopenharmony_citreated as an argument to the executed command.</p>
2111cb0ef41Sopenharmony_ci<p>In contrast, due to npm's argument parsing logic, running this command is
2121cb0ef41Sopenharmony_cidifferent:</p>
2131cb0ef41Sopenharmony_ci<pre><code>$ npm exec foo@latest bar --package=@npmcli/foo
2141cb0ef41Sopenharmony_ci</code></pre>
2151cb0ef41Sopenharmony_ci<p>In this case, npm will parse the <code>--package</code> option first, resolving the
2161cb0ef41Sopenharmony_ci<code>@npmcli/foo</code> package.  Then, it will execute the following command in that
2171cb0ef41Sopenharmony_cicontext:</p>
2181cb0ef41Sopenharmony_ci<pre><code>$ foo@latest bar
2191cb0ef41Sopenharmony_ci</code></pre>
2201cb0ef41Sopenharmony_ci<p>The double-hyphen character is recommended to explicitly tell npm to stop
2211cb0ef41Sopenharmony_ciparsing command line options and switches.  The following command would
2221cb0ef41Sopenharmony_cithus be equivalent to the <code>npx</code> command above:</p>
2231cb0ef41Sopenharmony_ci<pre><code>$ npm exec -- foo@latest bar --package=@npmcli/foo
2241cb0ef41Sopenharmony_ci</code></pre>
2251cb0ef41Sopenharmony_ci<h3 id="configuration">Configuration</h3>
2261cb0ef41Sopenharmony_ci<h4 id="package"><code>package</code></h4>
2271cb0ef41Sopenharmony_ci<ul>
2281cb0ef41Sopenharmony_ci<li>Default:</li>
2291cb0ef41Sopenharmony_ci<li>Type: String (can be set multiple times)</li>
2301cb0ef41Sopenharmony_ci</ul>
2311cb0ef41Sopenharmony_ci<p>The package or packages to install for <a href="../commands/npm-exec.html"><code>npm exec</code></a></p>
2321cb0ef41Sopenharmony_ci<h4 id="call"><code>call</code></h4>
2331cb0ef41Sopenharmony_ci<ul>
2341cb0ef41Sopenharmony_ci<li>Default: ""</li>
2351cb0ef41Sopenharmony_ci<li>Type: String</li>
2361cb0ef41Sopenharmony_ci</ul>
2371cb0ef41Sopenharmony_ci<p>Optional companion option for <code>npm exec</code>, <code>npx</code> that allows for specifying a
2381cb0ef41Sopenharmony_cicustom command to be run along with the installed packages.</p>
2391cb0ef41Sopenharmony_ci<pre><code class="language-bash">npm exec --package yo --package generator-node --call "yo node"
2401cb0ef41Sopenharmony_ci</code></pre>
2411cb0ef41Sopenharmony_ci<h4 id="workspace"><code>workspace</code></h4>
2421cb0ef41Sopenharmony_ci<ul>
2431cb0ef41Sopenharmony_ci<li>Default:</li>
2441cb0ef41Sopenharmony_ci<li>Type: String (can be set multiple times)</li>
2451cb0ef41Sopenharmony_ci</ul>
2461cb0ef41Sopenharmony_ci<p>Enable running a command in the context of the configured workspaces of the
2471cb0ef41Sopenharmony_cicurrent project while filtering by running only the workspaces defined by
2481cb0ef41Sopenharmony_cithis configuration option.</p>
2491cb0ef41Sopenharmony_ci<p>Valid values for the <code>workspace</code> config are either:</p>
2501cb0ef41Sopenharmony_ci<ul>
2511cb0ef41Sopenharmony_ci<li>Workspace names</li>
2521cb0ef41Sopenharmony_ci<li>Path to a workspace directory</li>
2531cb0ef41Sopenharmony_ci<li>Path to a parent workspace directory (will result in selecting all
2541cb0ef41Sopenharmony_ciworkspaces within that folder)</li>
2551cb0ef41Sopenharmony_ci</ul>
2561cb0ef41Sopenharmony_ci<p>When set for the <code>npm init</code> command, this may be set to the folder of a
2571cb0ef41Sopenharmony_ciworkspace which does not yet exist, to create the folder and set it up as a
2581cb0ef41Sopenharmony_cibrand new workspace within the project.</p>
2591cb0ef41Sopenharmony_ci<p>This value is not exported to the environment for child processes.</p>
2601cb0ef41Sopenharmony_ci<h4 id="workspaces"><code>workspaces</code></h4>
2611cb0ef41Sopenharmony_ci<ul>
2621cb0ef41Sopenharmony_ci<li>Default: null</li>
2631cb0ef41Sopenharmony_ci<li>Type: null or Boolean</li>
2641cb0ef41Sopenharmony_ci</ul>
2651cb0ef41Sopenharmony_ci<p>Set to true to run the command in the context of <strong>all</strong> configured
2661cb0ef41Sopenharmony_ciworkspaces.</p>
2671cb0ef41Sopenharmony_ci<p>Explicitly setting this to false will cause commands like <code>install</code> to
2681cb0ef41Sopenharmony_ciignore workspaces altogether. When not set explicitly:</p>
2691cb0ef41Sopenharmony_ci<ul>
2701cb0ef41Sopenharmony_ci<li>Commands that operate on the <code>node_modules</code> tree (install, update, etc.)
2711cb0ef41Sopenharmony_ciwill link workspaces into the <code>node_modules</code> folder. - Commands that do
2721cb0ef41Sopenharmony_ciother things (test, exec, publish, etc.) will operate on the root project,
2731cb0ef41Sopenharmony_ci<em>unless</em> one or more workspaces are specified in the <code>workspace</code> config.</li>
2741cb0ef41Sopenharmony_ci</ul>
2751cb0ef41Sopenharmony_ci<p>This value is not exported to the environment for child processes.</p>
2761cb0ef41Sopenharmony_ci<h4 id="include-workspace-root"><code>include-workspace-root</code></h4>
2771cb0ef41Sopenharmony_ci<ul>
2781cb0ef41Sopenharmony_ci<li>Default: false</li>
2791cb0ef41Sopenharmony_ci<li>Type: Boolean</li>
2801cb0ef41Sopenharmony_ci</ul>
2811cb0ef41Sopenharmony_ci<p>Include the workspace root when workspaces are enabled for a command.</p>
2821cb0ef41Sopenharmony_ci<p>When false, specifying individual workspaces via the <code>workspace</code> config, or
2831cb0ef41Sopenharmony_ciall workspaces via the <code>workspaces</code> flag, will cause npm to operate only on
2841cb0ef41Sopenharmony_cithe specified workspaces, and not on the root project.</p>
2851cb0ef41Sopenharmony_ci<p>This value is not exported to the environment for child processes.</p>
2861cb0ef41Sopenharmony_ci<h3 id="examples">Examples</h3>
2871cb0ef41Sopenharmony_ci<p>Run the version of <code>tap</code> in the local dependencies, with the provided
2881cb0ef41Sopenharmony_ciarguments:</p>
2891cb0ef41Sopenharmony_ci<pre><code>$ npm exec -- tap --bail test/foo.js
2901cb0ef41Sopenharmony_ci$ npx tap --bail test/foo.js
2911cb0ef41Sopenharmony_ci</code></pre>
2921cb0ef41Sopenharmony_ci<p>Run a command <em>other than</em> the command whose name matches the package name
2931cb0ef41Sopenharmony_ciby specifying a <code>--package</code> option:</p>
2941cb0ef41Sopenharmony_ci<pre><code>$ npm exec --package=foo -- bar --bar-argument
2951cb0ef41Sopenharmony_ci# ~ or ~
2961cb0ef41Sopenharmony_ci$ npx --package=foo bar --bar-argument
2971cb0ef41Sopenharmony_ci</code></pre>
2981cb0ef41Sopenharmony_ci<p>Run an arbitrary shell script, in the context of the current project:</p>
2991cb0ef41Sopenharmony_ci<pre><code>$ npm x -c 'eslint &amp;&amp; say "hooray, lint passed"'
3001cb0ef41Sopenharmony_ci$ npx -c 'eslint &amp;&amp; say "hooray, lint passed"'
3011cb0ef41Sopenharmony_ci</code></pre>
3021cb0ef41Sopenharmony_ci<h3 id="workspaces-support">Workspaces support</h3>
3031cb0ef41Sopenharmony_ci<p>You may use the <a href="../using-npm/config#workspace.html"><code>workspace</code></a> or
3041cb0ef41Sopenharmony_ci<a href="../using-npm/config#workspaces.html"><code>workspaces</code></a> configs in order to run an
3051cb0ef41Sopenharmony_ciarbitrary command from an npm package (either one installed locally, or fetched
3061cb0ef41Sopenharmony_ciremotely) in the context of the specified workspaces.
3071cb0ef41Sopenharmony_ciIf no positional argument or <code>--call</code> option is provided, it will open an
3081cb0ef41Sopenharmony_ciinteractive subshell in the context of each of these configured workspaces one
3091cb0ef41Sopenharmony_ciat a time.</p>
3101cb0ef41Sopenharmony_ci<p>Given a project with configured workspaces, e.g:</p>
3111cb0ef41Sopenharmony_ci<pre><code>.
3121cb0ef41Sopenharmony_ci+-- package.json
3131cb0ef41Sopenharmony_ci`-- packages
3141cb0ef41Sopenharmony_ci   +-- a
3151cb0ef41Sopenharmony_ci   |   `-- package.json
3161cb0ef41Sopenharmony_ci   +-- b
3171cb0ef41Sopenharmony_ci   |   `-- package.json
3181cb0ef41Sopenharmony_ci   `-- c
3191cb0ef41Sopenharmony_ci       `-- package.json
3201cb0ef41Sopenharmony_ci</code></pre>
3211cb0ef41Sopenharmony_ci<p>Assuming the workspace configuration is properly set up at the root level
3221cb0ef41Sopenharmony_ci<code>package.json</code> file. e.g:</p>
3231cb0ef41Sopenharmony_ci<pre><code>{
3241cb0ef41Sopenharmony_ci    "workspaces": [ "./packages/*" ]
3251cb0ef41Sopenharmony_ci}
3261cb0ef41Sopenharmony_ci</code></pre>
3271cb0ef41Sopenharmony_ci<p>You can execute an arbitrary command from a package in the context of each of
3281cb0ef41Sopenharmony_cithe configured workspaces when using the
3291cb0ef41Sopenharmony_ci<a href="../using-npm/config#workspace.html"><code>workspaces</code> config options</a>, in this example
3301cb0ef41Sopenharmony_ciwe're using <strong>eslint</strong> to lint any js file found within each workspace folder:</p>
3311cb0ef41Sopenharmony_ci<pre><code>npm exec --ws -- eslint ./*.js
3321cb0ef41Sopenharmony_ci</code></pre>
3331cb0ef41Sopenharmony_ci<h4 id="filtering-workspaces">Filtering workspaces</h4>
3341cb0ef41Sopenharmony_ci<p>It's also possible to execute a command in a single workspace using the
3351cb0ef41Sopenharmony_ci<code>workspace</code> config along with a name or directory path:</p>
3361cb0ef41Sopenharmony_ci<pre><code>npm exec --workspace=a -- eslint ./*.js
3371cb0ef41Sopenharmony_ci</code></pre>
3381cb0ef41Sopenharmony_ci<p>The <code>workspace</code> config can also be specified multiple times in order to run a
3391cb0ef41Sopenharmony_cispecific script in the context of multiple workspaces. When defining values for
3401cb0ef41Sopenharmony_cithe <code>workspace</code> config in the command line, it also possible to use <code>-w</code> as a
3411cb0ef41Sopenharmony_cishorthand, e.g:</p>
3421cb0ef41Sopenharmony_ci<pre><code>npm exec -w a -w b -- eslint ./*.js
3431cb0ef41Sopenharmony_ci</code></pre>
3441cb0ef41Sopenharmony_ci<p>This last command will run the <code>eslint</code> command in both <code>./packages/a</code> and
3451cb0ef41Sopenharmony_ci<code>./packages/b</code> folders.</p>
3461cb0ef41Sopenharmony_ci<h3 id="compatibility-with-older-npx-versions">Compatibility with Older npx Versions</h3>
3471cb0ef41Sopenharmony_ci<p>The <code>npx</code> binary was rewritten in npm v7.0.0, and the standalone <code>npx</code>
3481cb0ef41Sopenharmony_cipackage deprecated at that time.  <code>npx</code> uses the <code>npm exec</code>
3491cb0ef41Sopenharmony_cicommand instead of a separate argument parser and install process, with
3501cb0ef41Sopenharmony_cisome affordances to maintain backwards compatibility with the arguments it
3511cb0ef41Sopenharmony_ciaccepted in previous versions.</p>
3521cb0ef41Sopenharmony_ci<p>This resulted in some shifts in its functionality:</p>
3531cb0ef41Sopenharmony_ci<ul>
3541cb0ef41Sopenharmony_ci<li>Any <code>npm</code> config value may be provided.</li>
3551cb0ef41Sopenharmony_ci<li>To prevent security and user-experience problems from mistyping package
3561cb0ef41Sopenharmony_cinames, <code>npx</code> prompts before installing anything.  Suppress this
3571cb0ef41Sopenharmony_ciprompt with the <code>-y</code> or <code>--yes</code> option.</li>
3581cb0ef41Sopenharmony_ci<li>The <code>--no-install</code> option is deprecated, and will be converted to <code>--no</code>.</li>
3591cb0ef41Sopenharmony_ci<li>Shell fallback functionality is removed, as it is not advisable.</li>
3601cb0ef41Sopenharmony_ci<li>The <code>-p</code> argument is a shorthand for <code>--parseable</code> in npm, but shorthand
3611cb0ef41Sopenharmony_cifor <code>--package</code> in npx.  This is maintained, but only for the <code>npx</code>
3621cb0ef41Sopenharmony_ciexecutable.</li>
3631cb0ef41Sopenharmony_ci<li>The <code>--ignore-existing</code> option is removed.  Locally installed bins are
3641cb0ef41Sopenharmony_cialways present in the executed process <code>PATH</code>.</li>
3651cb0ef41Sopenharmony_ci<li>The <code>--npm</code> option is removed.  <code>npx</code> will always use the <code>npm</code> it ships
3661cb0ef41Sopenharmony_ciwith.</li>
3671cb0ef41Sopenharmony_ci<li>The <code>--node-arg</code> and <code>-n</code> options are removed.</li>
3681cb0ef41Sopenharmony_ci<li>The <code>--always-spawn</code> option is redundant, and thus removed.</li>
3691cb0ef41Sopenharmony_ci<li>The <code>--shell</code> option is replaced with <code>--script-shell</code>, but maintained
3701cb0ef41Sopenharmony_ciin the <code>npx</code> executable for backwards compatibility.</li>
3711cb0ef41Sopenharmony_ci</ul>
3721cb0ef41Sopenharmony_ci<h3 id="a-note-on-caching">A note on caching</h3>
3731cb0ef41Sopenharmony_ci<p>The npm cli utilizes its internal package cache when using the package
3741cb0ef41Sopenharmony_ciname specified.  You can use the following to change how and when the
3751cb0ef41Sopenharmony_cicli uses this cache. See <a href="../commands/npm-cache.html"><code>npm cache</code></a> for more on
3761cb0ef41Sopenharmony_cihow the cache works.</p>
3771cb0ef41Sopenharmony_ci<h4 id="prefer-online">prefer-online</h4>
3781cb0ef41Sopenharmony_ci<p>Forces staleness checks for packages, making the cli look for updates
3791cb0ef41Sopenharmony_ciimmediately even if the package is already in the cache.</p>
3801cb0ef41Sopenharmony_ci<h4 id="prefer-offline">prefer-offline</h4>
3811cb0ef41Sopenharmony_ci<p>Bypasses staleness checks for packages.  Missing data will still be
3821cb0ef41Sopenharmony_cirequested from the server. To force full offline mode, use <code>offline</code>.</p>
3831cb0ef41Sopenharmony_ci<h4 id="offline">offline</h4>
3841cb0ef41Sopenharmony_ci<p>Forces full offline mode. Any packages not locally cached will result in
3851cb0ef41Sopenharmony_cian error.</p>
3861cb0ef41Sopenharmony_ci<h4 id="workspace2">workspace</h4>
3871cb0ef41Sopenharmony_ci<ul>
3881cb0ef41Sopenharmony_ci<li>Default:</li>
3891cb0ef41Sopenharmony_ci<li>Type: String (can be set multiple times)</li>
3901cb0ef41Sopenharmony_ci</ul>
3911cb0ef41Sopenharmony_ci<p>Enable running a command in the context of the configured workspaces of the
3921cb0ef41Sopenharmony_cicurrent project while filtering by running only the workspaces defined by
3931cb0ef41Sopenharmony_cithis configuration option.</p>
3941cb0ef41Sopenharmony_ci<p>Valid values for the <code>workspace</code> config are either:</p>
3951cb0ef41Sopenharmony_ci<ul>
3961cb0ef41Sopenharmony_ci<li>Workspace names</li>
3971cb0ef41Sopenharmony_ci<li>Path to a workspace directory</li>
3981cb0ef41Sopenharmony_ci<li>Path to a parent workspace directory (will result to selecting all of the
3991cb0ef41Sopenharmony_cinested workspaces)</li>
4001cb0ef41Sopenharmony_ci</ul>
4011cb0ef41Sopenharmony_ci<p>This value is not exported to the environment for child processes.</p>
4021cb0ef41Sopenharmony_ci<h4 id="workspaces2">workspaces</h4>
4031cb0ef41Sopenharmony_ci<ul>
4041cb0ef41Sopenharmony_ci<li>Alias: <code>--ws</code></li>
4051cb0ef41Sopenharmony_ci<li>Type: Boolean</li>
4061cb0ef41Sopenharmony_ci<li>Default: <code>false</code></li>
4071cb0ef41Sopenharmony_ci</ul>
4081cb0ef41Sopenharmony_ci<p>Run scripts in the context of all configured workspaces for the current
4091cb0ef41Sopenharmony_ciproject.</p>
4101cb0ef41Sopenharmony_ci<h3 id="see-also">See Also</h3>
4111cb0ef41Sopenharmony_ci<ul>
4121cb0ef41Sopenharmony_ci<li><a href="../commands/npm-run-script.html">npm run-script</a></li>
4131cb0ef41Sopenharmony_ci<li><a href="../using-npm/scripts.html">npm scripts</a></li>
4141cb0ef41Sopenharmony_ci<li><a href="../commands/npm-test.html">npm test</a></li>
4151cb0ef41Sopenharmony_ci<li><a href="../commands/npm-start.html">npm start</a></li>
4161cb0ef41Sopenharmony_ci<li><a href="../commands/npm-restart.html">npm restart</a></li>
4171cb0ef41Sopenharmony_ci<li><a href="../commands/npm-stop.html">npm stop</a></li>
4181cb0ef41Sopenharmony_ci<li><a href="../commands/npm-config.html">npm config</a></li>
4191cb0ef41Sopenharmony_ci<li><a href="../using-npm/workspaces.html">npm workspaces</a></li>
4201cb0ef41Sopenharmony_ci<li><a href="../commands/npx.html">npx</a></li>
4211cb0ef41Sopenharmony_ci</ul></div>
4221cb0ef41Sopenharmony_ci
4231cb0ef41Sopenharmony_ci<footer id="edit">
4241cb0ef41Sopenharmony_ci<a href="https://github.com/npm/cli/edit/latest/docs/content/commands/npm-exec.md">
4251cb0ef41Sopenharmony_ci<svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;">
4261cb0ef41Sopenharmony_ci<path fill-rule="evenodd" d="M11.013 1.427a1.75 1.75 0 012.474 0l1.086 1.086a1.75 1.75 0 010 2.474l-8.61 8.61c-.21.21-.47.364-.756.445l-3.251.93a.75.75 0 01-.927-.928l.929-3.25a1.75 1.75 0 01.445-.758l8.61-8.61zm1.414 1.06a.25.25 0 00-.354 0L10.811 3.75l1.439 1.44 1.263-1.263a.25.25 0 000-.354l-1.086-1.086zM11.189 6.25L9.75 4.81l-6.286 6.287a.25.25 0 00-.064.108l-.558 1.953 1.953-.558a.249.249 0 00.108-.064l6.286-6.286z"></path>
4271cb0ef41Sopenharmony_ci</svg>
4281cb0ef41Sopenharmony_ciEdit this page on GitHub
4291cb0ef41Sopenharmony_ci</a>
4301cb0ef41Sopenharmony_ci</footer>
4311cb0ef41Sopenharmony_ci</section>
4321cb0ef41Sopenharmony_ci
4331cb0ef41Sopenharmony_ci
4341cb0ef41Sopenharmony_ci
4351cb0ef41Sopenharmony_ci</body></html>