Episode 3: Setting Up for Monorepo Development

Prerequisites

To follow along with this course, you’ll need to install two essential tools on your computer:

  1. Node.js

    • Node.js is required to run JavaScript on your system and manage packages using npm.

    • Download Node.js from Node.js Official Website.

    • Choose either:

      • LTS (Long-Term Support): Stable and reliable.

      • Current Version: Includes the latest features.

Both versions will work perfectly for this course.

  1. Yarn Package Manager

    • Yarn has a unique feature called workspaces, which simplifies monorepo development.

    • To install Yarn, run the following command in your terminal after installing Node.js:

        npm install -g yarn
      

Why Yarn?

Yarn’s workspace feature is particularly useful for monorepos. It allows you to:

  • Import and use your custom libraries just like any other npm package.

  • Automatically make local changes available to all projects using your library.

For example:

  1. You create a utility library in your monorepo.

  2. Another project within the monorepo imports and uses this library.

  3. Any local updates you make to the library will instantly reflect in the consuming project.


How Does Node.js Resolve Modules?

Understanding how Node.js locates and resolves modules is critical for working with monorepos effectively.

When you import a module in Node.js, you use either:

  1. A file path

  2. A package name


1. Resolving Modules by File Path

File paths can be:

  • Relative Paths

    • Start with a . or .. to indicate location relative to the current file.

    • Example:

        const helper = require('./utils/helper');
        const config = require('../config/settings');
      
  • Absolute Paths

    • Start with / to specify the exact location from the root of your file system.

    • Example:

        const helper = require('/Users/username/project/utils/helper');
      

2. Resolving Modules by Package Name

If the module is an npm package:

  • Simply use the package name.

  • Example:

      const express = require('express');
      const lodash = require('lodash');
    

When Node.js encounters a package name (without . or /):

  1. It searches for a node_modules folder in the current directory.

  2. If not found, it moves up the parent directories to locate a node_modules folder.

  3. This continues until:

    • The module is found.

    • It reaches the root of the file system.


What Happens if the Module Isn’t Found?

If Node.js:

  • Finds the node_modules folder but doesn’t locate the package:

    • It continues searching up the directory tree.
  • Doesn’t find any node_modules folder:

    • It throws an error:

        Error: Cannot find module 'module-name'
      

Why This Behavior Matters for Monorepos

This module resolution mechanism is the key to how monorepos work seamlessly:

  • Custom Libraries in monorepos behave like npm packages.

  • Node.js resolves them using its module resolution logic, allowing local updates to be instantly available in all dependent projects.


Next Steps

In the next lesson, we’ll:

  1. Explore how Yarn Workspaces take advantage of Node.js module resolution.

  2. Learn to set up your first monorepo using Yarn Workspaces.

By understanding the fundamentals of module resolution, you’ll be ready to unlock the full potential of monorepos!

Stay tuned! 🚀