unenv
Convert JavaScript code to be runtime agnostic
unenv provides a collection of Node.js and Web polyfills and mocking utilities with configurable presets for converting JavaScript code and libraries to be platform and runtime agnostic, working in any environment including Browsers, Workers, Node.js, Cloudflare Workers, Deno.
Unenv is used by Nitro and Nuxt today.
Install
# ✨ Auto-detect
npx nypm install -D unenv
# npm
npm install -D unenv
# yarn
yarn add -D unenv
# pnpm
pnpm install -D unenv
# bun
bun install -D unenv
# deno
deno install --dev unenv
Usage
Using env
utility and built-in presets, unenv
will provide an abstract configuration that can be used in bundlers (rollup.js, webpack, etc.).
import { env } from "unenv";
const { alias, inject, polyfill, external } = env({}, {}, {});
Note: You can provide as many presets as you want. unenv will merge them internally and the right-most preset has a higher priority.
Presets
node
Suitable to convert universal libraries working in Node.js.
- Add supports for global
fetch
API - Set Node.js built-ins as externals
import { env, node } from "unenv";
const envConfig = env(node, {});
nodeless
Suitable to transform libraries made for Node.js to run in other JavaScript runtimes.
import { env, nodeless } from "unenv";
const envConfig = env(nodeless, {});
deno
This preset can be used to extend nodeless
to use Deno's Node.js API Compatibility (docs, docs).
import { env, nodeless, deno } from "unenv";
const envConfig = env(nodeless, deno, {});
cloudflare
This preset can be used to extend nodeless
to use Cloudflare Worker Node.js API Compatibility (docs).
nodejs_compat
compatibility flag.import { env, nodeless, cloudflare } from "unenv";
const envConfig = env(nodeless, cloudflare, {});
vercel
This preset can be used to extend nodeless
to use Vercel Edge Node.js API Compatibility (docs).
import { env, nodeless, vercel } from "unenv";
const envConfig = env(nodeless, vercel, {});
Built-in Node.js modules
unenv
provides a replacement for Node.js built-in modules compatible with any runtime.
- ✅ 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
Package replacements
unenv
provides a replacement for common npm packages for cross-platform compatibility.
Manual mocking utils
// Magic proxy to replace any unknown API
import MockProxy from "unenv/runtime/mock/proxy";
// You can also create named mocks
const lib = MockProxy.__createMock__("lib", {
/* overrides */
});
Other polyfills
To discover other polyfills, please check https://raw.githubusercontent.com/unjs/unenv/main/src/runtime.
Nightly release channel
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"
}
}
License
Published under the MIT license. Made by @pi0 and community 💛