Nx How to Pass Arguments to Target Dependency

I have the project config set in the package.json as below:

"nx": {
  "targets": {
    "build": {
      "outputs": [
        "{projectRoot}/dist"
      ]
    },
    "build-site": {
      "dependsOn": [
        "build"
      ],
      "outputs": [
        "{projectRoot}/fractal/build"
      ]
    },
    "bundlesize": {
      "dependsOn": [
        "build"
      ]
    }
  }
}

I want to pass an argument to the build target it depends on.

nx run-many --target=build-site --all -- --foo=bar

This does not seem to get passed down to the build target. Is there any way to do this?

[EDIT]

I use npm scripts (in package.json) as targets and have target dependencies defined with Nx like below.

{
  "name": "project-a",
  "scripts": {
    "build": "gulp build",
    "build-site": "gulp buildFractal"
  },
  "nx": {
    "build-site": {
      "dependsOn": [
        "build"
      ],
    }
  }
}

And then I run nx run-many --target=build-site --all. However, the issue is that I want to pass a flag to the dependent build target which does not appear to work when I do nx run-many --target=build-site --all -- --foo=bar

1 Answer

dependsOn can be configured with a object instead of the shorthand:

"dependsOn": [{"projects": "self", "target": "build", "params": "forward"}],

Then the params will get forwarded- Ref:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest