vite-node
Vite as Node.js runtime
Last updated 4 months ago by vitestbot .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install vite-node 
SYNC missed versions from official npm registry.

vite-node

Vite as Node runtime.
The engine that powers Vitest and Nuxt 3 Dev SSR.

Features

  • On-demand evaluation
  • Vite's pipeline, plugins, resolve, aliasing
  • Out-of-box ESM & TypeScript support
  • Respect vite.config.ts
  • Hot module replacement (HMR)
  • Separate server/client architecture
  • Top-level await
  • 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

Options via CLI

All ViteNodeServer options are supported by the CLI. They may be defined through the dot syntax, as shown below:

npx vite-node --options.deps.inline="module-name" --options.deps.external="/module-regexp/" index.ts

Note that for options supporting RegExps, strings passed to the CLI must start and end with a /;

Hashbang

If you prefer to write scripts that don't need to be passed into Vite Node, you can declare it in the hashbang.

Simply add #!/usr/bin/env vite-node --script at the top of your file:

file.ts

#!/usr/bin/env vite-node --script

console.log('argv:', process.argv.slice(2))

And make the file executable:

chmod +x ./file.ts

Now, you can run the file without passing it into Vite Node:

$ ./file.ts hello
argv: [ 'hello' ]

Note that when using the --script option, Vite Node forwards every argument and option to the script to execute, even the one supported by Vite Node itself.

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 both (server and runner) running in the same context

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

// create vite server
const server = await createServer({
  optimizeDeps: {
    // It's recommended to disable deps optimization
    disabled: true,
  },
})
// this is need to initialize the plugins
await server.pluginContainer.buildStart({})

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

// fixes stacktraces in Errors
installSourcemapsSupport({
  getSourceMap: source => node.getSourceMap(source),
})

// 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)
  },
  resolveId(id, importer) {
    return node.resolveId(id, importer)
  },
})

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

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

Debugging

Debug Transformation

Sometimes you might want to inspect the transformed code to investigate issues. You can set environment variable VITE_NODE_DEBUG_DUMP=true to let vite-node write the transformed result of each module under .vite-node/dump.

If you want to debug by modifying the dumped code, you can change the value of VITE_NODE_DEBUG_DUMP to load and search for the dumped files and use them for executing.

VITE_NODE_DEBUG_DUMP=load vite-node example.ts

Or programmatically:

import { ViteNodeServer } from 'vite-node/server'

const server = new ViteNodeServer(viteServer, {
  debug: {
    dumpModules: true,
    loadDumppedModules: true,
  },
})

Debug Execution

If the process gets stuck, it might be because there are unresolvable circular dependencies. You can set VITE_NODE_DEBUG_RUNNER=true for vite-node to warn about this.

VITE_NODE_DEBUG_RUNNER=true vite-node example.ts

Or programmatically:

import { ViteNodeRunner } from 'vite-node/client'

const runner = new ViteNodeRunner({
  debug: true,
})

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

Current Tags

  • 2.1.0-beta.7                                ...           beta (2 months ago)
  • 2.1.3                                ...           latest (a month ago)

255 Versions

  • 2.1.3                                ...           a month ago
  • 2.1.2                                ...           a month ago
  • 2.1.1                                ...           2 months ago
  • 2.1.0                                ...           2 months ago
  • 2.1.0-beta.7                                ...           2 months ago
  • 2.1.0-beta.6                                ...           3 months ago
  • 2.1.0-beta.5                                ...           3 months ago
  • 2.1.0-beta.4                                ...           3 months ago
  • 2.1.0-beta.3                                ...           3 months ago
  • 2.1.0-beta.2                                ...           3 months ago
  • 2.1.0-beta.1                                ...           3 months ago
  • 2.0.5                                ...           3 months ago
  • 2.0.4                                ...           4 months ago
  • 2.0.3                                ...           4 months ago
  • 2.0.2                                ...           4 months ago
  • 2.0.1                                ...           4 months ago
  • 2.0.0                                ...           4 months ago
  • 2.0.0-beta.13                                ...           4 months ago
  • 2.0.0-beta.12                                ...           5 months ago
  • 2.0.0-beta.11                                ...           5 months ago
  • 2.0.0-beta.10                                ...           5 months ago
  • 2.0.0-beta.9                                ...           5 months ago
  • 2.0.0-beta.8                                ...           5 months ago
  • 2.0.0-beta.7                                ...           5 months ago
  • 2.0.0-beta.6                                ...           5 months ago
  • 2.0.0-beta.5                                ...           5 months ago
  • 2.0.0-beta.4                                ...           5 months ago
  • 2.0.0-beta.3                                ...           6 months ago
  • 2.0.0-beta.2                                ...           6 months ago
  • 2.0.0-beta.1                                ...           6 months ago
  • 1.6.0                                ...           6 months ago
  • 1.5.3                                ...           7 months ago
  • 1.5.2                                ...           7 months ago
  • 1.5.1                                ...           7 months ago
  • 1.5.0                                ...           7 months ago
  • 1.4.0                                ...           8 months ago
  • 1.3.1                                ...           9 months ago
  • 1.3.0                                ...           9 months ago
  • 1.2.2                                ...           10 months ago
  • 1.2.1                                ...           10 months ago
  • 1.2.0                                ...           10 months ago
  • 1.1.3                                ...           10 months ago
  • 1.1.2                                ...           10 months ago
  • 1.1.1                                ...           10 months ago
  • 1.1.0                                ...           a year ago
  • 1.0.4                                ...           a year ago
  • 1.0.3                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 1.0.0-beta.6                                ...           a year ago
  • 1.0.0-beta.5                                ...           a year ago
  • 1.0.0-beta.4                                ...           a year ago
  • 1.0.0-beta.3                                ...           a year ago
  • 1.0.0-beta.2                                ...           a year ago
  • 1.0.0-beta.1                                ...           a year ago
  • 0.34.7                                ...           a year ago
  • 1.0.0-beta.0                                ...           a year ago
  • 0.34.6                                ...           a year ago
  • 0.34.5                                ...           a year ago
  • 0.34.4                                ...           a year ago
  • 0.34.3                                ...           a year ago
  • 0.34.2                                ...           a year ago
  • 0.34.1                                ...           a year ago
  • 0.34.0                                ...           a year ago
  • 0.33.0                                ...           a year ago
  • 0.32.4                                ...           a year ago
  • 0.32.3                                ...           a year ago
  • 0.32.2                                ...           a year ago
  • 0.32.1                                ...           a year ago
  • 0.32.0                                ...           a year ago
  • 0.31.4                                ...           a year ago
  • 0.31.3                                ...           a year ago
  • 0.31.2                                ...           a year ago
  • 0.31.1                                ...           a year ago
  • 0.31.0                                ...           2 years ago
  • 0.30.1                                ...           2 years ago
  • 0.30.0                                ...           2 years ago
  • 0.29.8                                ...           2 years ago
  • 0.29.7                                ...           2 years ago
  • 0.29.6                                ...           2 years ago
  • 0.29.5                                ...           2 years ago
  • 0.29.4                                ...           2 years ago
  • 0.29.3                                ...           2 years ago
  • 0.29.2                                ...           2 years ago
  • 0.29.1                                ...           2 years ago
  • 0.29.0                                ...           2 years ago
  • 0.28.5                                ...           2 years ago
  • 0.28.4                                ...           2 years ago
  • 0.28.3                                ...           2 years ago
  • 0.28.2                                ...           2 years ago
  • 0.28.1                                ...           2 years ago
  • 0.28.0                                ...           2 years ago
  • 0.27.3                                ...           2 years ago
  • 0.27.2                                ...           2 years ago
  • 0.27.1                                ...           2 years ago
  • 0.27.0                                ...           2 years ago
  • 0.26.3                                ...           2 years ago
  • 0.26.2                                ...           2 years ago
  • 0.26.1                                ...           2 years ago
  • 0.26.0                                ...           2 years ago
  • 0.25.8                                ...           2 years ago
  • 0.25.7                                ...           2 years ago
  • 0.25.6                                ...           2 years ago
  • 0.25.5                                ...           2 years ago
  • 0.25.4                                ...           2 years ago
  • 0.25.3                                ...           2 years ago
  • 0.25.2                                ...           2 years ago
  • 0.25.1                                ...           2 years ago
  • 0.25.0                                ...           2 years ago
  • 0.24.5                                ...           2 years ago
  • 0.24.4                                ...           2 years ago
  • 0.24.3                                ...           2 years ago
  • 0.24.2                                ...           2 years ago
  • 0.24.1                                ...           2 years ago
  • 0.24.0                                ...           2 years ago
  • 0.23.4                                ...           2 years ago
  • 0.23.2                                ...           2 years ago
  • 0.23.1                                ...           2 years ago
  • 0.23.0                                ...           2 years ago
  • 0.22.1                                ...           2 years ago
  • 0.22.0                                ...           2 years ago
  • 0.21.1                                ...           2 years ago
  • 0.21.0                                ...           2 years ago
  • 0.20.3                                ...           2 years ago
  • 0.20.2                                ...           2 years ago
  • 0.20.1                                ...           2 years ago
  • 0.20.0                                ...           2 years ago
  • 0.19.1                                ...           2 years ago
  • 0.19.0                                ...           2 years ago
  • 0.18.1                                ...           2 years ago
  • 0.18.0                                ...           2 years ago
  • 0.17.1                                ...           2 years ago
  • 0.17.0                                ...           2 years ago
  • 0.16.0                                ...           2 years ago
  • 0.15.2                                ...           2 years ago
  • 0.15.1                                ...           2 years ago
  • 0.15.0                                ...           2 years ago
  • 0.14.2                                ...           2 years ago
  • 0.14.1                                ...           2 years ago
  • 0.14.0                                ...           2 years ago
  • 0.13.1                                ...           2 years ago
  • 0.13.0                                ...           2 years ago
  • 0.12.10                                ...           2 years ago
  • 0.12.9                                ...           2 years ago
  • 0.12.8                                ...           2 years ago
  • 0.12.7                                ...           2 years ago
  • 0.12.6                                ...           3 years ago
  • 0.12.5                                ...           3 years ago
  • 0.12.4                                ...           3 years ago
  • 0.12.3                                ...           3 years ago
  • 0.12.2                                ...           3 years ago
  • 0.12.1                                ...           3 years ago
  • 0.12.0                                ...           3 years ago
  • 0.11.0                                ...           3 years ago
  • 0.10.5                                ...           3 years ago
  • 0.10.4                                ...           3 years ago
  • 0.10.3                                ...           3 years ago
  • 0.10.2                                ...           3 years ago
  • 0.10.1                                ...           3 years ago
  • 0.10.0                                ...           3 years ago
  • 0.9.4                                ...           3 years ago
  • 0.9.3                                ...           3 years ago
  • 0.9.2                                ...           3 years ago
  • 0.9.1                                ...           3 years ago
  • 0.9.0                                ...           3 years ago
  • 0.8.5                                ...           3 years ago
  • 0.8.4                                ...           3 years ago
  • 0.8.3                                ...           3 years ago
  • 0.8.2                                ...           3 years ago
  • 0.8.1                                ...           3 years ago
  • 0.8.0                                ...           3 years ago
  • 0.7.13                                ...           3 years ago
  • 0.7.12                                ...           3 years ago
  • 0.7.11                                ...           3 years ago
  • 0.7.10                                ...           3 years ago
  • 0.7.9                                ...           3 years ago
  • 0.7.8                                ...           3 years ago
  • 0.7.7                                ...           3 years ago
  • 0.7.6                                ...           3 years ago
  • 0.7.5                                ...           3 years ago
  • 0.7.4                                ...           3 years ago
  • 0.7.3                                ...           3 years ago
  • 0.7.2                                ...           3 years ago
  • 0.7.1                                ...           3 years ago
  • 0.7.0                                ...           3 years ago
  • 0.6.3                                ...           3 years ago
  • 0.6.1                                ...           3 years ago
  • 0.6.0                                ...           3 years ago
  • 0.5.9                                ...           3 years ago
  • 0.5.8                                ...           3 years ago
  • 0.5.7                                ...           3 years ago
  • 0.5.6                                ...           3 years ago
  • 0.5.5                                ...           3 years ago
  • 0.5.4                                ...           3 years ago
  • 0.5.3                                ...           3 years ago
  • 0.5.2                                ...           3 years ago
  • 0.5.1                                ...           3 years ago
  • 0.5.0                                ...           3 years ago
  • 0.4.3                                ...           3 years ago
  • 0.4.2                                ...           3 years ago
  • 0.4.1                                ...           3 years ago
  • 0.4.0                                ...           3 years ago
  • 0.3.6                                ...           3 years ago
  • 0.3.5                                ...           3 years ago
  • 0.3.4                                ...           3 years ago
  • 0.3.3                                ...           3 years ago
  • 0.3.2                                ...           3 years ago
  • 0.3.1                                ...           3 years ago
  • 0.3.0                                ...           3 years ago
  • 0.2.8                                ...           3 years ago
  • 0.2.7                                ...           3 years ago
  • 0.2.6                                ...           3 years ago
  • 0.2.5                                ...           3 years ago
  • 0.2.4                                ...           3 years ago
  • 0.2.3                                ...           3 years ago
  • 0.2.2                                ...           3 years ago
  • 0.2.1                                ...           3 years ago
  • 0.2.0                                ...           3 years ago
  • 0.1.27                                ...           3 years ago
  • 0.1.26                                ...           3 years ago
  • 0.1.25                                ...           3 years ago
  • 0.1.24                                ...           3 years ago
  • 0.1.23                                ...           3 years ago
  • 0.1.22                                ...           3 years ago
  • 0.1.21                                ...           3 years ago
  • 0.1.20                                ...           3 years ago
  • 0.1.19                                ...           3 years ago
  • 0.1.18                                ...           3 years ago
  • 0.1.17                                ...           3 years ago
  • 0.1.16                                ...           3 years ago
  • 0.1.13                                ...           3 years ago
  • 0.1.12                                ...           3 years ago
  • 0.0.142                                ...           3 years ago
  • 0.0.141                                ...           3 years ago
  • 0.0.140                                ...           3 years ago
  • 0.1.11                                ...           3 years ago
  • 0.1.10                                ...           3 years ago
  • 0.1.9                                ...           3 years ago
  • 0.1.8                                ...           3 years ago
  • 0.1.7                                ...           3 years ago
  • 0.1.6                                ...           3 years ago
  • 0.1.5                                ...           3 years ago
  • 0.1.4                                ...           3 years ago
  • 0.1.3                                ...           3 years ago
  • 0.1.2                                ...           3 years ago
  • 0.1.1                                ...           3 years ago
  • 0.1.0                                ...           3 years ago
  • 0.0.5                                ...           3 years ago
  • 0.0.4                                ...           3 years ago
  • 0.0.3                                ...           3 years ago
  • 0.0.2                                ...           3 years ago
  • 0.0.1                                ...           3 years ago
  • 0.0.0                                ...           3 years ago
  • 0.0.0-alpha.0                                ...           4 years ago
Downloads
Total 95
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 59
Dependencies (5)
Dev Dependencies (2)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE