DEV Community

Cover image for Simplify your monorepo with npm 7 workspaces

Simplify your monorepo with npm 7 workspaces

Łukasz Wolnik on October 31, 2020

This month npm has released a major version of their package manager npm 7. It shipped with support for workspaces. Why is it big news? Because ...
Collapse
 
raimeyuu profile image
Damian Płaza

Webpack 5 module federation might match npm 7 workspaces nicely. One could evolve apps within monorepo independently, sharing code seamlessly and gain some modularity points when running apps thanks for module federation 😃

Collapse
 
blowsie profile image
Sam Blowes

Doesnt module federation seem like re-inventing the wheel when we already have ESM?

Vite and Snowpack are already using the ESM approach, I wonder how long WebPack will hold the throne.

Collapse
 
michalzalecki profile image
Michal Zalecki

They allow you to manage and share code on different levels. Workspaces are for monorepo, you create and import packages as ESM modules. Integration happens in build time with the ability to import modules dynamically, but you need to build the entire app with dynamic imports.

The benefits of module federation are in the runtime e.g. you can update and deploy only a part of your application (e.g. one monorepo package) owned by a separate team. Workspaces and federated modules can work together.

Collapse
 
limal profile image
Łukasz Wolnik

Oh, yes, they should match nicely. Although I have never dared to explore microfrontends to such extent. I guess I would like to see how micro services are working for the back-end systems in the long run first.

In the article I made a Create React App 4's app (based on webpack 4) to import a custom built module (using webpack 5) without issues. It's hardly suprising as in the end it was the webpack 4 that created a single bundle. Nevertheless it feels magical how each piece of the puzzle fit nicely and allowed for hot reloading a separate package. That's what I call sharing code seamlessly.

Collapse
 
raimeyuu profile image
Damian Płaza

Yes, this feels truly seamless. However, I can imagine that monorepo might bring troubles with CI/CD when there are multiple artifacts produced for each app 🤔

Thread Thread
 
limal profile image
Łukasz Wolnik

That;s true. Although with some planning it doesn't have to take long and produce large Docker image diffs.

The biggest offender is node_modules for each app. Not only it takes long to npm install each of the apps but they also contribute hugely into an image size if not dealt carefully.

  1. Install dependencies for each of the apps by copying just the package.json for each of the app and running their npm install.
  2. Then COPY . . the rest of the files (with node_modules in .dockerignore).
  3. Then build each of the app.

Each consecutive build starts from the step 2 which just copies the source files (not node_modules) and runs the npm build for each of the apps. And that's 10 times faster than installing the dependencies.

You can optimise further and instead of copying all apps together you can order them by their frequence of commits. Placing the most active one as the last step to copy source files and then build.

Thread Thread
 
raimeyuu profile image
Damian Płaza

Yes, I suppose you're talking about docker layers and writing docker files in the way to reuse as much as possible to not trigger unnecessary operations while building the images.

I was thinking more about multiple teams (so many people) contributing to two apps and queueing builds/deploys many, many times.

Nevertheless, it might be that I'm creating imaginary issues that will never happen :-D

Thread Thread
 
limal profile image
Łukasz Wolnik

Multiple teams can create as many PRs to the monorepo as they want and each commit within the PRs will trigger a new build in a CI. It's not a problem given that builds are so quick thanks to the optimised Dockerfile.

Most of the time each team will update just their app leaving a shared UI code unchanged.

A deployment would only be a problem if the shared code had been modified. Then every app in the monorepo would have to be tested for regressions by a QA team.

But if a newly merged PR contains only code for a single app then it's perfectly safe to deploy it. For only the modified app will create a new CSS/JavaScript bundle.

I am assuming all apps would sit n a single Docker image running from a single nginx cotainer, i.e. example.com/app1 example/app2. It's be possible though to make a separate build of a Docker image for each of the apps and run them in independent containers.

A risk of regression would still be present in this scenario too as updating the shared code alters all artifacts. But that's the trade off of using a shared code/DRY principle in the first place.

Collapse
 
sashirachakonda profile image
sashiRachakonda

Thanks for this article. I was able to able to follow the article and try out the running example.

trying to apply the same to one of my projects and running into the invalid hook call error...do you have any suggestions ?

Here is the monorepo setup

  • root/
    • /lib1
    • /lib2
    • /create-react-app1

package.json at the root folder designates lib1 and lib2 as workspaces

Thanks,
-Sashi.

Collapse
 
limal profile image
Łukasz Wolnik

Thanks, Sashi.

No idea what's wrong with it, sorry. I have never encountered the issue you're having.

Collapse
 
sashirachakonda profile image
sashiRachakonda

I believe its caused due to multiple copies (not versions) of React..
I do see multiple copies of react

  • one at the /create-react-app1 level
  • other at the root level. lib1 and/or lib2 require React. That get installed at the root node_modules level.

So, when rendering a view in the CRA, the app thinks there are multiple copies or React and hence the invalid hook call..

Not sure how to resolve this..

Thread Thread
 
limal profile image
Łukasz Wolnik

You're absolutely correct! In fact I had this problem myself but couldn't recognise it at first from invalid hook call.

Add below externals Webpack settings for lib1 and lib2 so that they don't bundle its own React but use whatever create-react-app1 is using.

webpack.common.js

module.exports = {
  ...
  externals: {
    react: {
      root: "React",
      commonjs2: "react",
      commonjs: "react",
      amd: "react",
    },
  },
};
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
sashirachakonda profile image
sashiRachakonda

i did add react and react-dom to the externals in lib1 and lib2...some reason, when i do the npm install at the root, i do see react and react-dom in the node_modules folder at the root..

i'll give it another shot...appreciate your comments..

Thread Thread
 
limal profile image
Łukasz Wolnik

No worries. I know it can be tricky.

Maybe try fixing for a single external lib1 and /create-react-app1 firstly. You might be very close to solving this but there's too many moving parts.

Good luck!

Thread Thread
 
sashirachakonda profile image
sashiRachakonda

If its not much trouble would it be possible to post your example on github ?

Thread Thread
 
limal profile image
Łukasz Wolnik

Good idea. I'll do it this weekend.

Collapse
 
ruanmartinelli profile image
Ruan Martinelli • Edited

Hey @limal , I wrote a piece about about npm Workspaces too, here's the link in case it might be useful ✌️ dev.to/ruanmartinelli/what-you-nee...

Very cool gifs by the way!

Collapse
 
limal profile image
Łukasz Wolnik

Hey Ruan. Nice! It's very useful. You gave the topic a lot more background.

Ha! Thanks!

Collapse
 
jaworek profile image
Jan Jaworski

Do you have any tips on how to set it up with React Native? I find it quite challenging to do it as it involves changing a lot of build settings. I never managed to get it right.

Collapse
 
limal profile image
Łukasz Wolnik

No, not at the moment. I am starting a big React Native project in the coming weeks so I will share my experiences once I figure it out. I don't have high expectations though as I'd rather keep the React Native setup intact so it will be easy to upgrade to the anticipated version 0.64.

Collapse
 
jaworek profile image
Jan Jaworski

Thanks. I am currently working on two apps that share a lot of components and logic. I have to copy stuff between projects which is time consuming and prone to error. I've been trying to setup monorepo for them but always had some issues with finding native modules or crashes when building so I gave up for now/

Thread Thread
 
limal profile image
Łukasz Wolnik

Totally agree. I'll give it a shot.

Thanks for the heads-up!

Collapse
 
tinynow_31 profile image
Matt Kreiling

Thanks for this post - there is indeed a need for examples better than npm's docs.
FYI, this post got plagiarized at these two urls:
angrychopshopmaker.tumblr.com/post...
digitalnow878391108.wordpress.com/...

Collapse
 
limal profile image
Łukasz Wolnik

Thanks, Matt! I hope it helps.

Regarding duplicated content. I feel flattered that someone took an effort to copy it. Although those articles look like they were copied by a web scrapper.

Collapse
 
amos80m profile image
Amos Megidish

Hi, Thank you for this article.
I have a question about the "Apps" section.
once created my "apps" folder and created inside multiple react apps (and everything is working 100%).
how do i "git push" it all(including the new react apps), the git won't upload the apps from the apps folder.
what is the best practice for this, please?

Collapse
 
limal profile image
Łukasz Wolnik • Edited

Hi Amos,

I think the reason why you can't push all of them is because Create React App is initialising a Git repository inside each of your apps.

Simply remove a .git folder inside the apps and you should be able to have a single repo for all apps.

cd apps/app1
rm -rf .git
Enter fullscreen mode Exit fullscreen mode
Collapse
 
amos80m profile image
Amos Megidish

Working!, I will need to see how can I manage this with react native apps. you helped me a lot with the start - Thx

Collapse
 
kapaski profile image
Kapaski

Great read - I wonder when it comes to dockerising just app1 or app2 to their own images, how we handle the common part? will it work if I just copy app1 and common with respect of the folder structure, install dependencies respectively, and create a shorter version of the workspace package.json that just mention app1 and common, then use that package.json as docker's command entry point, so that @xyz /ui still works for app1 even in a contianer, meanwhile i don't have to copy and build the entire monorepo in an image where I just need app1?

Collapse
 
mkman profile image
Mustafa Mansour

Awesome article, thank you!
I was going through the npm documentation and they seems to indicate that it's possible to run scripts in the workspaces from the monorepo root directory.

Collapse
 
manojkarmocha profile image
mkarmocha

can this tutorial have another version with typescript ?

Collapse
 
limal profile image
Łukasz Wolnik

Good idea! I'll write a TypeScript version once I find some free time and this time I'll provide also a public repo to easily follow the setup presented here.

Collapse
 
mellis481 profile image
Mike E

Does the npm workspaces setup work properly with Dependabot ie. will a Dependabot PR properly update packages and be able to be merged without manual intervention?