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.
Example project and tasks
Section titled “Example project and tasks”For this recipe we'll use a project with the following project.json file:
{ "sourceRoot": "apps/my-app/src", "projectType": "application", "targets": {}}And the following final configuration:
my-app
Root: apps/my-app
Type:application
Targets
build
vite build
Cacheablepreview
vite preview
serve
vite serve
test
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:
{ "sourceRoot": "apps/my-app/src", "projectType": "application", "targets": { "build": { "options": { "assetsInlineLimit": 2048, "assetsDir": "static/assets" } } }}{ "sourceRoot": "apps/my-app/src", "projectType": "application", "targets": { "build": { "options": { "args": ["--assetsInlineLimit=2048", "--assetsDir=static/assets"] // it also accepts a single string: // "args": "--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:
{ "targetDefaults": { "build": { "options": { "assetsInlineLimit": 2048, "assetsDir": "static/assets" } } }}{ "targetDefaults": { "build": { "options": { "args": ["--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/assetsnx build my-app --args="--assetsInlineLimit=2048 --assetsDir=static/assets"