`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.
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 npmnpm i -D unenv# Using yarnyarn add --dev unenv# Using pnpmpnpm 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 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.