What is vite-node?
The vite-node package is a tool that allows you to run Node.js scripts with Vite's native ES modules support, providing features such as hot module replacement (HMR) and TypeScript support out of the box. It is designed to work with Vite's ecosystem and can be used for tasks like server-side rendering (SSR) and testing.
What are vite-node's main functionalities?
Running Node.js scripts with ES modules
This feature allows you to run Node.js scripts that use ES module syntax, leveraging Vite's module resolution and transformation capabilities.
import { createServer } from 'vite-node/server';
const server = createServer({
// Vite's options here
});
server.moduleGraph.ensureEntryFromUrl('/path/to/your/module.js');
Hot Module Replacement (HMR)
vite-node supports HMR, enabling developers to have a more interactive development experience by automatically reloading modules when changes are detected.
import { createServer } from 'vite-node/server';
const server = createServer({
// Vite's options here
});
server.watcher.on('change', (file) => {
server.moduleGraph.invalidateModule(file);
});
TypeScript support
With vite-node, you can run TypeScript files directly without pre-compilation, as it integrates with Vite's built-in TypeScript support.
import { createServer } from 'vite-node/server';
const server = createServer({
// Vite's options here
});
server.moduleGraph.ensureEntryFromUrl('/path/to/your/typescript-module.ts');
Other packages similar to vite-node
ts-node
ts-node is a TypeScript execution engine and REPL for Node.js. It allows you to run TypeScript files directly in Node.js without pre-compiling them. Unlike vite-node, ts-node does not provide HMR and is not integrated with Vite's ecosystem.
esbuild-runner
esbuild-runner enables you to run scripts using ESBuild, which is a fast JavaScript bundler and minifier. It provides fast compilation but does not offer HMR or the same level of integration with Vite's development server and features.
babel-node
babel-node is part of the Babel toolchain and allows you to run Node.js scripts with Babel's support for the latest JavaScript syntax. It is similar to vite-node in that it supports modern JavaScript features, but it does not have built-in HMR or the optimizations provided by Vite.
vite-node

Vite as Node runtime.
EXPERIMENTAL
Usage
npx vite-node index.ts
Options:
npx vite-node -h
Features
- Out-of-box ESM & TypeScript support (possible for more with plugins)
- Top-level await
- Vite plugins, resolve, aliasing
- Respect
vite.config.ts
- Shims for
__dirname
and __filename
in ESM - Access to native node modules like
fs
, path
, etc. - Watch mode (like
nodemon
)
When NOT to Use
- Production, yet - in very early stage, check it later
- Most of the time, when other tools can do that job
- We need to start a Vite server upon each execution, which inevitably introduces some overhead. Only use it when you want the same behavior as Vite or the powerful plugins system (for example, testing components with a Vite-specific setup).
Why?
It runs Vite's id resolving, module transforming, and most importantly, the powerful plugins system!
How?
It fires up a Vite dev server, transforms the requests, and runs them in Node.
Credits
Based on @pi0's brilliant idea of having a Vite server as the on-demand transforming service for Nuxt's Vite SSR.
Thanks @brillout for kindly sharing this package name.
License
MIT License © 2021 Anthony Fu