node.jsmonoreponx-monorepo

What is the different between `nx target` and native command?


I am using nx to manage a monorepo and found there are many targets for nx projects e.g. @nx/eslint:lint.

This target can be defined in project.json managed by nx:

"targets": {
    "lint": {
      "executor": "@nx/eslint:lint",
      "inputs": [
        "{projectRoot}/**/*.ts"
      ],
    ...

I can also add eslint dependency on the package.json and add a script like:

scripts: {
   "lint": "eslint src"
   ...

I wonder what the fundamental different between using @nx/eslint:lint and the native eslint command.


Solution

  • First, by adding to a script in package.json you can run eslint on all code in the monorepo, but you can't run it on each package of the monorepo at a time.

    One reason this is good is that when you run it on each package at a time, you can take advantage of NX caching, which will re-use a successful result if the code inside a package hasn't changed. But, there are other reasons, too.

    So using targets lets you hook into all the tooling that NX offers to be able to consistently and intelligently run lint on all the packages in your monorepo.


    Second, you can use the JSON file to configure how that target is run. The @nx/eslint:lint target supports quite a few options. See docs: https://nx.dev/nx-api/eslint/executors/lint#options

    And while you could encode all those as command line arguments in your script invocation, it gets hard to maintain when there gets to be a lot of options.