<!-- automd:badges color=yellow packagephobia -->
npm install unenv-nightly


> [!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.
- Nitro
- Nuxt
- Cloudflare
- ESM.sh
The defineEnv utility can generate a target environment configuration.
``js
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/plugin-alias
| -------- | ----------------------------------------------------------------------------- | ------------------------------------------------------------------------------ | ------------------------------------------------------------------------ |
| rollup | | @rollup/plugin-inject | external |resolve.alias
| rolldown | | inject | external |resolve.alias
| vite | | @rollup/plugin-inject | ssr.external |alias
| esbuild | | inject | external |resolve.alias
| rspack | | - | externals |resolve.alias
| webpack | | webpack-plugin-inject | externals |
- nodeCompat: (default: true)alias
- Add entries for Node.js builtins as and node:.inject
- Add entries for Node.js globals global, Buffer, and process.npmShims
- : (default: false)alias
- Add entries to replace npm packages like node-fetch with lighter shims.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
`js
// 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:
`json`
{
"devDependencies": {
"unenv": "npm:unenv-nightly"
}
}
If using unenv via another tool (Nuxt or Nitro) in your project:
`json``
{
"resolutions": {
"unenv": "npm:unenv-nightly"
}
}
Published under the MIT license.
Made by @pi0 and community 💛
---
_🤖 auto updated with automd_