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