Run Tasks
Monorepos can have hundreds or even thousands of projects, so being able to run npm scripts against all (or some) ofthem is a key feature of a tool like Lerna.
Definitions
- Command - anything the developer types into the terminal (e.g.,
lerna run build --scope=header --concurrency=5). - Target - the name of an npm script (e.g.,
build). - Task - an invocation of an npm script (e.g.,
header:build).
Example Repository
Examples are based onthis repository, so feel free to clone itand follow along.
Run Everything
Each project has thetest andbuild scripts defined.
Run:
npx lerna run build
This will build the projects in the right order:footer andheader and thenremixapp.
✔ header:build (501ms)
✔ footer:build (503ms)
✔ remixapp:build (670ms)
—————————————————————————————————————————————————————————————————————————————
Lerna (powered by Nx) Successfully ran target build for 3 projects (1s)
Note that Lerna doesn't care what each of the build scripts does. The namebuild is alsonot special: it's simplythe name of the npm script.
Run a Multiple Tasks concurrently
You can pass a comma-delimited list of targets you wish to trigger to run concurrently.
npx lerna run test,build,lint
If, for example, there are dependencies between your tasks, such asbuild needing to run beforetest for particular packages, the task-runner will coordinate that for you as long as you have configured an appropriateTask Pipeline Configuration.
Run a Task for a single Package
While developing you rarely run all the builds or all the tests. Instead, you often run things only against the projectsyou are changing. For instance, you can run theheader tests like this:
npx lerna run test --scope=header
Run Tasks Affected by a PR
You can also run a command for all the projects affected in your PR like this:
npx lerna run test --since=origin/main
Learn morehere.
Control How Tasks Run
For more control over the order tasks are executed, edit theTask Pipeline Configuration.
To speed up your task execution, learn how toCache Task Results andDistribute Task Execution.
Automatic loading of.env files
By default the modern task runner powered by Nx will automatically load.env files for you. You can set--load-env-files to false if you want to disable this behavior for any reason.
For more details about what.env files will be loaded by default please see:https://nx.dev/recipes/environment-variables/define-environment-variables