node.js 如何通过 Yarn 安装带有本地路径的包?找不到包

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40102686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:46:10  来源:igfitidea点击:

How to install package with local path by Yarn? It couldn't find package

node.jsnpmyarnpkg

提问by michalczukm

In my package.jsonI'm pointing local package my-custom-i18nby its relative path:

在我的package.jsonmy-custom-i18n通过其相对路径指向本地包:

package.json

包.json

"dependencies": {
 "core-js": "^2.4.1",
 "my-custom-i18n": "./../MyProject.Shared/myproject-i18n",
 "rxjs": "5.0.0-beta.12",
 ...
}

npm installinstalls packages correctly, but yarnhas problem with it and simply cannot find this package:

npm install正确安装包,但yarn有问题,根本找不到这个包:

yarn output

纱线输出

$ yarn
yarn install v0.15.1
info No lockfile found.
[1/4] Resolving packages...
error Couldn't find package "myproject-i18n" on the "npm" registry.
info Visit http://yarnpkg.com/en/docs/cli/install for documentation about this command.

I see that it looks it on the npmregistry, where this package doesn't live.

我看到它在npm注册表中查找,该软件包不在其中。

Question

Is there any change to use yarn with local packages? By local packages I mean packages pointed by relative path as my-custom-i18n.

在本地包中使用纱线有什么变化吗?本地包是指由相对路径指向的包为my-custom-i18n.

回答by Piotr Lewandowski

Yarn requires prefix file:for local packages.

Yarn 需要file:本地包的前缀。

For relative path:

对于相对路径:

yarn add file:./../your-project

For absolute path

对于绝对路径

yarn add file:/dev/your-project

For your example, dependency in package.jsonwould be declared as follows:

对于您的示例,依赖项package.json将声明如下:

 "my-custom-i18n": "file:./../MyProject.Shared/myproject-i18n",

This works both for Yarn and NPM as well.

这也适用于 Yarn 和 NPM。

It is incompatibility with NPM client, Yarn team is aware and declared to support this behavior - reference on GitHub issue.

它与 NPM 客户端不兼容,Yarn 团队意识到并声明支持此行为 -参考 GitHub 问题

Update:

更新:

Since v0.21.0release, file:prefix is not needed. See pull-request with fixand changelog.

v0.21.0版本开始,file:不需要前缀。请参阅带有修复更改日志的拉取请求