Vite Plugin Node

8.0.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Sets up a basic Express.js server with Vite-powered Hot Module Replacement (HMR) using `vite-plugin-node`, demonstrating `vite.config.ts` and the server entry point.

// 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`

view raw JSON →