选择命令行版本:
🌐 Synopsis
npm diff [...<paths>]
🌐 Description
与其 git diff 对应命令类似,此命令将显示发布到 npm 注册表的包文件的差异补丁。
🌐 Similar to its git diff counterpart, this command will print diff patches of files for packages published to the npm registry.
npm diff --diff=<spec-a> --diff=<spec-b>
使用它们的注册表指定符比较两个软件包版本,例如:npm diff --diff=pkg@1.0.0 --diff=pkg@^2.0.0。也可以比较任何软件包的不同分支,例如:npm diff --diff=pkg@1.0.0 --diff=pkg-fork@1.0.0。
可以使用任何有效的规范,因此也可以比较目录或 git 仓库,例如:npm diff --diff=pkg@latest --diff=./packages/pkg
下面是一个示例,比较来自注册表的名为 abbrev 的包的两个不同版本:
npm diff --diff=abbrev@1.1.0 --diff=abbrev@1.1.1
成功后,输出如下所示:
diff --git a/package.json b/package.jsonindex v1.1.0..v1.1.1 100644--- a/package.json+++ b/package.json@@ -1,6 +1,6 @@{"name": "abbrev",- "version": "1.1.0",+ "version": "1.1.1","description": "Like ruby's abbrev module, but in js","author": "Isaac Z. Schlueter <i@izs.me>","main": "abbrev.js",
鉴于 npm 规范的灵活性,你也可以像使用 npm install 时一样,直接针对本地目录或 git 仓库:
npm diff --diff=https://github.com/npm/libnpmdiff --diff=./local-path
在上面的例子中,我们可以比较从 git 仓库 github.com/npm/libnpmdiff 安装的包的内容与包含有效包的 ./local-path 的内容,例如修改过的原始副本。
npm diff(在包目录中,无参数):
如果该包已发布到注册表,npm diff 将获取标记为 latest 的 tar 包版本(此值可以通过 tag 选项进行配置),并将该 tar 包中的文件内容与本地文件系统中的当前文件进行比较。
此工作流程为包作者提供了一种方便的方式,可以查看与该包的最新发布版本相比,哪些包跟踪文件已更改。
npm diff --diff=<pkg-name>(在包目录中):
当使用单个包名(不带版本或标签说明符)作为参数时,npm diff 的工作方式将类似于 npm-outdated,并会访问注册表以确定名为 <pkg-name> 的包的当前已发布版本是否符合其依赖声明的语义化版本范围。一旦确定了具体版本,npm diff 将打印比较当前在本地文件系统中找到的 <pkg-name> 版本与注册表返回的该具体版本的差异补丁。
假设当前已安装名为 abbrev 的软件包:
npm diff --diff=abbrev
这将向注册表请求其最新版本,如果版本号不同,将打印一个比较当前安装的版本与新版本的差异输出。
npm diff --diff=<spec-a>(在包目录中):
类似于仅使用单个包名,如果你希望将已安装包的本地版本与 <spec-a> 中提供的特定版本/标签/语义化版本范围进行比较,也可以声明完整的注册表指定版本。
例如:假设 pkg@1.0.0 已安装在当前的 node_modules 文件夹中,运行:
npm diff --diff=pkg@2.0.0
它实际上将是 npm diff --diff=pkg@1.0.0 --diff=pkg@2.0.0 的一个别名。
npm diff --diff=<semver-a> [--diff=<semver-b>](在包目录中):
将 npm diff 与符合 semver 的版本号一起使用,是比较当前包不同版本的一种简写方式。
它需要从包目录运行,这样对于名为 pkg 的包,运行 npm diff --diff=1.0.0 --diff=1.0.1 与运行 npm diff --diff=pkg@1.0.0 --diff=pkg@1.0.1 是一样的。
如果只提供了一个参数 <version-a>,那么当前的本地文件系统将与该版本进行比较。
这是一个比较当前项目目录的两个特定版本(发布到配置的注册表)的示例:
npm diff --diff=1.0.0 --diff=1.1.0
请注意,标签名称不是有效的 --diff 参数值,如果你想与已发布的标签进行比较,必须使用 pkg@tagname 语法。
🌐 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.
🌐 Filtering files
也可以使用文件名或 glob 模式匹配指定位置参数,以便将差异补丁的结果限制为给定包的文件子集,例如:
🌐 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:
npm diff --diff=pkg@2 ./lib/ CHANGELOG.md
在上面的例子中,diff 输出只会打印位于文件夹 ./lib/ 中的文件内容以及 CHANGELOG.md 文件中更改的代码行。
🌐 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.
🌐 Configuration
diff在 npm diff 中定义要比较的参数。
🌐 Define arguments to compare in npm diff.
diff-name-only使用 npm diff 时仅打印文件名。
🌐 Prints only filenames when using npm diff.
diff-unified在 npm diff 中要打印的上下文行数。
🌐 The number of lines of context to print in npm diff.
diff-ignore-all-space在比较 npm diff 中的行时忽略空白字符。
🌐 Ignore whitespace when comparing lines in npm diff.
diff-no-prefix在 npm diff 输出中不要显示任何源或目标前缀。
🌐 Do not show any source or destination prefix in npm diff output.
注意:这会导致 npm diff 忽略 --diff-src-prefix 和 --diff-dst-prefix 配置。
🌐 Note: this causes npm diff to ignore the --diff-src-prefix and --diff-dst-prefix configs.
diff-src-prefixnpm diff 输出中使用的源前缀。
🌐 Source prefix to be used in npm diff output.
diff-dst-prefixnpm diff 输出中使用的目标前缀。
🌐 Destination prefix to be used in npm diff output.
diff-text将所有文件视为 npm diff 中的文本。
🌐 Treat all files as text in npm diff.
global以“全局”模式运行,因此软件包会安装到 prefix 文件夹中,而不是当前工作目录。有关行为差异的更多信息,请参见 folders。
🌐 Operates in "global" mode, so that packages are installed into the prefix folder instead of the current working directory. See folders for more on the differences in behavior.
{prefix}/lib/node_modules 文件夹中,而不是当前工作目录。{prefix}/bin 关联{prefix}/share/mantag如果你让 npm 安装一个包并且没有告诉它一个特定的版本,那么它会安装指定的标签。
🌐 If you ask npm to install a package and don't tell it a specific version, then it will install the specified tag.
同样,如果没有给出明确的标签,npm tag 命令指定的 package@version 也会添加该标签。
🌐 Also the tag that is added to the package@version specified by the npm tag command, if no explicit tag is given.
当被 npm diff 命令使用时,这是用于获取默认将与本地文件进行比较的 tarball 的标签。
🌐 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.
workspace启用在当前项目的已配置工作区的上下文中运行命令,同时通过仅运行此配置选项定义的工作区进行过滤。
🌐 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.
workspace 配置的有效值为以下之一:
🌐 Valid values for the workspace config are either:
对于 npm init 命令设置时,可以将其设置为一个尚不存在的工作区文件夹,以创建该文件夹并将其作为项目内全新的工作区进行设置。
🌐 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.
此值不会导出到子进程的环境中。
🌐 This value is not exported to the environment for child processes.
workspaces设置为 true 以在 所有 配置的工作区上下文中运行命令。
🌐 Set to true to run the command in the context of all configured workspaces.
将此显式设置为 false 会导致像 install 这样的命令完全忽略工作区。如果不显式设置:
🌐 Explicitly setting this to false will cause commands like install to ignore workspaces altogether. When not set explicitly:
node_modules 树操作的命令(安装、更新等)会将工作区链接到 node_modules 文件夹。- 执行其他操作的命令(测试、执行、发布等)会在根项目上运行,除非 在 workspace 配置中指定了一个或多个工作区。此值不会导出到子进程的环境中。
🌐 This value is not exported to the environment for child processes.
include-workspace-root为命令启用工作区时包括工作区根。
🌐 Include the workspace root when workspaces are enabled for a command.
当为 false 时,通过 workspace 配置指定单个工作区,或通过 workspaces 标志指定所有工作区,将导致 npm 仅在指定的工作区上操作,而不会在根项目上操作。
🌐 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.
此值不会导出到子进程的环境中。
🌐 This value is not exported to the environment for child processes.
🌐 See Also