Vite.js is a build tool that aims to provide a faster and leaner development experience for modern web projects.
Why should you use this plugin?
- Instant dev server start
- Lightning fast Hot-Module Reloading
- Fast builds using Vite.
- Vite-powered tests with smart and instant watch mode
Read more about Vite and Vitest in the Vite documentation.
Setting up a new Nx workspace with Vite
You can create a new workspace that uses Vite with one of the following commands:
- Generate a new monorepo with a Web Components app set up with Vite
npx create-nx-workspace@latest --preset=web-components
- Generate a new standalone React app set up with Vite
npx create-nx-workspace@latest --preset=react-standalone --bundler=vite
Add Vite to an existing workspace
There is a number of ways to use Vite in your existing workspace.
Generate a new project using Vite
You can generate a React application or library or a Web application that uses Vite.js. The
@nx/web:app generators accept the
bundler option, where you can pass
vite. This will generate a new application configured to use Vite.js, and it will also install all the necessary dependencies, including the
To generate a React application using Vite.js, run the following:
To generate a React library using Vite.js, run the following:
To generate a Web application using Vite.js, run the following:
Modify an existing React or Web project to use Vite.js
You can use the
@nx/vite:configuration generator to change your React or Web project to use Vite.js. This generator will modify your project's configuration to use Vite.js, and it will also install all the necessary dependencies, including the
You can read more about this generator on the
@nx/vite:configuration generator page.
If you do not want to create any new projects or convert any existing projects yet, you can still use Nx to install all the necessary dependencies for Vite.js. This, for example, could be useful if you want to set up Vite.js manually for a project.
Install the Keep Nx Package Versions In Sync
Make sure to install the
@nx/vite version that matches the version of
nx in your repository. If the version numbers get out of sync, you can encounter some difficult to debug errors. You can fix Nx version mismatches with this recipe.
Ask Nx to install the necessary dependencies
After you install the plugin, you can initialize Vite.js. You can do this by running the
init executor. This executor will make sure to install all the necessary dependencies.
You will notice that the generator will ask you of the framework you are planning to use. This is just to make sure that the right dependencies are installed. You can always install manually any other dependencies you need.