This page looks best with JavaScript enabled

Quick Tips - npm workspaces

 ·  ☕ 1 min read

    Many of you might be in a similar position to where I was, thinking lerna still served some sort of useful purpose in the node ecosystem, but no, it doesnt apparently.

    Since npm v7, there has been a workspaces option supported in package.json, and it allows you to run arbitrary npm commands in these workspaces the same way these external dependencies used to do. Workspaces allow you to auto symlink and set up local dependencies with their own versions for packages. You don’t have to run npm link once, who would have guessed. You can define workspaces like so:

    1
    2
    3
    4
    5
    6
    
    {
      "name": "my-workspaces-powered-project",
      "workspaces": [
        "packages/a"
      ]
    }
    

    This will symlink package a at that directory, and install it in node_modules when you install the package.json itself. If your project already has a package.json you can run npm init -w ./packages/a to create these lines automatically. It can also create a new project with the package workspace automatically set up, if no such package.json exists.

    run unit tests in package a:

    1
    
    npm run test --workspace=a
    

    run unit tests in all workspace packages:

    1
    
    npm run test --workspaces
    

    conditional:

    1
    
    npm run test --workspaces --if-present
    

    The moral of the story is stop using lerna. stop using lerna

    Share on

    abschill
    WRITTEN BY
    abschill
    software engineer