npm workspaces: npm run and exec

ruyadorno

Ruy Adorno

Posted on March 24, 2021

npm workspaces: npm run and exec

I bring good news!

Workspaces support for both npm run and npm exec landed today in npm@7.7.0, it adds the new -w and -ws config options that allows for running scripts in workspaces from the top-level folder, e.g:

assuming a file structure:

.
├── package.json ->  { "workspaces": ["packages/*"] }
└── packages
    ├── a
    │   ├── index.js
    │   └── package.json
    ├── b
    │   ├── index.js
    │   └── package.json
    └── c
        ├── index.js
        └── package.json
Enter fullscreen mode Exit fullscreen mode

It's now possible to run a script in a given workspace. In order to run one of the scripts available in the workspace named a, at location: ./packages/a/ you may run from the root of your project, either of the following:

  • npm run <script-name> -w a
  • npm run <script-name> -w ./packages/a

It also supports the test|start|stop|restart top level comands, so if you're using any of these you can just add -w <workspaces-name> and it should work as expected, e.g:

  • npm test -w a (will run the tests of workspace a)

In case you want to run a script against all the configured workspaces, there's a workspaces configuration option that enables just that:

  • npm test -ws

Lastly it's also possible to run scripts in a group of workspaces by using the path to their parent folder as the workspace config value. That means running npm test -w ./packages will run tests in all workspaces nested at the ./packages location.

Want to learn more about it?

We updated the docs, see:

You may also want to check out the changelog:

💖 💪 🙅 🚩
ruyadorno
Ruy Adorno

Posted on March 24, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related