Note
You are on the development (v2) branch. Check out v1 for the current release.
unenv provides polyfills to add Node.js compatibility for any JavaScript runtime, including browsers and edge workers.
The defineEnv
utility can generate a target environment configuration.
import { defineEnv } from "unenv";
const { env } = defineEnv({
nodeCompat: true,
npmShims: true,
resolve: true,
overrides: {},
presets: [],
});
const { alias, inject, external, polyfill } = env;
You can then integrate the env object with your build tool:
Bundler | alias |
inject |
external |
---|---|---|---|
rollup | @rollup/plugin-alias |
@rollup/plugin-inject |
external |
rolldown | resolve.alias |
inject |
external |
vite | resolve.alias |
@rollup/plugin-inject |
ssr.external |
esbuild | alias |
inject |
external |
rspack | resolve.alias |
- | externals |
webpack | resolve.alias |
webpack-plugin-inject |
externals |
nodeCompat
: (default:true
)- Add
alias
entries for Node.js builtins as<id>
andnode:<id>
. - Add
inject
entries for Node.js globalsglobal
,Buffer
, andprocess
.
- Add
npmShims
: (default:false
)- Add
alias
entries to replace npm packages likenode-fetch
with lighter shims.
- Add
resolve
: (default:false
) Resolve config values to absolute paths.overrides
: Additional overrides for env config.presets
: Additional presets (for example@cloudflare/unenv-preset
).
You can also directly import unenv/
polyfills:
Polyfills | Description | Source |
---|---|---|
unenv/mock/* |
Mocking utils | src/runtime/mock |
unenv/node/* |
APIs compatible with Node.js API |
src/runtime/node |
unenv/npm/* |
NPM package shims | src/runtime/npm |
unenv/polyfill/* |
Global polyfills | src/runtime/polyfill |
unenv/web/* |
Subset of Web APIs | src/runtime/web |
unenv
replaces Node.js built-in modules compatible with any runtime (view source).
- β node:assert
- β node:assert/strict
- β node:async_hooks
- β node:buffer
- β node:child_process
- β node:cluster
- β node:console
- β node:constants
- β node:crypto
- β node:dgram
- β node:diagnostics_channel
- β node:dns
- β node:dns/promises
- β node:domain
- β node:events
- β node:fs
- β node:fs/promises
- β node:http
- β node:http2
- β node:https
- β node:inspector
- β node:inspector/promises
- β node:module
- β node:net
- β node:os
- β node:path
- β node:path/posix
- β node:path/win32
- β node:perf_hooks
- β node:process
- β node:punycode
- β node:querystring
- β node:readline
- β node:readline/promises
- β node:repl
- β node:stream
- β node:stream/consumers
- β node:stream/promises
- β node:stream/web
- β node:string_decoder
- β node:sys
- β node:timers
- β node:timers/promises
- β node:tls
- β node:trace_events
- β node:tty
- β node:url
- β node:util
- β node:util/types
- β node:v8
- β node:vm
- β node:wasi
- β node:worker_threads
- β node:zlib
// Magic proxy to replace any unknown API
import MockProxy from "unenv/mock/proxy";
// You can also create named mocks
const lib = MockProxy.__createMock__("lib", {
/* overrides */
});
You can use the nightly release channel to try the latest changes in the main
branch via unenv-nightly
.
If directly using unenv
in your project:
{
"devDependencies": {
"unenv": "npm:unenv-nightly"
}
}
If using unenv
via another tool (Nuxt or Nitro) in your project:
{
"resolutions": {
"unenv": "npm:unenv-nightly"
}
}
Published under the MIT license.
Made by @pi0 and community π
π€ auto updated with automd