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.
Note
You are on the development (v2) branch. Check out v1 for the current release.
# β¨ 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
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.
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, {});
Suitable to transform libraries made for Node.js to run in other JavaScript runtimes.
import { env, nodeless } from "unenv";
const envConfig = env(nodeless, {});
This preset can be used to extend nodeless
to use Deno's Node.js API Compatibility (docs, docs).
Warning
This preset is experimental and behavior might change!
import { env, nodeless, deno } from "unenv";
const envConfig = env(nodeless, deno, {});
This preset can be used to extend nodeless
to use Cloudflare Worker Node.js API Compatibility (docs).
Warning
This preset is experimental and behavior might change!
Note
Make sure to enable nodejs_compat
compatibility flag.
import { env, nodeless, cloudflare } from "unenv";
const envConfig = env(nodeless, cloudflare, {});
This preset can be used to extend nodeless
to use Vercel Edge Node.js API Compatibility (docs).
Warning
This preset is experimental and behavior might change!
import { env, nodeless, vercel } from "unenv";
const envConfig = env(nodeless, vercel, {});
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
unenv
provides a replacement for common npm packages for cross-platform compatibility.
// 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 */
});
To discover other polyfills, please check ./src/runtime.
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