|
| 1 | +--- |
| 2 | +title: npm-install-scripts |
| 3 | +section: 1 |
| 4 | +description: Manage install-script approvals for dependencies |
| 5 | +--- |
| 6 | + |
| 7 | +### Synopsis |
| 8 | + |
| 9 | +```bash |
| 10 | +npm install-scripts approve <pkg> [<pkg> ...] |
| 11 | +npm install-scripts approve --all |
| 12 | +npm install-scripts deny <pkg> [<pkg> ...] |
| 13 | +npm install-scripts deny --all |
| 14 | +npm install-scripts ls |
| 15 | +npm install-scripts prune |
| 16 | +``` |
| 17 | + |
| 18 | +Note: This command is unaware of workspaces. |
| 19 | + |
| 20 | +### Description |
| 21 | + |
| 22 | +Manages the `allowScripts` field in your project's `package.json`, which |
| 23 | +records which of your dependencies are permitted to run install scripts |
| 24 | +(`preinstall`, `install`, `postinstall`, and `prepare` for non-registry |
| 25 | +sources). This is the recommended way to maintain that field. |
| 26 | + |
| 27 | +Dependency install scripts are blocked by default. Install commands |
| 28 | +silently skip lifecycle scripts for any dependency that does not have a |
| 29 | +matching entry in `allowScripts`, and end with a list of the packages |
| 30 | +whose scripts were skipped so you can review them here. |
| 31 | + |
| 32 | +This command only works inside a project that has a `package.json`. Running |
| 33 | +it with `--global` (`-g`) fails with an `EGLOBAL` error, since global |
| 34 | +installs (`npm install -g`) and one-off executions (`npm exec` / `npx`) have |
| 35 | +no project `package.json` to write to. To allow install scripts in those |
| 36 | +contexts, use the `--allow-scripts` flag at install time (for example |
| 37 | +`npm install -g --allow-scripts=canvas,sharp`) or persist the setting with |
| 38 | +`npm config set allow-scripts=canvas,sharp --location=user`. |
| 39 | + |
| 40 | +There are four subcommands: |
| 41 | + |
| 42 | +```bash |
| 43 | +npm install-scripts approve <pkg> [<pkg> ...] |
| 44 | +npm install-scripts approve --all |
| 45 | +npm install-scripts deny <pkg> [<pkg> ...] |
| 46 | +npm install-scripts deny --all |
| 47 | +npm install-scripts ls |
| 48 | +npm install-scripts prune |
| 49 | +``` |
| 50 | + |
| 51 | +`approve` allows install scripts for the named packages. `<pkg>` matches |
| 52 | +every installed version of that package. By default it writes pinned entries |
| 53 | +(`pkg@1.2.3`), which keep their approval narrowed to the specific version you |
| 54 | +reviewed. Pass `--no-allow-scripts-pin` to write name-only entries that allow |
| 55 | +any future version. `--all` approves every package with unreviewed install |
| 56 | +scripts in one go. |
| 57 | + |
| 58 | +`deny` records an explicit denial for the named packages (a name-only `false` |
| 59 | +entry), which survives `npm install-scripts approve --all` and excludes the |
| 60 | +package from any future blanket approval. `--all` denies every package with |
| 61 | +unreviewed install scripts. |
| 62 | + |
| 63 | +`ls` is read-only: it lists every package whose install scripts are not yet |
| 64 | +covered by `allowScripts`, without modifying `package.json`. |
| 65 | + |
| 66 | +`prune` removes `allowScripts` entries that no longer match an installed |
| 67 | +package with an install script, either because the package is no longer |
| 68 | +installed (a transitive dependency changed, or a pinned `pkg@1.2.3` was |
| 69 | +upgraded) or because it no longer has an install script. Both approvals |
| 70 | +(`true`) and denials (`false`) are removed. It edits only the `allowScripts` |
| 71 | +field in `package.json`, never `.npmrc` or `--allow-scripts`. Pass `--dry-run` |
| 72 | +to preview without writing. Unparseable keys are left alone. |
| 73 | + |
| 74 | +`approve` honours the asymmetric pin rule: if you re-approve a package whose |
| 75 | +installed version has changed, the existing pin is rewritten to track the new |
| 76 | +installed version. Multi-version statements (`pkg@1 || 2`) are left alone, |
| 77 | +since they likely capture intent that the command cannot infer. Existing |
| 78 | +`false` entries always win; `approve` will not silently re-allow a package you |
| 79 | +previously denied. |
| 80 | + |
| 81 | +The standalone commands [`npm approve-scripts`](/commands/npm-approve-scripts) |
| 82 | +and [`npm deny-scripts`](/commands/npm-deny-scripts) are aliases for |
| 83 | +`npm install-scripts approve` and `npm install-scripts deny`. |
| 84 | + |
| 85 | +### Examples |
| 86 | + |
| 87 | +```bash |
| 88 | +# Approve all currently-installed install scripts after reviewing them |
| 89 | +npm install-scripts approve --all |
| 90 | + |
| 91 | +# Approve specific packages, pinned to their installed version |
| 92 | +npm install-scripts approve canvas sharp |
| 93 | + |
| 94 | +# Deny a package so it stays blocked |
| 95 | +npm install-scripts deny telemetry-pkg |
| 96 | + |
| 97 | +# Preview which packages still need review |
| 98 | +npm install-scripts ls |
| 99 | + |
| 100 | +# Preview stale allowScripts entries, then remove them |
| 101 | +npm install-scripts prune --dry-run |
| 102 | +npm install-scripts prune |
| 103 | +``` |
| 104 | + |
| 105 | +### Configuration |
| 106 | + |
| 107 | +#### `all` |
| 108 | + |
| 109 | +* Default: false |
| 110 | +* Type: Boolean |
| 111 | + |
| 112 | +Show or act on all packages, not just the ones your project directly depends |
| 113 | +on. For `npm outdated` and `npm ls` this lists every outdated or installed |
| 114 | +package. For `npm approve-scripts` and `npm deny-scripts` it selects every |
| 115 | +package with pending install scripts. |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +#### `allow-scripts-pin` |
| 120 | + |
| 121 | +* Default: true |
| 122 | +* Type: Boolean |
| 123 | + |
| 124 | +Write pinned (`pkg@version`) entries when approving install scripts. Set to |
| 125 | +`false` to write name-only entries that allow any version. Has no effect on |
| 126 | +`npm deny-scripts`, which always writes name-only entries regardless of this |
| 127 | +setting. |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +#### `dry-run` |
| 132 | + |
| 133 | +* Default: false |
| 134 | +* Type: Boolean |
| 135 | + |
| 136 | +Indicates that you don't want npm to make any changes and that it should |
| 137 | +only report what it would have done. This can be passed into any of the |
| 138 | +commands that modify your local installation, eg, `install`, `update`, |
| 139 | +`dedupe`, `uninstall`, as well as `pack` and `publish`. |
| 140 | + |
| 141 | +Note: This is NOT honored by other network related commands, eg `dist-tags`, |
| 142 | +`owner`, etc. |
| 143 | + |
| 144 | + |
| 145 | + |
| 146 | +#### `json` |
| 147 | + |
| 148 | +* Default: false |
| 149 | +* Type: Boolean |
| 150 | + |
| 151 | +Whether or not to output JSON data, rather than the normal output. |
| 152 | + |
| 153 | +* In `npm pkg set` it enables parsing set values with JSON.parse() before |
| 154 | + saving them to your `package.json`. |
| 155 | + |
| 156 | +Not supported by all npm commands. |
| 157 | + |
| 158 | + |
| 159 | + |
| 160 | +### See Also |
| 161 | + |
| 162 | +* [npm approve-scripts](/commands/npm-approve-scripts) |
| 163 | +* [npm deny-scripts](/commands/npm-deny-scripts) |
| 164 | +* [npm install](/commands/npm-install) |
| 165 | +* [npm rebuild](/commands/npm-rebuild) |
| 166 | +* [package.json](/configuring-npm/package-json) |
0 commit comments