Socket
Socket
Sign inDemoInstall

vite-node

Package Overview
Dependencies
5
Maintainers
2
Versions
238
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

vite-node


Version published
Maintainers
2
Created

Package description

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

Readme

Source

vite-node

NPM version

Vite as Node runtime. The engine powers Vitest.

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.

CLI Usage

Run JS/TS file on Node.js using Vite's resolvers and transformers.

npx vite-node index.ts

Options:

npx vite-node -h

Programmatic Usage

In Vite Node, the server and runner (client) are separated, so you can integrate them in different contexts (workers, cross-process, or remote) if needed. The demo below shows a simple example of having the server and running in the same context

import { createServer } from 'vite'
import { ViteNodeServer } from 'vite-node/server'
import { ViteNodeRunner } from 'vite-node/client'

// create vite server
const server = await createServer()
// this is need to initialize the plugins
await server.pluginContainer.buildStart({})

// create vite-node server
const node = new ViteNodeServer(server)

// create vite-node runner
const runner = new ViteNodeRunner({
  root: server.config.root,
  base: server.config.base,
  // when having the server and runner in a different context,
  // you will need to handle the communication between them
  // and pass to this function
  fetchModule(id) {
    return node.fetchModule(id)
  },
})

// execute the file
await runner.executeFile('./example.ts')

// close the vite server
await server.close()

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.

Sponsors

License

MIT License © 2021 Anthony Fu

FAQs

Last updated on 21 Jan 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc