|
| 1 | +--- |
| 2 | +title: npm-diff |
| 3 | +section: 1 |
| 4 | +description: The registry diff command |
| 5 | +--- |
| 6 | + |
| 7 | +### Synopsis |
| 8 | + |
| 9 | +```bash |
| 10 | +npm diff |
| 11 | +npm diff --diff=<pkg-name> |
| 12 | +npm diff --diff=<version-a> [--diff=<version-b>] |
| 13 | +npm diff --diff=<spec-a> [--diff=<spec-b>] |
| 14 | +``` |
| 15 | + |
| 16 | +### Description |
| 17 | + |
| 18 | +Similar to its `git diff` counterpart, this command will print diff patches |
| 19 | +of files for packages published to the npm registry. |
| 20 | + |
| 21 | +* `npm diff --diff=<spec-a> --diff=<spec-b>` |
| 22 | + |
| 23 | + Compares two package versions using their registry specifiers, e.g: |
| 24 | + `npm diff --diff=pkg@1.0.0 --diff=pkg@^2.0.0`. It's also possible to |
| 25 | + compare across forks of any package, |
| 26 | + e.g: `npm diff --diff=pkg@1.0.0 --diff=pkg-fork@1.0.0`. |
| 27 | + |
| 28 | + Any valid spec can be used, so that it's also possible to compare |
| 29 | + directories or git repositories, |
| 30 | + e.g: `npm diff --diff=pkg@latest --diff=./packages/pkg` |
| 31 | + |
| 32 | +* `npm diff` (in a package directory, no arguments): |
| 33 | + |
| 34 | + If the package is published to the registry, `npm diff` will fetch the |
| 35 | + tarball version tagged as `latest` (this value can be configured using the |
| 36 | + `tag` option) and proceed to compare the contents of files present in that |
| 37 | + tarball, with the current files in your local file system. |
| 38 | + |
| 39 | + This workflow provides a handy way for package authors to see what |
| 40 | + package-tracked files have been changed in comparison with the latest |
| 41 | + published version of that package. |
| 42 | + |
| 43 | +* `npm diff --diff=<pkg-name>` (in a package directory): |
| 44 | + |
| 45 | + When using a single package name (with no version or tag specifier) as an |
| 46 | + argument, `npm diff` will work in a similar way to |
| 47 | + [`npm-outdated`](npm-outdated) and reach for the registry to figure out |
| 48 | + what current published version of the package named <pkg-name> will satisfy |
| 49 | + its dependent declared semver-range. Once that specific version is known |
| 50 | + `npm diff` will print diff patches comparing the current version of |
| 51 | + <pkg-name> found in the local file system with that specific version |
| 52 | + returned by the registry. |
| 53 | + |
| 54 | +* `npm diff --diff=<spec-a>` (in a package directory): |
| 55 | + |
| 56 | + Similar to using only a single package name, it's also possible to declare |
| 57 | + a full registry specifier version if you wish to compare the local version |
| 58 | + of a installed package with the specific version/tag/semver-range provided |
| 59 | + in `<spec-a>`. e.g: (assuming pkg@1.0.0 is installed in the current |
| 60 | + `node_modules` folder) running `npm diff --diff=pkg@2.0.0` will effectively |
| 61 | + be an alias to `npm diff --diff=pkg@1.0.0 --diff=pkg@2.0.0`. |
| 62 | + |
| 63 | +* `npm diff --diff=<semver-a> [--diff=<semver-b>]` (in a package directory): |
| 64 | + |
| 65 | + Using `npm diff` along with semver-valid version numbers is a shorthand |
| 66 | + to compare different versions of the current package. It needs to be run |
| 67 | + from a package directory, such that for a package named `pkg` running |
| 68 | + `npm diff --diff=1.0.0 --diff=1.0.1` is the same as running |
| 69 | + `npm diff --diff=pkg@1.0.0 --diff=pkg@1.0.1`. If only a single argument |
| 70 | + `<version-a>` is provided, then the current local file system is going to |
| 71 | + be compared against that version. |
| 72 | + |
| 73 | +Note that tag names are not valid `--diff` argument values, if you wish to |
| 74 | +compare against a published tag, you must use the `pkg@tagname` syntax. |
| 75 | + |
| 76 | +#### Filtering files |
| 77 | + |
| 78 | +It's possible to also specify positional arguments using file names or globs |
| 79 | +pattern matching in order to limit the result of diff patches to only a subset |
| 80 | +of files for a given package, e.g: |
| 81 | + |
| 82 | +`npm diff --diff=pkg@2 ./lib/ CHANGELOG.md` |
| 83 | + |
| 84 | +### Configuration |
| 85 | + |
| 86 | +#### diff |
| 87 | + |
| 88 | +* Type: String, Array, null |
| 89 | +* Default: null |
| 90 | + |
| 91 | +Defines up to two npm valid package specifiers in which to compare. |
| 92 | + |
| 93 | +#### diff-name-only |
| 94 | + |
| 95 | +* Type: Boolean |
| 96 | +* Default: false |
| 97 | + |
| 98 | +When set to `true` running `npm diff` only returns the names of the files that |
| 99 | +have any difference. |
| 100 | + |
| 101 | +#### diff-unified |
| 102 | + |
| 103 | +* Type: number |
| 104 | +* Default: `3` |
| 105 | + |
| 106 | +The number of lines of context to print in the unified diff format output. |
| 107 | + |
| 108 | +#### diff-ignore-all-space |
| 109 | + |
| 110 | +* Type: Boolean |
| 111 | +* Default: false |
| 112 | + |
| 113 | +Ignore whitespace when comparing lines. This ignores differences even if one |
| 114 | +line has whitespace where the other line has none. |
| 115 | + |
| 116 | +#### diff-no-prefix |
| 117 | + |
| 118 | +* Type: Boolean |
| 119 | +* Default: false |
| 120 | + |
| 121 | +Do not show any source or destination prefix. |
| 122 | + |
| 123 | +#### diff-src-prefix |
| 124 | + |
| 125 | +* Type: String |
| 126 | +* Default: `"a/"` |
| 127 | + |
| 128 | +Show the given source prefix in diff patches headers instead of using "a/". |
| 129 | + |
| 130 | +#### diff-dst-prefix |
| 131 | + |
| 132 | +* Type: String |
| 133 | +* Default: `"b/"` |
| 134 | + |
| 135 | +Show the given source prefix in diff patches headers instead of using "b/". |
| 136 | + |
| 137 | +#### diff-text |
| 138 | + |
| 139 | +* Type: Boolean |
| 140 | +* Default: false |
| 141 | + |
| 142 | +Treat all files as text. |
| 143 | + |
| 144 | +#### global |
| 145 | + |
| 146 | +* Default: false |
| 147 | +* Type: Boolean |
| 148 | + |
| 149 | +Uses packages from the global space as source for comparison. |
| 150 | + |
| 151 | +#### tag |
| 152 | + |
| 153 | +* Type: String |
| 154 | +* Default: `"latest"` |
| 155 | + |
| 156 | +The tag used to fetch the tarball that will be compared with local file system |
| 157 | +files when running npm diff with no arguments. |
| 158 | + |
| 159 | + |
| 160 | +## See Also |
| 161 | + |
| 162 | +* [npm outdated](/commands/npm-outdated) |
| 163 | +* [npm install](/commands/npm-install) |
| 164 | +* [npm config](/commands/npm-config) |
| 165 | +* [npm registry](/using-npm/registry) |
0 commit comments