Package detail

unenv

unjs7.7mMIT1.10.0

unenv is a framework-agnostic system that allows converting JavaScript code to be platform agnostic and work in any environment including Browsers, Workers, Node.js, or JavaScript runtime.

readme

unenv

unenv is a framework-agnostic system that allows converting JavaScript code to be platform agnostic and work in any environment including Browsers, Workers, Node.js, or JavaScript runtime.

[!NOTE] You are on the legacy (v1) branch. Checkout main branch for latest development.

Install

# Using npm
npm i -D unenv

# Using yarn
yarn add --dev unenv

# Using pnpm
pnpm add -D unenv

Usage

Using env utility and built-in presets, unenv will provide an abstract configuration that can be used in building pipelines (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

(view source)

Suitable to convert universal libraries working in Node.js.

  • Add supports for global fetch API
  • Set Node.js built-ins as externals
import { env, nodeless } from "unenv";

const envConfig = env(node, {});

nodeless

(view source)

Suitable to transform libraries made for Node.js to run in other JavaScript runtimes.

import { env, nodeless } from "unenv";

const envConfig = env(nodeless, {});

deno

(view source)

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, {});

cloudflare

(view source)

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, {});

vercel

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, {});

Built-in Node.js modules

unenv provides a replacement for all Node.js built-ins for cross-platform compatibility.

Module Status Source
node:assert Mocked -
node:async_hooks Polyfilled unenv/node/async_hooks
node:buffer Polyfilled unenv/node/buffer
node:child_process Mocked -
node:cluster Mocked -
node:console Mocked -
node:constants Mocked -
node:crypto Polyfilled unenv/node/crypto
node:dgram Mocked -
node:diagnostics_channel Mocked -
node:dns Mocked -
node:domain Mocked -
node:events Polyfilled unenv/node/events
node:fs Polyfilled unenv/node/fs
node:fs/promises Polyfilled unenv/node/fs/promises
node:http2 Mocked -
node:http Polyfilled unenv/node/http
node:https Polyfilled unenv/node/https
node:inspector Mocked -
node:module Polyfilled unenv/node/module -
node:net Polyfilled unenv/node/net
node:os Mocked -
node:path Polyfilled unenv/node/path
node:perf_hooks Mocked -
node:process Polyfilled unenv/node/process
node:punycode Mocked -
node:querystring Mocked -
node:readline Mocked -
node:repl Mocked -
node:stream Polyfilled unenv/node/stream
node:stream/consumers Mocked unenv/node/stream/consumers
node:stream/promises Mocked unenv/node/stream/promises
node:stream/web Native unenv/node/stream/web
node:string_decoder Polyfilled unenv/node/string_decoder
node:sys Mocked -
node:timers Mocked -
node:timers/promises Mocked -
node:tls Mocked -
node:trace_events Mocked -
node:tty Mocked -
node:url Polyfilled unenv/node/url
node:util Polyfilled unenv/node/util
node:util/types Polyfilled unenv/node/util/types
node:v8 Mocked -
node:vm Mocked -
node:wasi Mocked -
node:worker_threads Mocked -
node:zlib Mocked -

npm packages

unenv provides a replacement for common npm packages for cross platform compatibility.

Package Status Source
npm/consola Use native console unenv/runtime/npm/consola
npm/cross-fetch Use native fetch unenv/runtime/npm/cross-fetch
npm/debug Mocked with console.debug unenv/runtime/npm/debug
npm/fsevents Mocked unenv/runtime/npm/fsevents
npm/inherits Inlined unenv/runtime/npm/inherits
npm/mime-db Minimized unenv/runtime/npm/mime-db
npm/mime Minimized unenv/runtime/npm/mime
npm/node-fetch Use native fetch unenv/runtime/npm/node-fetch
npm/whatwg-url Use native URL unenv/runtime/npm/whatwg-url

Auto-mocking proxy

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 = function main() {
  return "Hello world"
}

By aliasing os to unenv/runtime/mock/proxy-cjs, the code will be compatible with other platforms.

Other polyfills

To discover other polyfills, please check ./src/runtime.

License

MIT

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v1.10.0

compare changes

🚀 Enhancements

  • Implement node:os (#282)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.8.0

compare changes

🚀 Enhancements

  • Polyfill node:module (#148)
  • Add polyfill for node:https with named exports (5917a50)
  • Support consola/utils subpath (#149)

🩹 Fixes

  • node:net: Add missing new exports (2b805b1)
  • node:net/socket: Add new destroySoon and autoSelectFamilyAttemptedAddresses (311c72b)

🏡 Chore

🎨 Styles

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.7.4

compare changes

🩹 Fixes

  • node:fs: Named exports for /promises subpath (b035e09)
  • node:stream: Add placeholder for not implemented new method (294f283)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.7.3

compare changes

🩹 Fixes

  • Add AbortController export from node-fetch (230385c)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.7.2

compare changes

🩹 Fixes

  • node:fs: Default export for node:fs/promises (37fd173)
  • node:crypto: Bind crypto functions to avoid Illegal invocation error (#134)
  • node:fs: Add mock for openAsBlob (16f6150)
  • node:stream: Support asyncDispose for readable (f8f8f46)

🏡 Chore

❤️ Contributors

v1.7.1

compare changes

🩹 Fixes

  • node:stream: Allow tree-shaking Duplex (587860f)
  • Add sideEffects to main package.json to allow tree-shaking (6dac339)
  • node:string_decoder: Use relative import (#129)

💅 Refactors

  • node:events: Rewrite EventEmitter (#128)

❤️ Contributors

v1.7.0

compare changes

🚀 Enhancements

  • Expose __unenv__ flag for unenv classes and functions (#125)
  • node: Allow overriding AsyncHook, AsyncLocalStorage and AsyncResource with globalThis (#126)
  • node:buffer: Allow overriding Buffer with globalThis (1337f98)
  • node:events: Allow overriding EventEmitter with globalThis (5ba2d03)
  • node:stream: Allow overriding Duplex, Readable, Transform and Writable with globalThis (e06358d)
  • Add node:string_decoder with global polyfill (002467a)

🩹 Fixes

  • fetch: Avoid sending body for null body reponses (#124)
  • node:crypto: Properly call web crypto methods (#122)

🏡 Chore

🤖 CI

❤️ Contributors

v1.6.2

compare changes

🩹 Fixes

  • Add async_hooks to nodeless preset (dee2ddd)
  • node: Add named exports for AsyncLocalStorage and AsyncResource (388f529)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.6.1

compare changes

🌊 Types

  • node: Type EventEmitter in dist (0106e19)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.6.0

compare changes

🚀 Enhancements

  • node: Export full node crypto exports (#121)

🩹 Fixes

  • node: Preserve http number header type with setHeader (#119)
  • node: Add missing compose to stream classes (5687712)
  • node: Import EventEmitter from local path (ecd741a)

🏡 Chore

🤖 CI

  • Use conventional commit for autofix action (#120)

❤️ Contributors

v1.5.2

compare changes

🩹 Fixes

  • node/utils: Add missing types named export (cf45410)
  • node/net: Add missing ip utils and SocketAddress class (7abe54e)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.5.1

compare changes

🩹 Fixes

  • Keep consola/core subpath as is (40617bc)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.5.0

compare changes

🚀 Enhancements

  • Implement basic node:async_hooks (#98)
  • Use consola/core instead of mocking consola (#93)
  • http: Support IncomingMessage.headersDistinct and IncomingMessage.trailersDistinct (08e36cc)
  • node/crypto: Implement randomBytes (#100)
  • http: Support ServerResponse.appendHeader (c492d45)

🩹 Fixes

  • async_hooks: Add snapshot stub (1eec581)

🏡 Chore

❤️ Contributors

v1.4.1

compare changes

🩹 Fixes

  • node:process: Avoid recursive access (a1ba86c)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.4.0

compare changes

🚀 Enhancements

  • node:process: Allow accessing process.env from dynamic sources (#95)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.3.1

compare changes

🩹 Fixes

  • node: Add webcrypto export for node:crypto (fb2b280)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.3.0

compare changes

🚀 Enhancements

  • npm: Add named consola export for consola v3 compat (7e67d71)
  • node: Add node:crypto polyfill (#90)

🩹 Fixes

  • node: Add new statfs exports (080cf5b)
  • pkg: Move types export condition (2d5a8dd)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.2.2

compare changes

🩹 Fixes

  • node/process: Support process.env shims (893421b)
  • Add new node shims (9ad4604)

📖 Documentation

  • Fix some small typos and grammars (#80)

🏡 Chore

❤️ Contributors

v1.2.1

compare changes

🩹 Fixes

  • node/stream: Stop writing data when writableEnded is set (#79)
  • node/buffer: Add isUtf8 util (not implemented) (527904b)

📖 Documentation

  • Fix typos (#78)

❤️ Contributors

v1.2.0

compare changes

🚀 Enhancements

  • node/stream: Support writing multipe chunks (#75)
  • node/util: Implement MIMEType and MIMEParams (#76)

❤️ Contributors

v1.1.1

compare changes

🩹 Fixes

  • fetch: Only pass context key from main context (2e9b9fb)

❤️ Contributors

v1.1.0

compare changes

🚀 Enhancements

  • call: Preserve fetch context in req.__unenv__ (#72)

🩹 Fixes

  • Support import alias with buffer/index.js (660df39)

❤️ Contributors

v1.0.3

compare changes

🩹 Fixes

  • Add extend method to debug mock (#69)

❤️ Contributors

v1.0.2

compare changes

🩹 Fixes

  • events: Export once function (#68)

🏡 Chore

  • Switch to changelogen for release (745cf52)

🎨 Styles

❤️ Contributors

1.0.1 (2023-01-03)

Bug Fixes

  • add missing latest node exports (dd046a8)
  • add missing polyfill and types (97e7c6f)
  • export named fetch from node-fetch polyfill (#63) (605e649)

1.0.0 (2022-11-15)

0.6.2 (2022-08-30)

Bug Fixes

0.6.1 (2022-08-24)

Bug Fixes

  • callHandle: narrrow body type to BodyInit (7030004)

0.6.0 (2022-08-24)

⚠ BREAKING CHANGES

  • createCall: return res._data as is

Bug Fixes

  • createCall: return res._data as is (8f1235f), closes #43

0.5.4 (2022-08-12)

Bug Fixes

  • remove native node:fs import (524bc21)

0.5.3 (2022-08-08)

Bug Fixes

  • allow mocking subpaths of cjs-proxy (#46) (70efe37)
  • order aliases from specific to general (#45) (9c11eaa)

0.5.2 (2022-05-11)

Features

  • use node-fetch-native for polyfill (7944681)

0.5.1 (2022-05-05)

Features

Bug Fixes

  • add default export for process polyfill (45d859f)
  • fix process polyfill (05aa34f)
  • import node-fetch polyfill from src (0e9afae)
  • update http and add missing exports (16338d4)
  • use named export for events (f4bf593)
  • use proxy for http class mocks (b8a44b4)

0.5.0 (2022-05-05)

⚠ BREAKING CHANGES

  • replace node polyfills (#30)
  • replace node-fetch and cross-fetch with platform natives (resolves #29)

Features

  • nodeless: support node: protocol aliases (d3e42f7)
  • node: polyfill FormData, Blob and AbortController (c0b83a7), closes #23
  • opt-in support of whatwg-url mock (#15) (a38cf29)
  • replace node-fetch and cross-fetch with platform natives (resolves #29) (276d44d)
  • replace node polyfills (#30) (0a99ce2)

Bug Fixes

  • add fs/promises to NodeBuiltinModules (resolves #34) (35f31a4)
  • add missing stream exports (resolves #11) (a51e893)
  • add more node subpath exports to built-in modules (8ed02ad), closes #34
  • fix util.promisify implementation (674dd27)
  • mock mime/lite as well (e733440)

0.4.6 (2022-04-07)

Bug Fixes

  • pkg: expose cjs proxy as mock/proxy-cjs (6ac4279)

0.4.5 (2022-04-07)

Bug Fixes

  • pkg: emit cjs for entry to avoid breaking change (0d64253)

0.4.4 (2022-04-07)

Bug Fixes

  • resolve proxy to commonjs (26c19f7)

0.4.3 (2021-12-14)

Bug Fixes

0.4.2 (2021-12-01)

Features

Bug Fixes

  • return this when calling setHeader, end and destroy (#20) (4ac95fd)

0.4.1 (2021-11-04)

Bug Fixes

  • package: wrong link to github repo (#18) (b775afc)

0.4.0 (2021-10-22)

⚠ BREAKING CHANGES

  • pkg: use explicit .cjs
  • update node-fetch to 3.x

Features

0.3.10 (2021-09-29)

Bug Fixes

  • remove node-fetch alias from node preset (#14) (9addd4c)

0.3.9 (2021-09-29)

Features

  • add nodeless node-fetch polyfill (#13) (52612ac)

0.3.8 (2021-09-20)

Bug Fixes

  • duplex: add missing mergeFns import (1b301de)

0.3.7 (2021-09-20)

Bug Fixes

  • duplex: avoid cycling reference (3cd4797)

0.3.6 (2021-09-20)

Features

  • node stream duplex and transform (#12) (940e72e)

0.3.5 (2021-09-10)

Bug Fixes

  • url: add default export (c5f99d9)

0.3.4 (2021-09-10)

Features

  • add basic implementation for NodeJS url (c76edbd)

0.3.3 (2021-09-08)

Features

0.3.2 (2021-07-12)

0.3.1 (2021-07-12)

Bug Fixes

  • use subpath import for node/http (#6) (1415896)

0.3.0 (2021-06-21)

⚠ BREAKING CHANGES

  • add exports field

Features

0.2.3 (2021-04-23)

Bug Fixes

  • dual exports for node index (e28374c)

0.2.2 (2021-04-21)

Bug Fixes

  • use globalThis for polyfills (e43a8b4)

0.2.1 (2021-04-21)

0.2.0 (2021-04-21)

⚠ BREAKING CHANGES

  • update build system

Features

0.1.1 (2021-02-08)

Features

0.1.0 (2021-01-21)

0.0.12 (2021-01-21)

Features

  • use mkdist to generate runtime (54c0f5d)

Bug Fixes

0.0.11 (2020-12-07)

0.0.10 (2020-12-07)

Bug Fixes

  • remove node-fetch alias (8c83e29)

0.0.9 (2020-11-21)

Features

  • alias un to RUNTIME_DIR by default (5af101e)

0.0.8 (2020-11-21)

Features

0.0.7 (2020-11-20)

Bug Fixes

  • get original fetch from global (bd6b3a8)

0.0.6 (2020-11-20)

Features

  • node preset and improved fetch (d982833)
  • support polyfill via env and node.fetch polyfill (cd392d5)

0.0.5 (2020-11-19)

0.0.4 (2020-11-19)

Features

  • emit close and finish events and remove listeners on destroy (cc42554)
  • fetch and localFetch (19a147d)
  • use babel to transpile runtime (ts => cjs) (82dac22)

Bug Fixes

  • use cjs export for exports with alias (00f911b)
  • use cjs exports for node entries (5aaed44)

0.0.3 (2020-11-19)

Bug Fixes

  • resolve npm packages that override (c42196c)
  • use default export for node mocks (cjs compat) (83c275d)

0.0.2 (2020-11-18)

Bug Fixes

0.0.1 (2020-11-18)

Features