UNPKG

npm

Version:

a package manager for JavaScript

335 lines (305 loc) 10.2 kB
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>npm-patch</title> <style> body { background-color: #ffffff; color: #24292e; margin: 0; line-height: 1.5; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji"; } #rainbar { height: 10px; background-image: linear-gradient(139deg, #fb8817, #ff4b01, #c12127, #e02aff); } a { text-decoration: none; color: #0366d6; } a:hover { text-decoration: underline; } pre { margin: 1em 0px; padding: 1em; border: solid 1px #e1e4e8; border-radius: 6px; display: block; overflow: auto; white-space: pre; background-color: #f6f8fa; color: #393a34; } code { font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, Courier, monospace; font-size: 85%; padding: 0.2em 0.4em; background-color: #f6f8fa; color: #393a34; } pre > code { padding: 0; background-color: inherit; color: inherit; } h1, h2, h3 { font-weight: 600; } #logobar { background-color: #333333; margin: 0 auto; padding: 1em 4em; } #logobar .logo { float: left; } #logobar .title { font-weight: 600; color: #dddddd; float: left; margin: 5px 0 0 1em; } #logobar:after { content: ""; display: block; clear: both; } #content { margin: 0 auto; padding: 0 4em; } #table_of_contents > h2 { font-size: 1.17em; } #table_of_contents ul:first-child { border: solid 1px #e1e4e8; border-radius: 6px; padding: 1em; background-color: #f6f8fa; color: #393a34; } #table_of_contents ul { list-style-type: none; padding-left: 1.5em; } #table_of_contents li { font-size: 0.9em; } #table_of_contents li a { color: #000000; } header.title { border-bottom: solid 1px #e1e4e8; } header.title > h1 { margin-bottom: 0.25em; } header.title > .description { display: block; margin-bottom: 0.5em; line-height: 1; } header.title .version { font-size: 0.8em; color: #666666; } footer#edit { border-top: solid 1px #e1e4e8; margin: 3em 0 4em 0; padding-top: 2em; } table { width: 100%; margin: 1em 0; border-radius: 6px; border: 1px solid #e1e4e8; overflow: hidden; border-collapse: separate; border-spacing: 0; } table thead { background-color: #f6f8fa; } table tbody { background-color: #ffffff; } table th, table td { padding: 0.75em; text-align: left; border-right: 1px solid #e1e4e8; border-bottom: 1px solid #e1e4e8; } table th:last-child, table td:last-child { border-right: none; } table tbody tr:last-child td { border-bottom: none; } table th { font-weight: 600; background-color: #f6f8fa; } table code { white-space: nowrap; } </style> </head> <body> <div id="banner"> <div id="rainbar"></div> <div id="logobar"> <svg class="logo" role="img" height="32" width="32" viewBox="0 0 700 700"> <polygon fill="#cb0000" points="0,700 700,700 700,0 0,0"></polygon> <polygon fill="#ffffff" points="150,550 350,550 350,250 450,250 450,550 550,550 550,150 150,150"></polygon> </svg> <div class="title"> npm command-line interface </div> </div> </div> <section id="content"> <header class="title"> <h1 id="----npm-patch----1201"> <span>npm-patch</span> <span class="version">@12.0.1</span> </h1> <span class="description">Apply local patches to installed dependencies</span> </header> <section id="table_of_contents"> <h2 id="table-of-contents">Table of contents</h2> <div id="_table_of_contents"><ul><li><a href="#see-also">See Also</a></li></ul></div> </section> <div id="_content"><h3 id="synopsis">Synopsis</h3> <pre><code class="language-bash">npm patch &lt;pkg&gt;[@&lt;version&gt;] npm patch add &lt;pkg&gt;[@&lt;version&gt;] [--edit-dir &lt;path&gt;] [--ignore-existing] npm patch commit &lt;edit-dir&gt; [--patches-dir &lt;dir&gt;] [--keep-edit-dir] npm patch update &lt;pkg&gt;[@&lt;old-version&gt;] [--to &lt;new-version&gt;] [--patches-dir &lt;dir&gt;] npm patch ls npm patch rm &lt;pkg&gt;[@&lt;version&gt;] </code></pre> <p>Note: This command is unaware of workspaces.</p> <h3 id="description">Description</h3> <p><code>npm patch</code> lets you apply small, local modifications to an installed dependency and have them re-applied automatically on every install. Patches are declared in the <code>patchedDependencies</code> field of your root <code>package.json</code>, stored as plain unified diffs under the <code>patches/</code> directory, and recorded with a content hash in <code>package-lock.json</code>.</p> <p>Because patches are applied during the install itself, they work regardless of <code>install-strategy</code>, apply to transitive dependencies, and are <strong>not</strong> disabled by <code>--ignore-scripts</code>.</p> <p>The bare form <code>npm patch &lt;pkg&gt;</code> is shorthand for <code>npm patch add &lt;pkg&gt;</code>. A package literally named like a subcommand must use the explicit form, e.g. <code>npm patch add add</code>.</p> <ul> <li> <p><code>npm patch add &lt;pkg&gt;[@&lt;version&gt;]</code></p> <p>Prepares a package for editing. npm extracts a clean copy of the resolved package tarball into a temporary directory outside <code>node_modules</code> and prints its path. Edit the files there, then run <code>npm patch commit</code>.</p> <p>If more than one version of <code>&lt;pkg&gt;</code> is installed, re-run with an exact selector such as <code>npm patch add lodash@4.17.21</code>.</p> </li> <li> <p><code>npm patch commit &lt;edit-dir&gt;</code></p> <p>Diffs the edited directory against a clean copy of the original tarball, writes the unified diff to <code>&lt;patches-dir&gt;/&lt;name&gt;@&lt;version&gt;.patch</code>, adds the entry to <code>patchedDependencies</code>, and updates <code>package-lock.json</code>.</p> </li> <li> <p><code>npm patch ls</code></p> <p>Lists registered patches and how many installed nodes each one matches.</p> </li> <li> <p><code>npm patch rm &lt;pkg&gt;[@&lt;version&gt;]</code></p> <p>Removes the matching entries from <code>patchedDependencies</code>, deletes the patch file when no other entry references it, and updates <code>package-lock.json</code>. If <code>&lt;version&gt;</code> is omitted, all entries for <code>&lt;pkg&gt;</code> are removed.</p> </li> </ul> <h3 id="failure-modes">Failure modes</h3> <p>By default any patch problem is a hard error that aborts the install: a patch that fails to apply, a registered patch that matches no installed package, a missing patch file, or a patch whose hash does not match the lockfile.</p> <p>Two CLI-only flags relax this for one-off cases: <code>--allow-unused-patches</code> and <code>--ignore-patch-failures</code>.</p> <h3 id="configuration">Configuration</h3> <h4 id="patches-dir"><code>patches-dir</code></h4> <ul> <li>Default: "patches"</li> <li>Type: String</li> </ul> <p>The directory, relative to the project root, where <code>npm patch commit</code> writes patch files for <code>patchedDependencies</code>.</p> <h4 id="allow-unused-patches"><code>allow-unused-patches</code></h4> <ul> <li>Default: false</li> <li>Type: Boolean</li> </ul> <p>Install even when a registered patch in <code>patchedDependencies</code> matches no installed package. Does not silence patch apply failures.</p> <p>This flag is only honored when passed on the command line; it is ignored in <code>.npmrc</code> and environment variables, and rejected by <code>npm ci</code>.</p> <h4 id="ignore-patch-failures"><code>ignore-patch-failures</code></h4> <ul> <li>Default: false</li> <li>Type: Boolean</li> </ul> <p>Install even when a registered patch fails to apply, with a warning per failure. Intended for incident response only.</p> <p>This flag is only honored when passed on the command line; it is ignored in <code>.npmrc</code> and environment variables, and rejected by <code>npm ci</code>.</p> <h4 id="edit-dir"><code>edit-dir</code></h4> <ul> <li>Default: null</li> <li>Type: null or Path</li> </ul> <p>Override the temporary directory used by <code>npm patch add</code> to prepare a package for editing.</p> <h4 id="ignore-existing"><code>ignore-existing</code></h4> <ul> <li>Default: false</li> <li>Type: Boolean</li> </ul> <p>With <code>npm patch add</code>, discard a previous unfinished edit directory and start fresh.</p> <h4 id="keep-edit-dir"><code>keep-edit-dir</code></h4> <ul> <li>Default: false</li> <li>Type: Boolean</li> </ul> <p>With <code>npm patch commit</code>, do not remove the edit directory after committing the patch.</p> <h4 id="to"><code>to</code></h4> <ul> <li>Default: null</li> <li>Type: null or String</li> </ul> <p>Used by <code>npm patch update</code> to set the version to rebase a patch onto when it cannot be read from <code>package-lock.json</code> — for example an exact-version selector, or a version that has not been installed yet.</p> <h4 id="registry"><code>registry</code></h4> <ul> <li>Default: "<a href="https://registry.npmjs.org/">https://registry.npmjs.org/</a>"</li> <li>Type: URL</li> </ul> <p>The base URL of the npm registry.</p> <h2 id="see-also">See Also</h2> <ul> <li><a href="../commands/npm-install.html">npm install</a></li> <li><a href="../commands/npm-ci.html">npm ci</a></li> <li><a href="../configuring-npm/package-lock-json.html">package-lock.json</a></li> <li><a href="../commands/npm-config.html">config</a></li> </ul></div> <footer id="edit"> <a href="https://github.com/npm/cli/edit/latest/docs/lib/content/commands/npm-patch.md"> <svg role="img" viewBox="0 0 16 16" width="16" height="16" fill="currentcolor" style="vertical-align: text-bottom; margin-right: 0.3em;"> <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> </svg> Edit this page on GitHub </a> </footer> </section> </body></html>