npmyarnpkgyarn-workspacespnpmyarn-v2

How to develop with an external npm package into a Yarn PnP monorepo?


I do have to work with a large Yarn PnP mono repo (~30 packages and 3 micro apps).

One of the packages (package-b) is using an external npm package package-a (which is using npm as a package manager) which is not part of the monorepo that I need to work with.

What is the proper way to develop npm package-a (external to the mono repo) and test it in Yarn PnP package-b (part of mono repo)?

Versions I'm using:

$ yarn --version
4.1.1
$ node --version # (same throughout all projects"
v18.15.0
$ npm --version
9.5.0

Here is what I tried:

Yarn link

$ cd /Users/user/gitlab.com/org/package-a
$ npm install
$ npm run build
$ yarn link
$ cd /Users/user/gitlab.com/org/monorepo/package-b
$ yarn link package-a
Usage Error: Invalid destination 'package-a'; Can't link the project to itself

$ yarn link [-A,--all] [-p,--private] [-r,--relative] ...

This error is absolutely odd because I am not linking to the same package (100% confirmed).

resolutions (portal)

In project-b/package.json:

  ...
  "resolutions": {
    "package-a": "portal:/Users/user/gitlab.com/org/package-a"
  },
  ...

I re-ran yarn (mono repo level). Rebuilt all packages from scratch. Changes are not showing.

dependencies (portal)

In project-b/package.json:

  "dependencies": {
    "package-a": "portal:/Users/user/gitlab.com/org/package-a",
  }

When I build my mono repo I get:

Module not found: Error: Can't resolve 'package-a' in '/Users/myuser/gitlab.com/org/monorepo/.yarn/__virtual__/package-b-virtual-126d6aafbe/1/packages/package-b/dist/components/SomeComponent/util'
 @ ../..

I confirmed that /Users/user/gitlab.com/org/package-a/dist exists and is fully built. I am able to consume this package in other npm-based projects without any issues.


Solution

  • I was able to get linking to work by completely uninstalling the package and reinstalling it using portal

    $ yarn remove package-a
    $ yarn add package-a@portal:/Users/user/gitlab.com/org/package-a
    

    After that, I rebuilt the mono repo. Since I'm using portal in combination with live build/refresh (vite) in both package-a and package-b, changes in package-a will be immediately available.