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