Simplify your monorepo with npm 7 workspaces

Simplify your monorepo with npm 7 workspaces:

 

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 the main advantage of npm over other package managers like yarn or pnpm is that it comes bundled with NodeJS.

Read on and you’ll find out how to use npm 7 workspaces in a real-world scenario and learn that using workspaces the npm’s way is very different to yarn’s.

Monorepo use cases

A monorepo is a term describing a single git repository that contains many projects.

The most common reason to set up a monorepo is to streamline work within a dev team that maintains multiple apps that are using a shared piece of code, for example a common User Interface library.

Imagine a team that develops two React apps that shares some common UI elements like inputs, selectors, accordions, etc. It would be nice to extract that UI in form of React components and prepare building blocks that are ready to use for all members of the team.

Apart from that it’s just more convenient to have all your source files opened in a single IDE instance. You can jump from project to project without switching windows on your desktop.

Common library

Let’s say I want to build two independent React apps called app1 and app2 that will use a common component from a common UI library called ui. And I want both apps to hot reload whenever I edit a file inside the UI library.

By independent I mean that app1 doesn’t know anything about app2 and vice-versa.

Below is a setup that is compatible with npm 7 workspaces.

Defining workspaces in npm 7

I am prefixing all packages’ names with @xyz so they will be distinctive from the official npm repo. Just change it to yours.

This is the most crucial part of the whole setup. Insert below inside your root folder’s package.json to set up a monorepo.

{
"name": "@xyz/monorepo",
"private": true,
"version": "1.0.0",
"workspaces": [
"./common/*"
]
}
Enter fullscreen mode Exit fullscreen mode

The new "workspaces" property lets npm know that I want to track any packages inside ./common folder and automatically symlink them in the root’s node_moduleswhen I run npm install.

From now on whenever our React apps will use import Foo from "@xyz/ui" the NodeJS will find it in ./node_modules/common/@xyz/ui that points to ./common/uifolder that contains our library. Perfect! No need for npm link anymore with the workspaces.

Without workspaces the React app would complain that it cannot find a module named @xyz/ui and would start looking for it in the npm official repo.

Start with pure JavaScript

To test our setup let’s export a vanilla JavaScript string from the ui library and import them into our React app.

Create the common UI library’s package.json:

{
"name": "@xyz/ui",
"version": "1.0.0",
"private": true,
"main": "index.js"
}
Enter fullscreen mode Exit fullscreen mode

and index.js file that will export a string:

const version = "This comes from UI! 1.0.0"

export default version;
Enter fullscreen mode Exit fullscreen mode

Time to import that string into our apps.

To quickly create React apps inside our monorepo. Just use the newly released Create React App 4:

mkdir apps
cd apps
npx create-react-app app1
npx create-react-app app2
Enter fullscreen mode Exit fullscreen mode

Now you’d think that we need to add our ui library to the app. In yarn it would look like below:

yarn workspace app1 add @xyz/ui
Enter fullscreen mode Exit fullscreen mode

But with npm we don’t need to add any dependency at all.

Just go to your App.js file in both app1 and app2 apps and add the below code to display a string from our UI library:

...
import testString from "@xyz/ui"; 
...
    {testString}
...
Enter fullscreen mode Exit fullscreen mode

To test it use the below commands:

# create a symlink to the @xyz/ui in the root folder
npm install
# go to the app's folder
cd apps/app1
# For CRA 4 you may need to add SKIP_PREFLIGHT_CHECK=true to .env file
# And use the --legacy-peer-deps flag as many packages hasn't been updated yet to officially support React 17
npm install --legacy-peer-deps
npm run start
Enter fullscreen mode Exit fullscreen mode

and from another terminal window:

cd apps/app2
npm install
npm run start
Enter fullscreen mode Exit fullscreen mode

You’ll see the This comes from UI! 1.0.0 text rendered inside both of your React apps!

Exporting JSX

Now if you’d try to export a JSX component the React apps will complain that they cannot parse the JSX. You need to transpile JSX code from the common UI package first.

You can use a basic Webpack 5 setup:

common/ui/package.json

{
"name": "@xyz/ui",
"version": "0.2.0",
"private": true,
"module": "build/ui.bundle.min.js", # Changed main to module
"scripts": {
"build": "webpack --config webpack.prod.js",
"build-watch": "webpack --config webpack.prod.js --watch",
},
... # webpack 5 dependencies
}
Enter fullscreen mode Exit fullscreen mode

common/ui/babel.config.js

module.exports = {
  presets: [
    [
      "@babel/preset-react",
      {
        targets: {
          node: "current",
        },
      },
    ],
  ],
};
Enter fullscreen mode Exit fullscreen mode

common/ui/webpack.prod.js

const path = require("path");

module.exports = {
  entry: {
    index: { import: "./src/index.js" }
  },
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: "babel-loader",
      },
    ],
  },
  output: {
    filename: "ui.bundle.min.js",
    path: path.resolve(__dirname, "build"),
    // Below two important lines!
    library: 'xyzUI',
    libraryTarget: 'umd'
  },
};
Enter fullscreen mode Exit fullscreen mode

Our simple component:

common/ui/src/index.js

import React from "react";

const UIExample = ({ text = "" }) => {
  return (
    

Shared UI library {text}

); }; export default UIExample;
Enter fullscreen mode Exit fullscreen mode

Import the UIExample component inside your React app using below:

apps/app1/src/App.js

...
import UIExample from "@xyz/ui";
...
    
...
Enter fullscreen mode Exit fullscreen mode

Make sure the UI library is transpiled on every code change:

cd common/ui
npm run build-watch
Enter fullscreen mode Exit fullscreen mode

Run the app1 in a separate terminal window and take notice that whenever you edit the UI component the webpack dev server will automatically reload it with the newest version thanks to the webpack watch running in the background.

cd apps/app1
npm run start
Enter fullscreen mode Exit fullscreen mode

Demo

Below I’m editing the common UI component UIElement and upon saving both React apps are automatically refreshed with the updated component:

Summary

With the newest npm 7 and its support of workspaces it is now possible to have a monorepo without a need of any external tools like @react-workspaces or nx.

Just remember that npm has a different philosophy than yarn. For example you cannot run a script inside a workspace from the monorepo’s root folder.

Recognize also that @xyz/app1 and @xyz/app2 weren’t defined in the monorepo’s package.json workspaces property. Only the modules that will be exported need to be there (@xyz/ui).

The npm 7 workspaces are mainly providing discovery for the modules. I wish it had been emphasised in release notes and that the npm help’s examples were a bit more complex. I hope this article fills this gap for the time being.

from Tumblr https://generouspiratequeen.tumblr.com/post/633569510587252736

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s