简体   繁体   English

Yarn 本地包依赖项

[英]Yarn local packages dependencies

I have the following folder structure:我有以下文件夹结构:

~ (user home folder)
 - api
    ...
    - package.json
 - lib
    - libA
      ...
      package.json
    - libB
      ...
      package.json

In libA/package.json I have the following local dependencylibA/package.json我有以下本地依赖

"dependencies": {
    "libB": "../libB",
  },

So libA depends on libB.所以 libA 依赖于 libB。

Now I want inside api project to add as local package libA .现在我想在api项目中添加为本地包libA I execute cd api && yarn add ../lib/libA and I get the following error /Users/a_user/libB doesn't exist .我执行cd api && yarn add ../lib/libA并收到以下错误/Users/a_user/libB doesn't exist I understand that yarn sees as current director ~/api so when is reading the dependency of libA it sees ../libB and translate it as ~/libB and not as ~/lib/libB我知道 yarn 被视为当前主管~/api所以当读取libA的依赖项时,它会看到../libB并将其翻译为~/libB而不是~/lib/libB

Is there anyway to achieve it without absolute paths ?无论如何,没有绝对路径可以实现它吗?

Yes, there is, using yarn link .是的,有,使用yarn link Yarn link allows you to create symlinks to local projects. Yarn 链接允许您创建到本地项目的符号链接。

Go to the folder libB and run:转到文件夹libB并运行:

yarn link

Then go to the folder libA and run:然后转到文件夹libA并运行:

yarn link libB

NOTE : that libB must be the name on the package.json inside the libB folder注意libB必须是libB文件夹中package.json上的名称

Then you can require your libB code as usual:然后你可以像往常一样要求你的libB代码:

const libB = require('libB')

The package where I created the link from used npm, where-as the second package that was consuming the first one used yarn.我从使用的 npm 创建链接的包,其中第二个使用第一个包的包使用了纱线。

In my case, I had to create as well as consume the link using npm link rather than yarn link as that is what the first package that I wanted to consume used.就我而言,我必须使用 npm link 而不是 yarn link 创建和使用链接,因为这是我想要使用的第一个包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM