reggi/dev-engines
@reggi/path-to-regexp
dependabot/npm_and_yarn/main/copy-to-clipboard-4.0.2
dependabot/npm_and_yarn/main/eslint-10.4.0
dependabot/npm_and_yarn/main/npmcli/eslint-config-7.0.0
dependabot/npm_and_yarn/main/proc-log-7.0.0
dependabot/npm_and_yarn/npm_and_yarn-826852524d
dependabot/npm_and_yarn/npm_and_yarn-ab9a7f4bc2
deprecate-totp-2fa
dhei/classic-tokens
gat-bypass-2fa-docs
jpg619/fix-accessibility-content-flow
jpg619/version-bump-tar-2
kartykp/gat-bypass-2fa-docs
kartykp/upgrade-path-to-regex
main
maitxn/version-bump-tar
patch-1
reggi/cache-based-on-version
reggi/dev-engines
reggi/fix-transform-prettier
reggi/overrides
update-search-sensitivity
| 1 | --- |
| 2 | title: npm-diff |
| 3 | section: 1 |
| 4 | description: The registry diff command |
| 5 | github_repo: npm/cli |
| 6 | github_branch: release/v9 |
| 7 | github_path: docs/lib/content/commands/npm-diff.md |
| 8 | redirect_from: |
| 9 | - /cli-documentation/v9/cli-commands/diff |
| 10 | - /cli-documentation/v9/cli-commands/npm-diff |
| 11 | - /cli-documentation/v9/commands/diff |
| 12 | - /cli-documentation/v9/commands/npm-diff |
| 13 | - /cli-documentation/v9/diff |
| 14 | - /cli-documentation/v9/npm-diff |
| 15 | - /cli/v9/cli-commands/diff |
| 16 | - /cli/v9/cli-commands/npm-diff |
| 17 | - /cli/v9/commands/diff |
| 18 | - /cli/v9/diff |
| 19 | - /cli/v9/npm-diff |
| 20 | --- |
| 21 | |
| 22 | ### Synopsis |
| 23 | |
| 24 | ```bash |
| 25 | npm diff [...<paths>] |
| 26 | ``` |
| 27 | |
| 28 | ### Description |
| 29 | |
| 30 | Similar to its `git diff` counterpart, this command will print diff patches of files for packages published to the npm registry. |
| 31 | |
| 32 | - `npm diff --diff=<spec-a> --diff=<spec-b>` |
| 33 | |
| 34 | Compares two package versions using their registry specifiers, e.g: `npm diff --diff=pkg@1.0.0 --diff=pkg@^2.0.0`. It's also possible to compare across forks of any package, e.g: `npm diff --diff=pkg@1.0.0 --diff=pkg-fork@1.0.0`. |
| 35 | |
| 36 | Any valid spec can be used, so that it's also possible to compare directories or git repositories, e.g: `npm diff --diff=pkg@latest --diff=./packages/pkg` |
| 37 | |
| 38 | Here's an example comparing two different versions of a package named `abbrev` from the registry: |
| 39 | |
| 40 | ```bash |
| 41 | npm diff --diff=abbrev@1.1.0 --diff=abbrev@1.1.1 |
| 42 | ``` |
| 43 | |
| 44 | On success, output looks like: |
| 45 | |
| 46 | ```bash |
| 47 | diff --git a/package.json b/package.json |
| 48 | index v1.1.0..v1.1.1 100644 |
| 49 | --- a/package.json |
| 50 | +++ b/package.json |
| 51 | @@ -1,6 +1,6 @@ |
| 52 | { |
| 53 | "name": "abbrev", |
| 54 | - "version": "1.1.0", |
| 55 | + "version": "1.1.1", |
| 56 | "description": "Like ruby's abbrev module, but in js", |
| 57 | "author": "Isaac Z. Schlueter <i@izs.me>", |
| 58 | "main": "abbrev.js", |
| 59 | ``` |
| 60 | |
| 61 | Given the flexible nature of npm specs, you can also target local directories or git repos just like when using `npm install`: |
| 62 | |
| 63 | ```bash |
| 64 | npm diff --diff=https://github.com/npm/libnpmdiff --diff=./local-path |
| 65 | ``` |
| 66 | |
| 67 | In the example above we can compare the contents from the package installed from the git repo at `github.com/npm/libnpmdiff` with the contents of the `./local-path` that contains a valid package, such as a modified copy of the original. |
| 68 | |
| 69 | - `npm diff` (in a package directory, no arguments): |
| 70 | |
| 71 | If the package is published to the registry, `npm diff` will fetch the tarball version tagged as `latest` (this value can be configured using the `tag` option) and proceed to compare the contents of files present in that tarball, with the current files in your local file system. |
| 72 | |
| 73 | This workflow provides a handy way for package authors to see what package-tracked files have been changed in comparison with the latest published version of that package. |
| 74 | |
| 75 | - `npm diff --diff=<pkg-name>` (in a package directory): |
| 76 | |
| 77 | When using a single package name (with no version or tag specifier) as an argument, `npm diff` will work in a similar way to [`npm-outdated`](npm-outdated) and reach for the registry to figure out what current published version of the package named `<pkg-name>` will satisfy its dependent declared semver-range. Once that specific version is known `npm diff` will print diff patches comparing the current version of `<pkg-name>` found in the local file system with that specific version returned by the registry. |
| 78 | |
| 79 | Given a package named `abbrev` that is currently installed: |
| 80 | |
| 81 | ```bash |
| 82 | npm diff --diff=abbrev |
| 83 | ``` |
| 84 | |
| 85 | That will request from the registry its most up to date version and will print a diff output comparing the currently installed version to this newer one if the version numbers are not the same. |
| 86 | |
| 87 | - `npm diff --diff=<spec-a>` (in a package directory): |
| 88 | |
| 89 | Similar to using only a single package name, it's also possible to declare a full registry specifier version if you wish to compare the local version of an installed package with the specific version/tag/semver-range provided in `<spec-a>`. |
| 90 | |
| 91 | An example: assuming `pkg@1.0.0` is installed in the current `node_modules` folder, running: |
| 92 | |
| 93 | ```bash |
| 94 | npm diff --diff=pkg@2.0.0 |
| 95 | ``` |
| 96 | |
| 97 | It will effectively be an alias to `npm diff --diff=pkg@1.0.0 --diff=pkg@2.0.0`. |
| 98 | |
| 99 | - `npm diff --diff=<semver-a> [--diff=<semver-b>]` (in a package directory): |
| 100 | |
| 101 | Using `npm diff` along with semver-valid version numbers is a shorthand to compare different versions of the current package. |
| 102 | |
| 103 | It needs to be run from a package directory, such that for a package named `pkg` running `npm diff --diff=1.0.0 --diff=1.0.1` is the same as running `npm diff --diff=pkg@1.0.0 --diff=pkg@1.0.1`. |
| 104 | |
| 105 | If only a single argument `<version-a>` is provided, then the current local file system is going to be compared against that version. |
| 106 | |
| 107 | Here's an example comparing two specific versions (published to the configured registry) of the current project directory: |
| 108 | |
| 109 | ```bash |
| 110 | npm diff --diff=1.0.0 --diff=1.1.0 |
| 111 | ``` |
| 112 | |
| 113 | Note that tag names are not valid `--diff` argument values, if you wish to compare to a published tag, you must use the `pkg@tagname` syntax. |
| 114 | |
| 115 | #### Filtering files |
| 116 | |
| 117 | It's possible to also specify positional arguments using file names or globs pattern matching in order to limit the result of diff patches to only a subset of files for a given package, e.g: |
| 118 | |
| 119 | ```bash |
| 120 | npm diff --diff=pkg@2 ./lib/ CHANGELOG.md |
| 121 | ``` |
| 122 | |
| 123 | In the example above the diff output is only going to print contents of files located within the folder `./lib/` and changed lines of code within the `CHANGELOG.md` file. |
| 124 | |
| 125 | ### Configuration |
| 126 | |
| 127 | #### `diff` |
| 128 | |
| 129 | - Default: |
| 130 | - Type: String (can be set multiple times) |
| 131 | |
| 132 | Define arguments to compare in `npm diff`. |
| 133 | |
| 134 | #### `diff-name-only` |
| 135 | |
| 136 | - Default: false |
| 137 | - Type: Boolean |
| 138 | |
| 139 | Prints only filenames when using `npm diff`. |
| 140 | |
| 141 | #### `diff-unified` |
| 142 | |
| 143 | - Default: 3 |
| 144 | - Type: Number |
| 145 | |
| 146 | The number of lines of context to print in `npm diff`. |
| 147 | |
| 148 | #### `diff-ignore-all-space` |
| 149 | |
| 150 | - Default: false |
| 151 | - Type: Boolean |
| 152 | |
| 153 | Ignore whitespace when comparing lines in `npm diff`. |
| 154 | |
| 155 | #### `diff-no-prefix` |
| 156 | |
| 157 | - Default: false |
| 158 | - Type: Boolean |
| 159 | |
| 160 | Do not show any source or destination prefix in `npm diff` output. |
| 161 | |
| 162 | Note: this causes `npm diff` to ignore the `--diff-src-prefix` and `--diff-dst-prefix` configs. |
| 163 | |
| 164 | #### `diff-src-prefix` |
| 165 | |
| 166 | - Default: "a/" |
| 167 | - Type: String |
| 168 | |
| 169 | Source prefix to be used in `npm diff` output. |
| 170 | |
| 171 | #### `diff-dst-prefix` |
| 172 | |
| 173 | - Default: "b/" |
| 174 | - Type: String |
| 175 | |
| 176 | Destination prefix to be used in `npm diff` output. |
| 177 | |
| 178 | #### `diff-text` |
| 179 | |
| 180 | - Default: false |
| 181 | - Type: Boolean |
| 182 | |
| 183 | Treat all files as text in `npm diff`. |
| 184 | |
| 185 | #### `global` |
| 186 | |
| 187 | - Default: false |
| 188 | - Type: Boolean |
| 189 | |
| 190 | Operates in "global" mode, so that packages are installed into the `prefix` folder instead of the current working directory. See [folders](/cli/v9/configuring-npm/folders) for more on the differences in behavior. |
| 191 | |
| 192 | - packages are installed into the `{prefix}/lib/node_modules` folder, instead of the current working directory. |
| 193 | - bin files are linked to `{prefix}/bin` |
| 194 | - man pages are linked to `{prefix}/share/man` |
| 195 | |
| 196 | #### `tag` |
| 197 | |
| 198 | - Default: "latest" |
| 199 | - Type: String |
| 200 | |
| 201 | If you ask npm to install a package and don't tell it a specific version, then it will install the specified tag. |
| 202 | |
| 203 | Also the tag that is added to the package@version specified by the `npm tag` command, if no explicit tag is given. |
| 204 | |
| 205 | When used by the `npm diff` command, this is the tag used to fetch the tarball that will be compared with the local files by default. |
| 206 | |
| 207 | #### `workspace` |
| 208 | |
| 209 | - Default: |
| 210 | - Type: String (can be set multiple times) |
| 211 | |
| 212 | Enable running a command in the context of the configured workspaces of the current project while filtering by running only the workspaces defined by this configuration option. |
| 213 | |
| 214 | Valid values for the `workspace` config are either: |
| 215 | |
| 216 | - Workspace names |
| 217 | - Path to a workspace directory |
| 218 | - Path to a parent workspace directory (will result in selecting all workspaces within that folder) |
| 219 | |
| 220 | When set for the `npm init` command, this may be set to the folder of a workspace which does not yet exist, to create the folder and set it up as a brand new workspace within the project. |
| 221 | |
| 222 | This value is not exported to the environment for child processes. |
| 223 | |
| 224 | #### `workspaces` |
| 225 | |
| 226 | - Default: null |
| 227 | - Type: null or Boolean |
| 228 | |
| 229 | Set to true to run the command in the context of **all** configured workspaces. |
| 230 | |
| 231 | Explicitly setting this to false will cause commands like `install` to ignore workspaces altogether. When not set explicitly: |
| 232 | |
| 233 | - Commands that operate on the `node_modules` tree (install, update, etc.) will link workspaces into the `node_modules` folder. - Commands that do other things (test, exec, publish, etc.) will operate on the root project, _unless_ one or more workspaces are specified in the `workspace` config. |
| 234 | |
| 235 | This value is not exported to the environment for child processes. |
| 236 | |
| 237 | #### `include-workspace-root` |
| 238 | |
| 239 | - Default: false |
| 240 | - Type: Boolean |
| 241 | |
| 242 | Include the workspace root when workspaces are enabled for a command. |
| 243 | |
| 244 | When false, specifying individual workspaces via the `workspace` config, or all workspaces via the `workspaces` flag, will cause npm to operate only on the specified workspaces, and not on the root project. |
| 245 | |
| 246 | This value is not exported to the environment for child processes. |
| 247 | |
| 248 | ## See Also |
| 249 | |
| 250 | - [npm outdated](/cli/v9/commands/npm-outdated) |
| 251 | - [npm install](/cli/v9/commands/npm-install) |
| 252 | - [npm config](/cli/v9/commands/npm-config) |
| 253 | - [npm registry](/cli/v9/using-npm/registry) |