190

In my package.json I'm pointing local package my-custom-i18n by its relative path:

package.json

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

npm install installs packages correctly, but yarn has problem with it and simply cannot find this package:

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 npm registry, where this package doesn't live.

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.

Community
  • 1
  • 1
michalczukm
  • 9,963
  • 6
  • 40
  • 47

4 Answers4

327

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.

michalczukm
  • 9,963
  • 6
  • 40
  • 47
Piotr Lewandowski
  • 6,540
  • 2
  • 27
  • 32
  • 1
    thanks, works like a charm. But if it's not compatible with npm client for now - I'll just leave `npm` as it is :) PS my idea was to use `yarn` only locally without converting whole team and project. So .. I'll have to wait till support for this :) – michalczukm Oct 18 '16 at 22:16
  • 3
    make sure you clear your cache. as yarn always try cached version first – Bo Chen Nov 03 '16 at 14:28
  • 8
    +1 for the update! Removing `file:` prefix solved a problem we had where `yarn install` on windows would add `./` prefix to relative file paths, but `yarn install` on macOS would remove it. – Magne Mar 14 '18 at 13:38
  • The absolute path didn't work for me. It would work for the install, but then the transpiling would fail because somehow it was looking for a relative path but the yarn.lock had the absolute path. – pixelearth Mar 01 '20 at 08:46
  • 2
    It seems with yarn `1.17.3` the `file:` prefix is still necessary in the following situation: Let's say you have package `bar` which has a local dependency `./dependencies/xyz`. If another package `foo` is using package `bar`, it will try resolve `./dependencies/xyz` relative to `foo` directory instead of relative to `bar` directory. After changing the dependency to `file:./dependencies/xyz` the problem was resolved. – David Callanan Apr 12 '20 at 08:56
  • 2
    In case anyone else stubles into this: I was having issues with getting a local dependency in a `yarn workspace` project to install correctly on Github Actions CI. Adding an explicit `file:` to the path in the individual workspace `package.json` allowed CI to resolve the dependency correctly. – cayleyh Feb 11 '21 at 22:24
  • Works like a charm `yarn add file:abspath` – itsmnthn Apr 08 '21 at 08:37
  • It's worth noting that the package to be added necessarily needs to have a `version` field defined. Otherwise it fails with `Can't add "my-package": invalid package version ""` – jlh Apr 15 '21 at 09:24
  • How can you add a specific branch? – Shane Sepac Aug 06 '21 at 19:21
  • How to do this with yarn berry? (v2 and v3) – Paul Razvan Berg Sep 10 '21 at 10:04
  • for me, it is always absolute path from the project root, not your current path when you run yarn. – Pencilcheck Aug 08 '22 at 00:53
  • For the `NPM` case, I use `npm add file:./../your-project`. It seems to be working – JotaPardo Nov 04 '22 at 03:31
  • https://stackoverflow.com/a/72520455/3874231 – wojtow Aug 12 '23 at 02:33
31

For yarn version >= 2.x (Yarn Berry)

To reference local packages, Yarn supports different protocols:

  • file: creates a copy of the specified directory. When specifying relative paths (starting with ./ or ../) or absolute paths (starting with /), the file: prefix can also be omitted.
  • portal: creates a symbolic link to the specified directory and resolves its transitive dependencies.
  • link: creates a symbolic link to the specified directory but does not resolve its transitive dependencies.

To add a dependency, run yarn add your-project@file:../your-project.

Attempting to run yarn add file:../your-project (without the your-project@ prefix) will result in the following error: Usage Error: The file:../your-project string didn't match the required format (package-name@range). Did you perhaps forget to explicitly reference the package name?.

cdauth
  • 6,171
  • 3
  • 41
  • 49
27

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

mr.bjerre
  • 2,384
  • 2
  • 24
  • 37
2

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.

  • which version of yarn you're running? This solution is for v.1 (I'll update answer with this info) – michalczukm Feb 23 '21 at 15:08
  • My yarn version is 1.22.10. The code is https://github.com/lonekorean/highlight-within-textarea . Currently my workaround is succeeding, but isn't very snappy in reaction time, but I'm too busy fighting CSS and DOM to try changing to a **proper** solution. – Mark Eklund Feb 26 '21 at 00:36