选择命令行版本:
🌐 Synopsis
npm link [<package-spec>]alias: ln
🌐 Description
这对于安装你自己的东西很方便,这样你就可以在它上面工作和迭代测试,而不必不断地重建。
🌐 This is handy for installing your own stuff, so that you can work on it and test iteratively without having to continually rebuild.
包链接是一个两步过程。
🌐 Package linking is a two-step process.
首先,在没有参数的情况下,位于包文件夹中的 npm link 会在全局文件夹 {prefix}/lib/node_modules/<package> 中创建一个符号链接,链接到执行 npm link 命令的包。它还会将包中的任何可执行文件链接到 {prefix}/bin/{name}。请注意,npm link 使用全局前缀(请参阅 npm prefix -g 了解其值)。
🌐 First, npm link in a package folder with no arguments will create a symlink in the global folder {prefix}/lib/node_modules/<package> that links to the package where the npm link command was executed. It will also link any bins in the package to {prefix}/bin/{name}. Note that npm link uses the global prefix (see npm prefix -g for its value).
接下来,在其他某个位置,npm link package-name 将从全局安装的 package-name 创建一个符号链接到当前文件夹的 node_modules/。
🌐 Next, in some other location, npm link package-name will create a symbolic link from globally-installed package-name to node_modules/ of the current folder.
请注意,package-name 是取自 package.json 的,而不是取自目录名。
🌐 Note that package-name is taken from package.json, not from the directory name.
包名称可以选择性地加上作用域前缀。详见 scope。作用域必须以 @ 符号开头,并且后跟斜杠。
🌐 The package name can be optionally prefixed with a scope. See scope. The scope must be preceded by an @-symbol and followed by a slash.
在为 npm publish 创建 tar 包时,如果它们被包含在 bundleDependencies 中,链接的包会通过解析符号链接来“快照”到其当前状态。
🌐 When creating tarballs for npm publish, the linked packages are "snapshotted" to their current state by resolving the symbolic links, if they are included in bundleDependencies.
例如:
🌐 For example:
cd ~/projects/node-redis # go into the package directorynpm link # creates global linkcd ~/projects/node-bloggy # go into some other package directory.npm link redis # link-install the package
现在,对 ~/projects/node-redis 的任何更改都会反映在 ~/projects/node-bloggy/node_modules/node-redis/ 中。请注意,链接应指向包名称,而不是该包的目录名称。
🌐 Now, any changes to ~/projects/node-redis will be reflected in ~/projects/node-bloggy/node_modules/node-redis/. Note that the link should be to the package name, not the directory name for that package.
你也可以将这两个步骤合并为一步。例如,要以更简便的方式完成上述用例:
🌐 You may also shortcut the two steps in one. For example, to do the above use-case in a shorter way:
cd ~/projects/node-bloggy # go into the dir of your main projectnpm link ../node-redis # link the dir of your dependency
第二行相当于做:
🌐 The second line is the equivalent of doing:
(cd ../node-redis; npm link)npm link redis
也就是说,它首先创建一个全局链接,然后将全局安装目标链接到你项目的 node_modules 文件夹中。
🌐 That is, it first creates a global link, and then links the global installation target into your project's node_modules folder.
请注意,在这种情况下,你指的是目录名称 node-redis,而不是包名称 redis。
🌐 Note that in this case, you are referring to the directory name, node-redis, rather than the package name redis.
如果你的关联包是有作用域的(见 scope),你的链接命令必须包含该作用域,例如。
🌐 If your linked package is scoped (see scope) your link command must include that scope, e.g.
npm link @myorg/privatepackage
🌐 Caveat
请注意,以这种方式链接的包依赖默认不会保存到 package.json 中,假设这种做法的目的是让链接代替普通的非链接依赖。否则,例如,如果你依赖 redis@^3.0.1 并运行了 npm link redis,它会用 file:../path/to/node-redis 替换 ^3.0.1 依赖,这可能不是你想要的!此外,如果项目中的其他用户或开发者的文件夹设置与你不完全相同,他们也可能会遇到问题。
🌐 Note that package dependencies linked in this way are not saved to package.json by default, on the assumption that the intention is to have a link stand in for a regular non-link dependency. Otherwise, for example, if you depend on redis@^3.0.1, and ran npm link redis, it would replace the ^3.0.1 dependency with file:../path/to/node-redis, which you probably don't want! Additionally, other users or developers on your project would run into issues if they do not have their folders set up exactly the same as yours.
如果你要以链接的形式添加一个_新的_依赖,你应该通过运行 npm install <dep> --package-lock-only 将其添加到相关的元数据中。
如果你想在你的 package.json 和 package-lock.json 文件中保存 file: 引用,你可以使用 npm link <dep> --save 来实现。
🌐 Workspace Usage
npm link <pkg> --workspace <name> 将会把相关包作为指定工作区的依赖进行链接。请注意,如果没有冲突的依赖,它实际上可能会被链接到父项目的 node_modules 文件夹中。
npm link --workspace <name> 将创建到指定工作区的全局链接。
🌐 Configuration
savetrue,除非使用 npm update 时,默认值为 false将已安装的软件包保存到 package.json 文件中作为依赖。
🌐 Save installed packages to a package.json file as dependencies.
与 npm rm 命令一起使用时,会移除对 package.json 的依赖。
🌐 When used with the npm rm command, removes the dependency from package.json.
如果设置为 false,也将阻止向 package-lock.json 写入。
🌐 Will also prevent writing to package-lock.json if set to false.
save-exact保存到 package.json 的依赖将使用精确的版本进行配置,而不是使用 npm 的默认 semver 范围运算符。
🌐 Dependencies saved to package.json will be configured with an exact version rather than using npm's default semver range operator.
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/maninstall-strategy设置在 node_modules 中安装包的策略。hoisted(默认):在顶层安装非重复包,必要时在目录结构中安装重复包。nested(原先为 --legacy-bundling):就地安装,不采用提升。shallow(原先为 --global-style):仅在顶层安装直接依赖。linked(实验性):安装在 node_modules/.store 中,就地链接,不提升。
🌐 Sets the strategy for installing packages in node_modules. hoisted (default): Install non-duplicated in top-level, and duplicated as necessary within directory structure. nested: (formerly --legacy-bundling) install in place, no hoisting. shallow (formerly --global-style) only install direct deps at top-level. linked: (experimental) install in node_modules/.store, link in place, unhoisted.
legacy-bundling--install-strategy=nested不要在 node_modules 中提升包安装,而是按照依赖的方式安装包。这可能会导致非常深的目录结构和重复的包安装,因为没有去重功能。设置 --install-strategy=nested。
🌐 Instead of hoisting package installs in node_modules, install packages in the same manner that they are depended on. This may cause very deep directory structures and duplicate package installs as there is no de-duplicating. Sets --install-strategy=nested.
global-style--install-strategy=shallow 取代只在顶层 node_modules 安装直接依赖,但在更深层的依赖上使用提升(hoist)。设置 --install-strategy=shallow。
🌐 Only install direct dependencies in the top level node_modules, but hoist on deeper dependencies. Sets --install-strategy=shallow.
strict-peer-deps如果设置为 true,且未设置 --legacy-peer-deps,那么任何冲突的 peerDependencies 都将被视为安装失败,即使 npm 能够基于非 peer 依赖合理地猜测适当的解决方案。
🌐 If set to true, and --legacy-peer-deps is not set, then any conflicting peerDependencies will be treated as an install failure, even if npm could reasonably guess the appropriate resolution based on non-peer dependency relationships.
默认情况下,依赖图中深层的冲突 peerDependencies 将使用最近的非对等依赖规范来解决,即使这样做会导致某些包获得的对等依赖超出其包的 peerDependencies 对象中设置的范围。
🌐 By default, conflicting peerDependencies deep in the dependency graph will be resolved using the nearest non-peer dependency specification, even if doing so will result in some packages receiving a peer dependency outside the range set in their package's peerDependencies object.
当执行此类覆盖时,会打印警告,解释冲突及涉及的包。如果设置了 --strict-peer-deps,则此警告将被视为失败。
🌐 When such an override is performed, a warning is printed, explaining the conflict and the packages involved. If --strict-peer-deps is set, then this warning is treated as a failure.
package-lock如果设置为 false,则在安装时忽略 package-lock.json 文件。如果 save 为 true,这也将防止写入 package-lock.json。
🌐 If set to false, then ignore package-lock.json files when installing. This will also prevent writing package-lock.json if save is true.
omitNODE_ENV 环境变量设置为 'production',则为 'dev',否则为空。要从磁盘上的安装树中省略的依赖类型。
🌐 Dependency types to omit from the installation tree on disk.
请注意,这些依赖仍然会被解析并添加到 package-lock.json 或 npm-shrinkwrap.json 文件中。它们只是没有实际安装到磁盘上。
🌐 Note that these dependencies are still resolved and added to the package-lock.json or npm-shrinkwrap.json file. They are just not physically installed on disk.
如果某种封装类型同时出现在 --include 和 --omit 列表中,那么它将被包括在内。
🌐 If a package type appears in both the --include and --omit lists, then it will be included.
如果生成的省略列表包含 'dev',那么所有生命周期脚本的 NODE_ENV 环境变量将被设置为 'production'。
🌐 If the resulting omit list includes 'dev', then the NODE_ENV environment variable will be set to 'production' for all lifecycle scripts.
ignore-scripts如果为 true,npm 不会运行 package.json 文件中指定的脚本。
🌐 If true, npm does not run scripts specified in package.json files.
请注意,明确用于运行特定脚本的命令,例如 npm start、npm stop、npm restart、npm test 和 npm run-script,如果设置了 ignore-scripts,仍会运行其指定的脚本,但它们不会运行任何前置或后置脚本。
🌐 Note that commands explicitly intended to run a particular script, such as npm start, npm stop, npm restart, npm test, and npm run-script will still run their intended script if ignore-scripts is set, but they will not run any pre- or post-scripts.
audit当设置为“true”时,会将审核报告与当前的 npm 命令一起提交到默认注册表以及为作用域配置的所有注册表。有关提交内容的详细信息,请参阅 npm audit 文档。
🌐 When "true" submit audit reports alongside the current npm command to the default registry and all registries configured for scopes. See the documentation for npm audit for details on what is submitted.
bin-links告诉 npm 为包的可执行文件创建符号链接(在 Windows 上则创建 .cmd 垫片)。
🌐 Tells npm to create symlinks (or .cmd shims on Windows) for package executables.
设置为 false 可使其不执行此操作。这可以用来应对某些文件系统不支持符号链接的情况,即使在表面上的 Unix 系统上也是如此。
🌐 Set to false to have it not do this. This can be used to work around the fact that some file systems don't support symlinks, even on ostensibly Unix systems.
fund当“true”显示消息时,会在每个 npm install 的末尾确认正在寻找资金的依赖数量。详情请参见 npm fund。
🌐 When "true" displays the message at the end of each npm install acknowledging the number of dependencies looking for funding. See npm fund for details.
dry-run表示你不希望 npm 做出任何更改,并且它只应该报告本来会做的事情。这个选项可以传递给任何修改本地安装的命令,例如 install、update、dedupe、uninstall,以及 pack 和 publish。
🌐 Indicates that you don't want npm to make any changes and that it should only report what it would have done. This can be passed into any of the commands that modify your local installation, eg, install, update, dedupe, uninstall, as well as pack and publish.
注意:其他与网络相关的命令(例如 dist-tags、owner 等)不支持此项。
🌐 Note: This is NOT honored by other network related commands, eg dist-tags, owner, etc.
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.
install-links当设置文件时:协议依赖将作为常规依赖被打包和安装,而不会创建符号链接。此选项对工作区没有影响。
🌐 When set file: protocol dependencies will be packed and installed as regular dependencies instead of creating a symlink. This option has no effect on workspaces.
🌐 See Also