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