Vite as Node.js runtime that enables on-demand evaluation with full ESM and TypeScript support
—
Command-line interface for executing JavaScript and TypeScript files directly with Vite's transformation pipeline. Provides a ts-node alternative with Vite's fast transformation and plugin ecosystem.
# Execute a TypeScript file
npx vite-node index.ts
# Execute a JavaScript file
npx vite-node app.js
# Execute with command line arguments
npx vite-node script.ts arg1 arg2 --flag
# Execute multiple files
npx vite-node file1.ts file2.ts-r, --root <path> # Use specified root directory
-c, --config <path> # Use specified config file
-m, --mode <mode> # Set env mode (development, production, etc.)
-w, --watch # Restart on file changes, similar to "nodemon"
--script # Use vite-node as a script runner
--options <options> # Use specified Vite server options
-v, --version # Output the version number
-h, --help # Display help for commandComplex server options can be passed via the --options flag using dot notation:
# Configure dependency handling
npx vite-node --options.deps.inline="module-name" index.ts
# Configure external dependencies with regex
npx vite-node --options.deps.external="/module-regexp/" index.ts
# Multiple options
npx vite-node \
--options.deps.inline="esm-module" \
--options.deps.external="/^lodash/" \
--options.sourcemap=true \
index.tsAutomatically restart when files change, similar to nodemon:
# Basic watch mode
npx vite-node --watch server.ts
# Watch with custom config
npx vite-node --watch --config vite.config.ts server.ts
# Watch with environment mode
npx vite-node --watch --mode development server.tsWatch Mode Features:
Use vite-node as a script runner for executable scripts:
# Execute in script mode (forwards all arguments to script)
npx vite-node --script script.ts arg1 arg2
# Script mode ignores vite-node options after --script
npx vite-node --script script.ts --some-script-flagCreate executable scripts with hashbang:
#!/usr/bin/env vite-node --script
console.log('argv:', process.argv.slice(2));# Make executable
chmod +x ./script.ts
# Run directly
./script.ts hello world
# Output: argv: [ 'hello', 'world' ]# Use custom Vite config
npx vite-node --config ./config/vite.config.ts app.ts
# Set root directory
npx vite-node --root ./src app.ts
# Set environment mode
npx vite-node --mode production app.ts# Inline specific modules for transformation
npx vite-node --options.deps.inline="@my-org/package" app.ts
# Externalize heavy dependencies
npx vite-node \
--options.deps.external="lodash" \
--options.deps.external="moment" \
app.ts
# Use regex patterns for dependency matching
npx vite-node --options.deps.inline="/^@my-org\//" app.ts# Enable module dumping for debugging
npx vite-node --options.debug.dumpModules=true app.ts
# Dump to custom directory
npx vite-node --options.debug.dumpModules="./debug-output" app.ts
# Load dumped modules instead of transforming
npx vite-node --options.debug.loadDumppedModules=true app.ts# Enable inline source maps
npx vite-node --options.sourcemap=true app.ts
# Disable source maps
npx vite-node --options.sourcemap=false app.tsConfigure vite-node behavior using environment variables:
# Enable debug dumping
VITE_NODE_DEBUG_DUMP=true npx vite-node app.ts
# Load dumped modules
VITE_NODE_DEBUG_DUMP=load npx vite-node app.ts
# Enable runner debugging
VITE_NODE_DEBUG_RUNNER=true npx vite-node app.ts
# Configure module directories
VITE_NODE_DEPS_MODULE_DIRECTORIES=/custom/modules npx vite-node app.ts{
"scripts": {
"dev": "vite-node --watch src/server.ts",
"start": "vite-node src/server.ts",
"debug": "vite-node --options.debug.dumpModules=true src/app.ts",
"build": "vite-node scripts/build.ts"
}
}FROM node:18
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
# Use vite-node for development
CMD ["npx", "vite-node", "--watch", "src/server.ts"]{
"apps": [{
"name": "my-app",
"script": "npx",
"args": ["vite-node", "src/server.ts"],
"instances": 1,
"exec_mode": "fork",
"watch": false,
"env": {
"NODE_ENV": "production"
}
}]
}# Externalize heavy dependencies to skip transformation
npx vite-node \
--options.deps.external="lodash" \
--options.deps.external="moment" \
--options.deps.external="/^@babel\//" \
app.ts# Use custom cache directory
npx vite-node --options.deps.cacheDir="./.vite-cache" app.ts
# Enable CJS fallback for compatibility
npx vite-node --options.deps.fallbackCJS=true app.ts# Configure transform modes for better performance
npx vite-node \
--options.transformMode.ssr="/\.server\./" \
--options.transformMode.web="/\.client\./" \
app.tsCommon CLI error scenarios and solutions:
# Error: Cannot find module 'my-package'
# Solution: Configure module directories
npx vite-node --options.deps.moduleDirectories="/custom/modules/" app.ts# Error: Transform failed
# Solution: Enable debug mode to inspect transformed code
VITE_NODE_DEBUG_DUMP=true npx vite-node app.ts
# Check .vite-node/dump/ directory for transformed code# Error: Invalid config
# Solution: Use absolute path for config file
npx vite-node --config "$(pwd)/vite.config.ts" app.ts| Feature | vite-node | ts-node | tsx |
|---|---|---|---|
| TypeScript Support | ✅ | ✅ | ✅ |
| ESM Support | ✅ | ⚠️ | ✅ |
| Vite Plugins | ✅ | ❌ | ❌ |
| Transform Speed | ⚡ Fast | 🐌 Slow | ⚡ Fast |
| HMR Support | ✅ | ❌ | ❌ |
| Vue/Svelte Support | ✅ | ❌ | ❌ |
| Watch Mode | ✅ | ✅ | ✅ |
| Source Maps | ✅ | ✅ | ✅ |
# Debug transformation issues
VITE_NODE_DEBUG_DUMP=true npx vite-node app.ts
# Debug runner issues
VITE_NODE_DEBUG_RUNNER=true npx vite-node app.ts
# Debug watcher issues (in watch mode)
VITE_TEST_WATCHER_DEBUG=true npx vite-node --watch app.ts