Allow local project to depend on local lerna packages

Jack Guy picture Jack Guy · Feb 28, 2018 · Viewed 9.7k times · Source

I have a lerna repo for a project under development. It has several packages that depend on each other. To make development easier, none of the packages are published and they depend on the latest version of each other.

Directory tree

foo/
  packages/
    core/
      package.json
    errors/
      package.json

foo/packages/core/package.json

{
  ...
  dependencies: {
    "@foo/errors": "*"
  }
}

I have another project, bar, that I'm using to test the lerna project. Currently I'm linking to its dependencies using a local file: dependency:

bar/package.json

{
  ...
  dependencies: {
    "@foo/core": "../foo/packages/core"
  }
}

This approach has given me a world of trouble.

  • Using npm, I'm constantly hit with ENOENT .DELETE errors. Removing my package-lock.json and reinstalling has taken years off my life.
  • Using yarn, I've been unable to yarn install in bar. Yarn follows the file: dependency to @foo/core, sees that it depends on @foo/errors and doesn't know about lerna's symlink. This causes it to fail, telling me it can't find @foo/errors.

This has made writing actual code for this project secondary to this mess of dependency management.

How can I make this (I feel fairly simple?) project structure work? Open to lerna/yarn/npm/pnpm/shell scripts/MS DOS at this point.

Answer

jjbskir picture jjbskir · Feb 27, 2019

You should be able to accomplish this with npm link. Although I have not tried this using a local dependency that is not published on npm.

Directory tree

foo/
  packages/
    core/
      package.json
    errors/
      package.json
bar/
    package.json

foo/packages/core/package.json

{
  ...
  dependencies: {
    "@foo/errors": "*"
  }
}

bar/package.json

{
  ...
  dependencies: {
    "@foo/core": "../foo/packages/core"
  }
}

Run the following commands

cd foo
npx lerna clean
npx lerna bootstrap --hoist
npm run build # command to build your projects
cd packages/core
npm link
cd ../../../bar
npm i
npm link @foo/core

Removing package-lock.json files usually does more harm then good! And about not being able to find @foo/errors, if you ran npm bootstrap, @foo/core should be symlinked to @foo/errors. One possibility could be that your lerna scripts are using npm while you where running install/link with yarn.