Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 540
Laxmana Avatar asked Feb 08 '18 12:02

Laxmana


People also ask

Where are yarn local packages installed?

yarn add file:/path/to/local/folder installs a package that is on your local file system.

How do you add dependencies to package json with yarn?

To install dependencies, you have to run yarn install in the root of your directory to install all the dependencies for a project. The dependencies will be retrieved from the package. json file you pushed to version control, and will be stored in the yarn. lock file.

Can yarn be installed locally?

Yarn has a unique way of installing and running itself in your JavaScript projects. First you install the yarn command globally, then you use the global yarn command to install a specific local version of Yarn into your project directory.


1 Answers

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') 
like image 72
Fabio Antunes Avatar answered Oct 03 '22 13:10

Fabio Antunes