Laxmana
Laxmana

Reputation: 1706

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 dependency

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

So libA depends on libB.

Now I want inside api project to add as local package libA. I execute cd api && yarn add ../lib/libA and I get the following error/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

Is there anyway to achieve it without absolute paths ?

Upvotes: 62

Views: 101297

Answers (2)

Kaiz Merchant
Kaiz Merchant

Reputation: 11

The package where I created the link from used npm, where-as the second package that was consuming the first one used yarn.

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.

Upvotes: 0

Fabio Antunes
Fabio Antunes

Reputation: 22862

Yes, there is, using yarn link. Yarn link allows you to create symlinks to local projects.

Go to the folder libB and run:

yarn link

Then go to the folder libA and run:

yarn link libB

NOTE: that libB must be the name on the package.json inside the libB folder

Then you can require your libB code as usual:

const libB = require('libB')

Upvotes: 101

Related Questions