ext: JavaScript Utilities and Polyfills
The `ext` package, formerly known as `es5-ext`, is a comprehensive JavaScript utility library that provides a collection of non-standard or soon-to-be-standard language extensions and polyfills. It differentiates itself by offering these utilities in a future-proof and non-invasive manner, meaning it generally avoids polluting global prototypes without explicit action, and is designed to be compatible with ES3+ environments without requiring a transpilation step. The library is highly modular, allowing developers to import only the specific functions or objects they need, such as `globalThis`, `Object.clear`, or `String.prototype.camelToHyphen` functionality. While the package has seen recent maintenance releases (e.g., `v0.10.64` in February 2024) addressing stability issues, the latest feature-rich version is `v1.7.0`, released in August 2022. Its release cadence combines frequent bug fixes with less frequent, but impactful, feature additions, ensuring both stability and progressive enhancement capabilities.
Common errors
-
Command failed with exit code 1
cause The `postinstall` script, which runs after `npm install`, encountered an error due to environmental factors, shell compatibility, or permissions issues on Windows, Powershell, or certain Linux terminals.fixUpdate Node.js/npm. If the error persists, install with `npm install ext --ignore-scripts`. This bypasses the script but might require manual setup if it performs critical configuration. Consult package GitHub issues for specific workarounds. -
TypeError: Cannot read property 'match' of null (or similar regex errors) when using function#toStringTokens()
cause Older versions of the library relied on problematic regex patterns, particularly affecting methods that parse function definitions, leading to failures with ES2015+ syntax.fixUpdate the `ext` package to at least `v0.10.63` (or the latest `1.x` series) which includes bug fixes for improved regex robustness and better support for modern JavaScript function definitions. -
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'ext' imported from ...
cause This error typically occurs when trying to `import` the package using an incorrect path or if the environment is configured for CommonJS while `ext` is being treated as an ESM module, or vice-versa, specifically for subpath imports.fixVerify the exact import path for the specific utility you are trying to use (e.g., `import clear from 'ext/object/clear';`). Ensure your project's module resolution (`package.json#type`) aligns with how you are importing modules.
Warnings
- breaking The package was renamed from `es5-ext` to `ext`. Users migrating from `es5-ext` will need to update all import paths (e.g., `require('es5-ext/object/clear')` becomes `import objectClear from 'ext/object/clear';`). While `es5-ext` still receives maintenance, `ext` is the officially supported name.
- gotcha The package's `postinstall` script has historically caused issues on various operating systems (Windows, specific Linux terminals, Powershell), potentially leading to installation failures or errors. While recent `0.10.x` releases have focused on patching these, environmental-specific problems may still arise.
- gotcha The library primarily promotes granular ESM imports (e.g., `import util from 'ext/path/to/util';`). Attempting to use CommonJS `require()` syntax with modern `ext` versions in an ESM-configured project, or assuming a single top-level default export for multiple utilities, will lead to module resolution errors.
Install
-
npm install ext -
yarn add ext -
pnpm add ext
Imports
- globalThis
const globalThis = require('ext/global-this');import globalThis from 'ext/global-this';
- objectClear
import { clear } from 'ext/object'; // Incorrect path/named import import objectClear from 'ext/object/clear.js'; // .js extension is usually not neededimport objectClear from 'ext/object/clear';
- camelToHyphen
import { camelToHyphen } from 'ext/string'; // Incorrect, it's a subpath module import 'ext/string/#/camel-to-hyphen'; // Old `es5-ext` path or direct prototype extension side-effectimport camelToHyphen from 'ext/string/camel-to-hyphen';
Quickstart
import globalThis from 'ext/global-this';
import objectClear from 'ext/object/clear';
import camelToHyphen from 'ext/string/camel-to-hyphen';
import identity from 'ext/function/identity';
// Access globalThis reliably across environments
console.log('Is this browser globalThis?', typeof window !== 'undefined' && globalThis === window);
console.log('Is this Node globalThis?', typeof global !== 'undefined' && globalThis === global);
// Clear an object's properties
const data = { a: 1, b: 'two', c: true };
console.log('Original object:', data); // { a: 1, b: 'two', c: true }
objectClear(data);
console.log('Object after clear:', data); // {}
// Convert camelCase to hyphen-case
const camelCaseString = 'myCamelCaseString';
const hyphenatedString = camelToHyphen.call(camelCaseString);
console.log(`'${camelCaseString}' hyphenated: '${hyphenatedString}'`); // 'my-camel-case-string'
// Use a simple identity function
const value = 42;
const transformedValue = identity(value);
console.log(`Identity of ${value} is ${transformedValue}`); // 'Identity of 42 is 42'