Developers August 26, 2020

Working with monorepos can be hard

Zlatko Fedor @zlatkofedor

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
  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)
Recommended Posts