Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow local project to depend on local lerna packages

Tags:

npm

yarnpkg

lerna

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.

like image 319
Jack Guy Avatar asked Feb 28 '18 20:02

Jack Guy


People also ask

What does lerna build do?

Lerna is a fast modern build system for managing and publishing multiple JavaScript/TypeScript packages from the same repository.

How do I start the lerna project?

To start to use lerna you need install the package in mode global or use npx, when you decide what you need to do. Before to continue I suggest you create a branch and call it lerna-init, it will be used in the future. After that we need to init our lerna project, lerna has two modes: fixed/locked and independent.


1 Answers

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.

like image 117
jjbskir Avatar answered Sep 23 '22 07:09

jjbskir