Skip to content

When you have an inferred task (or an explicitly defined task using the nx:run-commands executor) running a command, there'll come a time when you'll need to pass args to the underlying command being run. This recipe explains how you can achieve that.

For this recipe we'll use a project with the following project.json file:

apps/my-app/project.json
{
"sourceRoot": "apps/my-app/src",
"projectType": "application",
"targets": {}
}

And the following final configuration:

Project Details View

my-app

Vite

Root: apps/my-app

Type:application

Targets

  • build

    Vite

    vite build

    Cacheable
  • preview

    Vite

    vite preview

  • serve

    Vite

    vite serve

  • test

    Vite

    vitest run

    Cacheable

We'll focus on the build target of the project. If you expand the build target in the Project Details View above, you'll see that it runs the command vite build. In the next sections, we'll see how to provide --assetsInlineLimit=2048 and --assetsDir=static/assets args to that command.

Pass args in the project.json task configuration

Section titled “Pass args in the project.json task configuration”

To statically pass some extra args to a specific project, you can update its project.json file. You can do it by either providing the args as individual options or by providing the args option:

apps/my-app/project.json
{
"sourceRoot": "apps/my-app/src",
"projectType": "application",
"targets": {
"build": {
"options": {
"assetsInlineLimit": 2048,
"assetsDir": "static/assets"
}
}
}
}

Pass args in the targetDefaults for the task

Section titled “Pass args in the targetDefaults for the task”

To provide the same args for all projects in the workspace, you need to update the task configuration in the nx.json file. Similar to the previous section, you can do it by either providing the args as individual options or by providing the args option:

nx.json
{
"targetDefaults": {
"build": {
"options": {
"assetsInlineLimit": 2048,
"assetsDir": "static/assets"
}
}
}
}

Pass args when running the command in the terminal

Section titled “Pass args when running the command in the terminal”

To pass args in a one-off manner when running a task, you can also provide them as individual options or by providing the --args option when running the task:

nx build my-app --assetsInlineLimit=2048 --assetsDir=static/assets