Vite Plugin Node
Vite Plugin Node is a crucial Vite plugin specifically crafted to elevate the development experience for Node.js server applications by seamlessly integrating Hot Module Replacement (HMR). This integration allows developers to harness Vite's renowned speed for compilation and its powerful HMR features directly within their backend services, drastically accelerating iteration cycles during development. The current stable version, 8.0.0, is explicitly designed to be compatible with Vite 8.x.x, reflecting a release cadence that closely mirrors Vite's major version updates. A primary differentiator of this plugin is its extensive, out-of-the-box support for a wide array of popular Node.js frameworks, including Express, Fastify, Koa, and Nest. Beyond these, it also provides the flexibility to define custom request adapters, accommodating virtually any other Node.js framework or server setup. Furthermore, it offers developers a choice in TypeScript compilation, allowing them to utilize either Vite's default Oxc-based transformer or opt for SWC. The SWC option is particularly beneficial for projects that require specific TypeScript features such as decorator metadata. This comprehensive functionality streamlines Node.js backend development, embedding it deeply and efficiently within the modern Vite ecosystem, reducing overhead and maximizing developer productivity.
Common errors
-
Cannot find module '@swc/core' or similar SWC-related compilation error.
cause The 'tsCompiler: 'swc'' option is enabled in vite.config.ts, but the '@swc/core' package is not installed in the project's dev dependencies.fixInstall the required SWC compiler: `npm install -D @swc/core`. -
ReferenceError: viteNodeApp is not defined (or similar error indicating the app could not be loaded).
cause The 'exportName' option in vite.config.ts does not match the actual named export of your application instance from the specified 'appPath'.fixVerify that the 'exportName' (default 'viteNodeApp') in your plugin configuration precisely matches the named export in your server entry file, e.g., `export const viteNodeApp = app;`. -
ERR_REQUIRE_ESM or TypeError: require is not a function when importing vite-plugin-node.
cause Attempting to use CommonJS `require()` syntax for vite-plugin-node, which is primarily distributed as an ESM package.fixEnsure your project is configured for ESM (e.g., add `"type": "module"` to your `package.json`) and use the `import { VitePluginNode } from 'vite-plugin-node';` syntax in your `vite.config.ts`.
Warnings
- breaking Vite and vite-plugin-node versions are tightly coupled. Upgrading Vite to a new major version (e.g., from 7.x to 8.x) requires a corresponding upgrade of vite-plugin-node to its matching major version.
- gotcha Using 'tsCompiler: 'swc'' requires manually installing the '@swc/core' peer dependency as a dev dependency. Failing to do so will result in compilation errors.
- gotcha The 'exportName' option in the plugin configuration must precisely match the named export of your application instance from the 'appPath' file. A mismatch will prevent the plugin from correctly identifying and loading your server.
Install
-
npm install vite-plugin-node -
yarn add vite-plugin-node -
pnpm add vite-plugin-node
Imports
- VitePluginNode
const { VitePluginNode } = require('vite-plugin-node');import { VitePluginNode } from 'vite-plugin-node';
Quickstart
// app.ts (your Node.js server entry file)
import express from 'express';
const app = express();
// Basic route for demonstration
app.get('/', (req, res) => {
res.send('Hello from vite-plugin-node!');
});
// Your beautiful server logic goes here...
// This named export is crucial for vite-plugin-node to pick up your application.
// 'viteNodeApp' is the default exportName, but can be configured.
export const viteNodeApp = app;
// vite.config.ts
import { defineConfig } from 'vite';
import { VitePluginNode } from 'vite-plugin-node';
export default defineConfig({
// Vite server configuration, e.g., port for your dev server
server: {
port: 3000
},
plugins: [
// Apply the vite-plugin-node plugin
...VitePluginNode({
// Specify the adapter for your Node.js framework (e.g., 'express', 'nest', 'koa', 'fastify')
adapter: 'express',
// Path to your main Node.js server entry file
appPath: './app.ts',
// Optional: The name of the named export for your application instance from appPath.
// Defaults to 'viteNodeApp' if not specified.
exportName: 'viteNodeApp',
// Optional: Set to true to initialize your app when the Vite dev server starts.
initAppOnBoot: true,
// Optional: Set to true to automatically reload the app on file changes.
reloadAppOnFileChange: true,
// Optional: Choose TypeScript compiler ('vite' for Oxc or 'swc').
// If 'swc' is chosen, you must install '@swc/core' as a dev dependency.
tsCompiler: 'vite'
})
]
});
// To run this example:
// 1. Install dependencies: `npm install express vite vite-plugin-node -D`
// 2. Add a script to your package.json: `"dev": "vite"`
// 3. Create 'app.ts' and 'vite.config.ts' in your project root with the above content.
// 4. Start the dev server: `npm run dev`