Skip to content

The @nx/vite plugin provides various executors to help you create and configure vite projects within your Nx workspace. Below is a complete reference for all available executors and their options.

build

Builds a Vite.js application for production.

project.json:

//...
"my-app": {
"targets": {
//...
"build": {
"executor": "@nx/vite:build",
//...
//...
"options": {
"outputPath": "dist/apps/my-app"
},
//...
}
},
}
}
Terminal window
nx serve my-app

Examples

{% tabs %} {% tab label=“Set a custom path for vite.config.ts” %}

Nx will automatically look in the root of your application for a vite.config.ts (or a vite.config.js) file. If you want to use a different path, you can set it in your project.json file, in the build target options:

//...
"my-app": {
"targets": {
//...
"build": {
"executor": "@nx/vite:build",
//...
"options": {
"outputPath": "dist/apps/my-app",
"configFile": "apps/my-app/vite.config.other-path.ts"
},
"configurations": {
...
}
},
}
}

or even

//...
"my-app": {
"targets": {
//...
"build": {
"executor": "@nx/vite:build",
//...
"options": {
"outputPath": "dist/apps/my-app",
"configFile": "vite.config.base.ts"
},
"configurations": {
...
}
},
}
}

{% /tab %}

{% /tabs %}

Options

OptionTypeDescriptionDefault
buildLibsFromSourcebooleanRead buildable libraries from source instead of building them separately.true
configFilestringThe name of the Vite.js configuration file.
generatePackageJsonbooleanGenerate a package.json for the build output.
includeDevDependenciesInPackageJsonbooleanInclude devDependencies in the generated package.json.
outputPathstringThe output path of the generated files.
skipOverridesbooleanDo not add a overrides and resolutions entries to the generated package.json file. Only works in conjunction with generatePackageJson option.
skipPackageManagerbooleanDo not add a packageManager entry to the generated package.json file. Only works in conjunction with generatePackageJson option.
skipTypeCheckbooleanSkip type-checking via TypeScript. Skipping type-checking speeds up the build but type errors are not caught.false
tsConfigstringThe path to custom tsconfig file for type-checking when skipTypeCheck is false. Required when tsconfig file is not at the projectRoot level.
useEnvironmentsApibooleanUse the new Environments API for building multiple environments at once. Only works with Vite 6.0.0 or higher.false
watchstringEnable re-building when files change.false

dev-server

Starts a dev server using Vite.

project.json:

//...
"my-app": {
"targets": {
//...
"serve": {
"executor": "@nx/vite:dev-server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "my-app:build",
},
"configurations": {
...
}
},
}
}
Terminal window
nx serve my-app

Examples

{% tabs %} {% tab label=“Set up a custom port” %}

You can always set the port in your vite.config.ts file. However, you can also set it directly in your project.json file, in the serve target options:

//...
"my-app": {
"targets": {
//...
"serve": {
"executor": "@nx/vite:dev-server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "my-app:build",
"port": 4200,
},
"configurations": {
...
}
},
}
}

{% /tab %} {% tab label=“Specify a proxyConfig” %}

You can specify a proxy config by pointing to the path of your proxy configuration file:

//...
"my-app": {
"targets": {
//...
"serve": {
"executor": "@nx/vite:dev-server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "my-app:build",
"proxyConfig": "apps/my-app/proxy.conf.json"
},
"configurations": {
...
}
},
}
}

{% /tab %}

{% /tabs %}

Options

OptionTypeDescriptionDefault
buildTargetstring [required]Target which builds the application. Only used to retrieve the configuration as the dev-server does not build the code.
buildLibsFromSourcebooleanRead buildable libraries from source instead of building them separately.true
proxyConfigstringPath to the proxy configuration file.

preview-server

Preview Server for Vite.

project.json:

//...
"my-app": {
"targets": {
//...
"preview": {
"executor": "@nx/vite:preview-server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "my-app:build",
},
"configurations": {
...
}
},
}
}
Terminal window
nx preview my-app

Examples

{% tabs %} {% tab label=“Set up a custom port” %}

You can always set the port in your vite.config.ts file. However, you can also set it directly in your project.json file, in the preview target options:

//...
"my-app": {
"targets": {
//...
"preview": {
"executor": "@nx/vite:preview-server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "my-app:build",
"port": 4200,
},
"configurations": {
...
}
},
}
}

{% /tab %} {% tab label=“Specify a proxyConfig” %}

You can specify a proxy config by pointing to the path of your proxy configuration file:

//...
"my-app": {
"targets": {
//...
"preview": {
"executor": "@nx/vite:preview-server",
"defaultConfiguration": "development",
"options": {
"buildTarget": "my-app:build",
"proxyConfig": "apps/my-app/proxy.conf.json"
},
"configurations": {
...
}
},
}
}

{% /tab %}

{% /tabs %}

Options

OptionTypeDescriptionDefault
buildTargetstring [required]Target which builds the application.
proxyConfigstringPath to the proxy configuration file.
staticFilePathstringPath where the build artifacts are located. If not provided then it will be infered from the buildTarget executor options as outputPath

test

Test using Vitest.

project.json:

//...
"my-app": {
"targets": {
//...
"test": {
"executor": "@nx/vite:test",
//...
//...
"options": {
"config": "apps/my-app/vite.config.ts"
}
//...
}
}
}
Terminal window
nx test my-app

Examples

{% tabs %}

{% tab label=“Running in watch mode” %} To run testing in watch mode, you can create a new configuration within your test target, and have watch set to true. For example:

"my-app": {
"targets": {
//...
"test": {
"executor": "@nx/vite:test",
//...
//...
"options": {
"config": "apps/my-app/vite.config.ts"
},
"configurations": {
"watch": {
"watch": true
}
}
}
}
}

And then run nx run my-app:test:watch.

Alternatively, you can just run the default test target with the --watch flag preset, like so:

Terminal window
nx run my-app:test --watch

{% /tab %} {% tab label=“Updating snapshots” %} Whenever a test fails because of an outdated snapshot, you can tell vitest to update them with the following:

Terminal window
nx run my-app:test -u

{% /tab %}

{% /tabs %}

Options

OptionTypeDescriptionDefault
configFilestringThe path to the local vitest config, relative to the workspace root.
modestringMode for Vite.
reportsDirectorystringDirectory to write coverage report to.
testFilesarray
watchbooleanWatch files for changes and rerun tests related to changed files.