How to publish yarn workspace with root package.json dependencies?

Viewed 765

I'm trying to publish one (yarn) workspace from a monorepo to npm repository, and include root project's dependencies in the published package. The setup is as follows:

  • package.json (contains shared dependencies, eg. single version of React to be used by all workspaces)
  • library/ (this is the one I want to publish, for consumption outside of this monorepo, the published package.json should contain the root dependencies as well as library local)
  • library/package.json (contains library specific dependencies)
  • app1/ (application that uses library)
  • app1/package.json (contains app local dependencies, and depends on library)
  • app2/ (application that uses library)
  • app2/package.json (contains app local dependencies, and depends on library)

So what I want to do, is to cd library and yarn publish. What I expect to happen is that the published package will work exactly the same way it works for my monorepo local apps. The problem is that yarn doesn't merge-in the dependencies from the monorepo's root package.json, and the published package's package.json only contains library local dependencies, from library/package.json. So when anyone installs this published library package, it will be broken because it imports modules from packages (dependencies) not listed in library/package.json.

Do I really need to write a custom publishing shell script that merges the root package.json's dependencies into library/package.json before running yarn publish?

1 Answers

The only type of dependencies that you can reliably put in a workspace root are dev dependencies.

Each package should declare it's dependencies in it's package.json file respectively.

What yarn does during installation is that it now looks at all the dependencies in all your workspace packages to create a single lock file and it won't put redundant dependencies in the package node_modules folder.

How you publish your workspace is up to you. I typically write a small Node.js script to run the publish command line for each package. Depending on what you put in your workspace you might want to version and/or publish your packages independently, this will give you that flexibility.

Related