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

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


For yarn version < 2.x

Yarn requires prefix file: for local packages.

For relative path:

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

For absolute path

yarn add file:/dev/your-project

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

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

This works both for Yarn and NPM as well.

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

Update:

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


I tried the file: solution with yarn and never got it to work. I chose a different route of including the package directly. I was able to add a watch to automatically rebuild and update the browser on file change.

My Situation

I had a git repository with two directories: example/ and react-highlight-within-testarea/ (I'll simplify to mylib/ below). The example/ was a react application with a dependency in the example/package.json of: "mylib": "file:../mylib/" (a) and an inclusion in example/src/App.js of import { MyLib } from 'mylib'.

My Solution

  1. Remove mylib from the example/package.json.

    yarn remove mylib

  2. Make the distribution part of my project.

    (cd example/src && ln -s ../../mylib/dist mylib)

  3. Change the inclusion in example/src/App.js.

    import { MyLib } from './mylib'

  4. Install watch in mylib.

    (cd mylib ; yarn add watch)

  5. Add a watch command in scripts of mylib/package.json (b).

    "watch": "watch 'yarn build' src",

  6. I now run this in a terminal (c).

    (cd mylib && yarn install && yarn watch &)(cd example && yarn install && yarn start)

Success

Now any time I make a change and save it in mylib/src/ or example/src/, it gets rebuilt and pushed to the React page in my browser.


Footnotes

(a) These before settings are one of many attempts to use package.json to make this work.

(b) There probably are more correct ways of doing this, but it worked for me.

(c) I probably could have made this into a yarn script too.


If you want to simply link to the local package (so changes in the local package are picked up without reinstalling):

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

See more on yarn docs