Socket
Socket
Sign inDemoInstall

unenv

Package Overview
Dependencies
5
Maintainers
1
Versions
57
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    unenv

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


Version published
Maintainers
1
Install size
1.25 MB
Created

Changelog

Source

v1.5.1

compare changes

🩹 Fixes

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

❤️ Contributors

Readme

Source

unenv

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

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 (and nodeless), unenv will provide an abstract configuration that can be used in building pipelines (rollup.js, webpack, etc.).

import { env, node, nodeless } from "unenv";

const { alias, inject, polyfill, external } = env(...presets);

Presets

node

Suitable to convert universal libraries working in Node.js. (preset)

  • Add supports for global fetch API
  • Set Node.js built-ins as externals

nodeless

Using this preset, we can convert a code that is depending on Node.js to work anywhere else.

Built-in Node.js modules

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

ModuleStatusSource
node:assertMocked-
node:async_hooksPolyfilledunenv/node/async_hooks
node:bufferPolyfilledunenv/node/buffer
node:child_processMocked-
node:clusterMocked-
node:consoleMocked-
node:constantsMocked-
node:cryptoPolyfilledunenv/node/crypto
node:dgramMocked-
node:diagnostics_channelMocked-
node:dnsMocked-
node:domainMocked-
node:eventsPolyfilledunenv/node/events
node:fsPolyfilledunenv/node/fs
node:fs/promisesPolyfilledunenv/node/fs/promises
node:http2Mocked-
node:httpPolyfilledunenv/node/http
node:httpsMocked-
node:inspectorMocked-
node:moduleMocked-
node:netPolyfilledunenv/node/net
node:osMocked-
node:pathPolyfilledunenv/node/path
node:perf_hooksMocked-
node:processPolyfilledunenv/node/process
node:punycodeMocked-
node:querystringMocked-
node:readlineMocked-
node:replMocked-
node:streamPolyfilledunenv/node/stream
node:stream/consumersMockedunenv/node/stream/consumers
node:stream/promisesMockedunenv/node/stream/promises
node:stream/webNativeunenv/node/stream/web
node:string_decoderMocked-
node:sysMocked-
node:timersMocked-
node:timers/promisesMocked-
node:tlsMocked-
node:trace_eventsMocked-
node:ttyMocked-
node:urlPolyfilledunenv/node/url
node:utilPolyfilledunenv/node/util
node:util/typesPolyfilledunenv/node/util/types
node:v8Mocked-
node:vmMocked-
node:wasiMocked-
node:worker_threadsMocked-
node:zlibMocked-

npm packages

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

PackageStatusSource
npm/consolaUse native consoleunenv/runtime/npm/consola
npm/cross-fetchUse native fetchunenv/runtime/npm/cross-fetch
npm/debugMocked with console.debugunenv/runtime/npm/debug
npm/fseventsMockedunenv/runtime/npm/fsevents
npm/inheritsInlinedunenv/runtime/npm/inherits
npm/mime-dbMinimizedunenv/runtime/npm/mime-db
npm/mimeMinimizedunenv/runtime/npm/mime
npm/node-fetchUse native fetchunenv/runtime/npm/node-fetch
npm/whatwg-urlUse native URLunenv/runtime/npm/whatwg-url

Auto-mocking proxy

import MockProxy from "unenv/runtime/mock/proxy";

console.log(MockProxy().foo.bar()[0]);

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("os");
if (os.platform() === "windows") {
  /* do some fix */
}
module.exports = () => "Hello world";

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

Other polyfills

Please check ./src/runtime to discover other polyfills.

License

MIT

FAQs

Last updated on 24 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc