Share Code Between a Library and Applications Inside a Monorepo

Share this video with your friends

Send Tweet

We already have a utils package that outputs JavaScript. If we want to use that package inside any of our applications, in our case, blog or dashboard, we would have to install it first.

Michael
~ 2 years ago

What did you do to stop npm install a local package from reaching out to the npm registry? The command npm install @monorepo/utils --save -w @monorepo/blog -w @monorepo/dashboard gives me an error E404 because obviously the @monorepo/utils is not a published package.

Alejandro Ñáñezinstructor
~ 2 years ago

Hey @Michael! I’d double check a few things:

  • Do you have the “workspaces” property set up correctly in the root package.json
  • Do you have the correct names in each one of your packagess/apps? Check the “name” property in each one of your package.json files.
  • You can inspect the package-lock.json file to confirm that all the names for your packages are OK
Michael
~ 2 years ago

Thanks Alejandro. I double checked and they seem all good to me. I followed your course and created a project to follow along. Here is the commit which I manually added to the package.json to both apps/blog and apps/dashboard instead of run npm install @monorepo/utils --save -w @monorepo/blog -w @monorepo/dashboard. This works as I can now import and use copyright from the shared libs/utils in both projects. Ideally I was hoping to be able to run the npm install like you demonstrated in the course.

Jason
~ 2 years ago

I had the same experience but the work around is fine.

Tanawat Tassana
~ 2 years ago

@Michael @Jason I had the same experience before but after upgrade Node version to v18 I can install the libs using npm install as in the video.

Tanawat Tassana
~ 2 years ago

The "View code on Github" button doesn't bring me to the code in the video.

Lucas Minter
~ 2 years ago

The "View code on Github" button doesn't bring me to the code in the video.

The github code brings you to the finished code of the course. To find the exact code in this lesson, you'll need to go back a few commits. I believe this is the exact commit for this lesson: https://github.com/alejandronanez/turborepo-egghead/commit/c9f4042d3f9ca3bdf0dee8ebe73375fb727554cd

~ a year ago

Wow. Amazing how fast and smooth this works. Even on a desktop where I expect speed, I'm impressed the TS build and hmr update in both running apps happens faster than I can blink.