1 ---
2 title: npm-diff
3 section: 1
4 description: The registry diff command
5 github_repo: npm/cli
6 github_branch: latest
7 github_path: docs/lib/content/commands/npm-diff.md
8 redirect_from:
9 - /cli-commands/diff
10 - /cli-commands/npm-diff
11 - /cli-documentation/cli-commands/diff
12 - /cli-documentation/cli-commands/npm-diff
13 - /cli-documentation/commands/diff
14 - /cli-documentation/commands/npm-diff
15 - /cli-documentation/diff
16 - /cli-documentation/npm-diff
17 - /cli-documentation/v10/cli-commands/diff
18 - /cli-documentation/v10/cli-commands/npm-diff
19 - /cli-documentation/v10/commands/diff
20 - /cli-documentation/v10/commands/npm-diff
21 - /cli-documentation/v10/diff
22 - /cli-documentation/v10/npm-diff
23 - /cli/cli-commands/diff
24 - /cli/cli-commands/npm-diff
25 - /cli/commands/diff
26 - /cli/commands/npm-diff
27 - /cli/diff
28 - /cli/npm-diff
29 - /cli/v10/cli-commands/diff
30 - /cli/v10/cli-commands/npm-diff
31 - /cli/v10/commands/diff
32 - /cli/v10/diff
33 - /cli/v10/npm-diff
34 - /commands/diff
35 - /commands/npm-diff
36 ---
37
38 ### Synopsis
39
40 ```bash
41 npm diff [...<paths>]
42 ```
43
44 ### Description
45
46 Similar to its `git diff` counterpart, this command will print diff patches of files for packages published to the npm registry.
47
48 - `npm diff --diff=<spec-a> --diff=<spec-b>`
49
50 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`.
51
52 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`
53
54 Here's an example comparing two different versions of a package named `abbrev` from the registry:
55
56 ```bash
57 npm diff --diff=abbrev@1.1.0 --diff=abbrev@1.1.1
58 ```
59
60 On success, output looks like:
61
62 ```bash
63 diff --git a/package.json b/package.json
64 index v1.1.0..v1.1.1 100644
65 --- a/package.json
66 +++ b/package.json
67 @@ -1,6 +1,6 @@
68 {
69 "name": "abbrev",
70 - "version": "1.1.0",
71 + "version": "1.1.1",
72 "description": "Like ruby's abbrev module, but in js",
73 "author": "Isaac Z. Schlueter <i@izs.me>",
74 "main": "abbrev.js",
75 ```
76
77 Given the flexible nature of npm specs, you can also target local directories or git repos just like when using `npm install`:
78
79 ```bash
80 npm diff --diff=https://github.com/npm/libnpmdiff --diff=./local-path
81 ```
82
83 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.
84
85 - `npm diff` (in a package directory, no arguments):
86
87 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.
88
89 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.
90
91 - `npm diff --diff=<pkg-name>` (in a package directory):
92
93 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.
94
95 Given a package named `abbrev` that is currently installed:
96
97 ```bash
98 npm diff --diff=abbrev
99 ```
100
101 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.
102
103 - `npm diff --diff=<spec-a>` (in a package directory):
104
105 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>`.
106
107 An example: assuming `pkg@1.0.0` is installed in the current `node_modules` folder, running:
108
109 ```bash
110 npm diff --diff=pkg@2.0.0
111 ```
112
113 It will effectively be an alias to `npm diff --diff=pkg@1.0.0 --diff=pkg@2.0.0`.
114
115 - `npm diff --diff=<semver-a> [--diff=<semver-b>]` (in a package directory):
116
117 Using `npm diff` along with semver-valid version numbers is a shorthand 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 `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`.
120
121 If only a single argument `<version-a>` is provided, then the current local file system is going to be compared against that version.
122
123 Here's an example comparing two specific versions (published to the configured registry) of the current project directory:
124
125 ```bash
126 npm diff --diff=1.0.0 --diff=1.1.0
127 ```
128
129 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.
130
131 #### Filtering files
132
133 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:
134
135 ```bash
136 npm diff --diff=pkg@2 ./lib/ CHANGELOG.md
137 ```
138
139 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.
140
141 ### Configuration
142
143 #### `diff`
144
145 - Default:
146 - Type: String (can be set multiple times)
147
148 Define arguments to compare in `npm diff`.
149
150 #### `diff-name-only`
151
152 - Default: false
153 - Type: Boolean
154
155 Prints only filenames when using `npm diff`.
156
157 #### `diff-unified`
158
159 - Default: 3
160 - Type: Number
161
162 The number of lines of context to print in `npm diff`.
163
164 #### `diff-ignore-all-space`
165
166 - Default: false
167 - Type: Boolean
168
169 Ignore whitespace when comparing lines in `npm diff`.
170
171 #### `diff-no-prefix`
172
173 - Default: false
174 - Type: Boolean
175
176 Do not show any source or destination prefix in `npm diff` output.
177
178 Note: this causes `npm diff` to ignore the `--diff-src-prefix` and `--diff-dst-prefix` configs.
179
180 #### `diff-src-prefix`
181
182 - Default: "a/"
183 - Type: String
184
185 Source prefix to be used in `npm diff` output.
186
187 #### `diff-dst-prefix`
188
189 - Default: "b/"
190 - Type: String
191
192 Destination prefix to be used in `npm diff` output.
193
194 #### `diff-text`
195
196 - Default: false
197 - Type: Boolean
198
199 Treat all files as text in `npm diff`.
200
201 #### `global`
202
203 - Default: false
204 - Type: Boolean
205
206 Operates in "global" mode, so that packages are installed into the `prefix` folder instead of the current working directory. See [folders](/cli/v10/configuring-npm/folders) for more on the differences in behavior.
207
208 - packages are installed into the `{prefix}/lib/node_modules` folder, instead of the current working directory.
209 - bin files are linked to `{prefix}/bin`
210 - man pages are linked to `{prefix}/share/man`
211
212 #### `tag`
213
214 - Default: "latest"
215 - Type: String
216
217 If you ask npm to install a package and don't tell it a specific version, then it will install the specified tag.
218
219 It is the tag added to the package@version specified in the `npm dist-tag add` command, if no explicit tag is given.
220
221 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.
222
223 If used in the `npm publish` command, this is the tag that will be added to the package submitted to the registry.
224
225 #### `workspace`
226
227 - Default:
228 - Type: String (can be set multiple times)
229
230 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.
231
232 Valid values for the `workspace` config are either:
233
234 - Workspace names
235 - Path to a workspace directory
236 - Path to a parent workspace directory (will result in selecting all workspaces within that folder)
237
238 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.
239
240 This value is not exported to the environment for child processes.
241
242 #### `workspaces`
243
244 - Default: null
245 - Type: null or Boolean
246
247 Set to true to run the command in the context of **all** configured workspaces.
248
249 Explicitly setting this to false will cause commands like `install` to ignore workspaces altogether. When not set explicitly:
250
251 - 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.
252
253 This value is not exported to the environment for child processes.
254
255 #### `include-workspace-root`
256
257 - Default: false
258 - Type: Boolean
259
260 Include the workspace root when workspaces are enabled for a command.
261
262 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.
263
264 This value is not exported to the environment for child processes.
265
266 ## See Also
267
268 - [npm outdated](/cli/v10/commands/npm-outdated)
269 - [npm install](/cli/v10/commands/npm-install)
270 - [npm config](/cli/v10/commands/npm-config)
271 - [npm registry](/cli/v10/using-npm/registry)