
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
The 'unenv' npm package is designed to provide a universal environment for JavaScript and TypeScript projects. It allows developers to create isomorphic code that can run seamlessly in both Node.js and browser environments by providing polyfills and shims for various APIs.
Polyfills for Node.js APIs
This feature provides polyfills for Node.js APIs, allowing you to use Node.js-specific features in a browser environment. The code sample demonstrates how to use the Buffer API from Node.js in a universal environment.
const { Buffer } = require('unenv/runtime/polyfills/buffer');
const buffer = Buffer.from('Hello, world!');
console.log(buffer.toString('utf-8'));
Environment Detection
This feature allows you to detect the current environment (Node.js or browser) and execute code conditionally based on that. The code sample shows how to check if the code is running in Node.js or a browser.
const { isNode, isBrowser } = require('unenv');
if (isNode) {
console.log('Running in Node.js');
} else if (isBrowser) {
console.log('Running in the browser');
}
Universal Fetch API
This feature provides a universal Fetch API that works in both Node.js and browser environments. The code sample demonstrates how to make a fetch request and handle the response.
const fetch = require('unenv/runtime/polyfills/fetch');
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => console.log(data));
The 'isomorphic-fetch' package provides a Fetch API implementation that works in both Node.js and browser environments. It is similar to the Fetch API polyfill provided by 'unenv', but it focuses solely on fetch functionality.
The 'universal-env' package helps in detecting the current environment (Node.js or browser) and provides utility functions for environment-specific code. It is similar to the environment detection feature of 'unenv'.
The 'node-polyfill-webpack-plugin' package provides polyfills for Node.js core modules in a Webpack environment. It is similar to the polyfills provided by 'unenv', but it is specifically designed for use with Webpack.
[!NOTE] You are on the development (v2) branch. Check out v1 for the current release.
unenv provides polyfills to add Node.js compatibility for any JavaScript runtime, including browsers and edge workers.
The defineEnv
utility can generate a target environment configuration.
import { defineEnv } from "unenv";
const { env } = defineEnv({
nodeCompat: true,
npmShims: true,
resolve: true,
overrides: {},
presets: [],
});
const { alias, inject, external, polyfill } = env;
You can then integrate the env object with your build tool:
Bundler | alias | inject | external |
---|---|---|---|
rollup | @rollup/plugin-alias | @rollup/plugin-inject | external |
rolldown | resolve.alias | inject | external |
vite | resolve.alias | @rollup/plugin-inject | ssr.external |
esbuild | alias | inject | external |
rspack | resolve.alias | - | externals |
webpack | resolve.alias | webpack-plugin-inject | externals |
nodeCompat
: (default: true
)
alias
entries for Node.js builtins as <id>
and node:<id>
.inject
entries for Node.js globals global
, Buffer
, and process
.npmShims
: (default: false
)
alias
entries to replace npm packages like node-fetch
with lighter shims.resolve
: (default: false
) Resolve config values to absolute paths.overrides
: Additional overrides for env config.presets
: Additional presets (for example @cloudflare/unenv-preset
).unenv/
polyfillsYou can also directly import unenv/
polyfills:
Polyfills | Description | Source |
---|---|---|
unenv/mock/* | Mocking utils | src/runtime/mock |
unenv/node/* | APIs compatible with Node.js API | src/runtime/node |
unenv/npm/* | NPM package shims | src/runtime/npm |
unenv/polyfill/* | Global polyfills | src/runtime/polyfill |
unenv/web/* | Subset of Web APIs | src/runtime/web |
unenv
replaces Node.js built-in modules compatible with any runtime (view source).
// Magic proxy to replace any unknown API
import MockProxy from "unenv/mock/proxy";
// You can also create named mocks
const lib = MockProxy.__createMock__("lib", {
/* overrides */
});
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
v2.0.0-rc.13
readline/promise
(#482)globalThis.performance
before reexporting it (#483)FAQs
`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.
The npm package unenv receives a total of 1,355,312 weekly downloads. As such, unenv popularity was classified as popular.
We found that unenv demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.