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.
# ✨ 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 unenvUsing 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
fetchAPI - 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 - 🚧 mocked using proxy
- node:assert/strict - 🚧 mocked using proxy
- node:async_hooks - ✅ polyfilled 6/7 exports
- node:buffer - ✅ polyfilled all exports
- node:child_process - ✅ polyfilled all exports
- node:cluster - ✅ polyfilled all exports
- node:console - ✅ polyfilled 23/25 exports
- node:constants - ✅ polyfilled all exports
- node:crypto - ✅ polyfilled all exports
- node:dgram - ✅ polyfilled all exports
- node:diagnostics_channel - ✅ polyfilled all exports
- node:dns - ✅ polyfilled all exports
- node:dns/promises - ✅ polyfilled all exports
- node:domain - ✅ polyfilled all exports
- node:events - ✅ polyfilled 2/15 exports
- node:fs - ✅ polyfilled all exports
- node:fs/promises - ✅ polyfilled all exports
- node:http - ✅ polyfilled 16/17 exports
- node:http2 - ✅ polyfilled all exports
- node:https - ✅ polyfilled all exports
- node:inspector - ✅ polyfilled all exports
- node:inspector/promises - 🚧 mocked using proxy
- node:module - ✅ polyfilled 9/21 exports
- node:net - ✅ polyfilled 14/18 exports
- node:os - ✅ polyfilled all exports
- node:path - ✅ polyfilled 15/16 exports
- node:path/posix - ✅ polyfilled 15/16 exports
- node:path/win32 - ✅ polyfilled 15/16 exports
- node:perf_hooks - ✅ polyfilled all exports
- node:process - ✅ polyfilled 84/92 exports
- node:punycode - ✅ polyfilled all exports
- node:querystring - ✅ polyfilled all exports
- node:readline - ✅ polyfilled all exports
- node:readline/promises - ✅ polyfilled all exports
- node:repl - 🚧 mocked using proxy
- node:stream - ✅ polyfilled 17/37 exports
- node:stream/consumers - ✅ polyfilled all exports
- node:stream/promises - ✅ polyfilled all exports
- node:stream/web - ✅ polyfilled 16/17 exports
- node:string_decoder - ✅ polyfilled all exports
- node:sys - ✅ polyfilled all exports
- node:timers - ✅ polyfilled all exports
- node:timers/promises - ✅ polyfilled all exports
- node:tls - ✅ polyfilled all exports
- node:trace_events - ✅ polyfilled all exports
- node:tty - ✅ polyfilled all exports
- node:url - ✅ polyfilled 10/12 exports
- node:util - ✅ polyfilled all exports
- node:util/types - ✅ polyfilled all exports
- node:v8 - ✅ polyfilled all exports
- node:vm - ✅ polyfilled all exports
- node:wasi - ✅ polyfilled all exports
- node:worker_threads - ✅ polyfilled all exports
- node:zlib - ✅ polyfilled all exports
unenv provides a replacement for common npm packages for cross platform compatibility.
import MockProxy from "unenv/runtime/mock/proxy";
console.log(MockProxy().foo.bar()[0]);The above package doesn't work outside of Node.js and neither we need any platform-specific logic! When aliasing os to mock/proxy-cjs, it will be auto-mocked using a Proxy Object which can be recursively traversed like an Object, called like a Function, Iterated like an Array, or instantiated like a Class.
We use this proxy for auto-mocking unimplemented internals. Imagine a package does this:
const os = require("node:os");
if (os.platform() === "windows") {
/* do some fix */
}
module.exports = "Hello world";By aliasing os to unenv/runtime/mock/proxy-cjs, the code will be compatible with other platforms.
To discover other polyfills, please check ./src/runtime.
You can use nightly release channel to try latest changes in 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