1
3 Comments

Working with monorepos can be hard

I decided to use monorepo for my new project.
After a few months of using I need to tell you that it was a really great choice.

Meanwhile, I had just one problem and it was with duplicates in the final build.
Especially with React package. I saw too many times Invalid Hook Call Warning. When I was digging more I found comment: You might have more than one copy of React in the same app. (https://reactjs.org/warnings/invalid-hook-call-warning.html)

First of all, I tried to fix it with Webpack (aliases, resolve) but it started to be unmaintainable with new packages.

Then I found parameter --hoist. It will put all shared(duplicate) dependencies from all your packages into your root node_modules directory.

Rollup with Babel will start to work out of the box. You just need to set up Webpack resolve.modules. Here is my example of webpack.config.babel.js located in the root directory of each package.

  resolve: {
    modules: [
      path.resolve(__dirname, 'node_modules'),
      path.resolve(__dirname, '../../node_modules'),
      'node_modules',
    ],
    ...

For more information check out Lerna documentation:
https://github.com/lerna/lerna/blob/master/doc/hoist.md

FYI: I wanted to work with pure NPM and not to use YARN workspaces.

Are you using monorepos for your projects?
  1. Yes
  2. No
Vote
posted to Icon for group Developers
Developers
on August 26, 2020
  1. 1

    I still don’t understand with the benefit of monorepo and can’t relate with it. That’s why I don’t use monorepo and prefer to use multi repo. Perhaps you have great references that I can read 😀

  2. 1

    Why do you want to avoid yarn workspaces? I find it much easier to use.

    1. 1

      Hi @dannychickenegg. Actually I like YARN and I am glad that they created it. I think that it was the main reason why they updated NPM faster :)
      I have two answers:

      1. I prefer to work with NPM (it is just my personal decision)
      2. If you want to use workspaces you need to write more configuration fields and I like the current trend (write less and it will work out of the box)
Trending on Indie Hackers
AI runs 70% of my distribution. The exact stack. User Avatar 147 comments I'm a solo founder. It took me 9 months and at least 3 stack rewrites to ship my SaaS. User Avatar 128 comments Show IH: I'm building a lead gen + CRM tool for web designers targeting local businesses without websites — starting with Spain User Avatar 79 comments I built a URL indexing SaaS in 40 days — here's the honest story User Avatar 58 comments We could see our AI bill, but not explain it — so I built AiKey User Avatar 25 comments AI coding should not turn software development into a black box User Avatar 11 comments