Modular standard library for JavaScript. Includes polyfills for ECMAScript up to 2024: promises, symbols, collections, iterators, typed arrays, many other features, ECMAScript proposals, some cross-platform WHATWG / W3C features and proposals like
URL
. You can load only required features or use it without global namespace pollution.
If you are looking for documentation for obsolete core-js@2
, please, check this branch.
core-js
isn't backed by a company, so the future of this project depends on you. Become a sponsor or a backer if you are interested in core-js
: Open Collective, Patreon, Boosty, Bitcoin ( bc1qlea7544qtsmj2rayg0lthvza9fau63ux0fstcz ), Alipay.
Example of usage:
import 'core-js/actual';
Promise.resolve(42).then(it => console.log(it)); // => 42
Array.from(new Set([1, 2, 3]).union(new Set([3, 4, 5]))); // => [1, 2, 3, 4, 5]
[1, 2].flatMap(it => [it, it]); // => [1, 1, 2, 2]
(function * (i) { while (true) yield i++; })(1)
.drop(1).take(5)
.filter(it => it % 2)
.map(it => it ** 2)
.toArray(); // => [9, 25]
structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
You can load only required features:
import 'core-js/actual/promise';
import 'core-js/actual/set';
import 'core-js/actual/iterator';
import 'core-js/actual/array/from';
import 'core-js/actual/array/flat-map';
import 'core-js/actual/structured-clone';
Promise.resolve(42).then(it => console.log(it)); // => 42
Array.from(new Set([1, 2, 3]).union(new Set([3, 4, 5]))); // => [1, 2, 3, 4, 5]
[1, 2].flatMap(it => [it, it]); // => [1, 1, 2, 2]
(function * (i) { while (true) yield i++; })(1)
.drop(1).take(5)
.filter(it => it % 2)
.map(it => it ** 2)
.toArray(); // => [9, 25]
structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
Or use it without global namespace pollution:
import Promise from 'core-js-pure/actual/promise';
import Set from 'core-js-pure/actual/set';
import Iterator from 'core-js-pure/actual/iterator';
import from from 'core-js-pure/actual/array/from';
import flatMap from 'core-js-pure/actual/array/flat-map';
import structuredClone from 'core-js-pure/actual/structured-clone';
Promise.resolve(42).then(it => console.log(it)); // => 42
from(new Set([1, 2, 3]).union(new Set([3, 4, 5]))); // => [1, 2, 3, 4, 5]
flatMap([1, 2], it => [it, it]); // => [1, 1, 2, 2]
Iterator.from(function * (i) { while (true) yield i++; }(1))
.drop(1).take(5)
.filter(it => it % 2)
.map(it => it ** 2)
.toArray(); // => [9, 25]
structuredClone(new Set([1, 2, 3])); // => new Set([1, 2, 3])
postinstall
message@babel/polyfill
@babel/preset-env
@babel/runtime
globalThis
Array.prototype.includes
Array.prototype.flat
/ Array.prototype.flatMap
Array
find from lastArray
by copyArray
groupingArrayBuffer.prototype.transfer
and friendsIterator
helpersObject.values
/ Object.entries
Object.fromEntries
Object.getOwnPropertyDescriptors
Object.prototype.hasOwnProperty
String
paddingString.prototype.matchAll
String.prototype.replaceAll
String.prototype.trimStart
/ String.prototype.trimEnd
RegExp
s
(dotAll
) flagRegExp
named capture groupsPromise.allSettled
Promise.any
Promise.prototype.finally
Promise.try
Promise.withResolvers
Symbol.asyncIterator
for asynchronous iterationSymbol.prototype.description
JSON.stringify
Set
methodsArray.fromAsync
JSON.parse
source text accessFloat16
methodsUint8Array
to / from base64 and hexRegExp
escapingMath.sumPrecise
Symbol.metadata
for decorators metadata proposalIterator
sequencingAsyncIterator
helpersIterator.range
Map
upsertArray.isTemplateObject
String.dedent
Symbol
predicatesSymbol.customMatcher
for extractorsObservable
.of
and .from
methods on collection constructorscompositeKey
and compositeSymbol
Array
filteringArray
deduplicationDataView
get / set Uint8Clamped
methodsNumber.fromString
String.cooked
String.prototype.codePoints
Symbol.customMatcher
for pattern matchingFunction.prototype.demethodize
Function.{ isCallable, isConstructor }
Reflect
metadataself
structuredClone
setTimeout
and setInterval
setImmediate
queueMicrotask
URL
and URLSearchParams
DOMException
// global version
npm install --save [email protected]
// version without global namespace pollution
npm install --save [email protected]
// bundled global version
npm install --save [email protected]
Or you can use core-js
from CDN.
postinstall
message⬆The core-js
project needs your help, so the package shows a message about it after installation. If it causes problems for you, you can disable it:
ADBLOCK=true npm install
// or
DISABLE_OPENCOLLECTIVE=true npm install
// or
npm install --loglevel silent
You can import only-required-for-you polyfills, like in the examples at the top of README.md
. Available CommonJS entry points for all polyfilled methods / constructors and namespaces. Just some examples:
// polyfill all `core-js` features, including early-stage proposals:
import "core-js";
// or:
import "core-js/full";
// polyfill all actual features - stable ES, web standards and stage 3 ES proposals:
import "core-js/actual";
// polyfill only stable features - ES and web standards:
import "core-js/stable";
// polyfill only stable ES features:
import "core-js/es";
// if you want to polyfill `Set`:
// all `Set`-related features, with early-stage ES proposals:
import "core-js/full/set";
// stable required for `Set` ES features, features from web standards and stage 3 ES proposals:
import "core-js/actual/set";
// stable required for `Set` ES features and features from web standards
// (DOM collections iterator in this case):
import "core-js/stable/set";
// only stable ES features required for `Set`:
import "core-js/es/set";
// the same without global namespace pollution:
import Set from "core-js-pure/full/set";
import Set from "core-js-pure/actual/set";
import Set from "core-js-pure/stable/set";
import Set from "core-js-pure/es/set";
// if you want to polyfill just the required methods:
import "core-js/full/set/intersection";
import "core-js/actual/array/find-last";
import "core-js/stable/queue-microtask";
import "core-js/es/array/from";
// polyfill iterator helpers proposal:
import "core-js/proposals/iterator-helpers";
// polyfill all stage 2+ proposals:
import "core-js/stage/2";
Tip
The usage of the /actual/
namespace is recommended since it includes all actual JavaScript features and does not include unstable early-stage proposals that are available mainly for experiments.
Warning
modules
path is an internal API, does not inject all required dependencies and can be changed in minor or patch releases. Use it only for a custom build and/or if you know what are you doing.core-js
with the extension of native objects, recommended to load all core-js
modules at the top of the entry point of your application, otherwise, you can have conflicts.
Symbol.iterator
, conflicting with Array.from
, URLSearchParams
and / or something else from core-js
, see related issues.core-js
.core-js
is extremely modular and uses a lot of very tiny modules, because of that for usage in browsers bundle up core-js
instead of a usage loader for each file, otherwise, you will have hundreds of requests.In the pure
version, we can't pollute prototypes of native constructors. Because of that, prototype methods transformed into static methods like in examples above. But with transpilers, we can use one more trick - bind operator and virtual methods. Special for that, available /virtual/
entry points. Example:
import fill from 'core-js-pure/actual/array/virtual/fill';
import findIndex from 'core-js-pure/actual/array/virtual/find-index';
Array(10)::fill(0).map((a, b) => b * b)::findIndex(it => it && !(it % 8)); // => 4
Warning
The bind operator is an early-stage ECMAScript proposal and usage of this syntax can be dangerous.
core-js
is integrated with babel
and is the base for polyfilling-related babel
features:
@babel/polyfill
⬆@babel/polyfill
IS just the import of stable core-js
features and regenerator-runtime
for generators and async functions, so if you load @babel/polyfill
- you load the global version of core-js
without ES proposals.
Now it's deprecated in favor of separate inclusion of required parts of core-js
and regenerator-runtime
and, for preventing breaking changes, left on core-js@2
.
As a full equal of @babel/polyfill
, you can use this:
import 'core-js/stable';
import 'regenerator-runtime/runtime';
@babel/preset-env
⬆@babel/preset-env
has useBuiltIns
option, which optimizes working with the global version of core-js
. With useBuiltIns
option, you should also set corejs
option to the used version of core-js
, like corejs: '3.39'
.
Important
Recommended to specify used minor core-js
version, like corejs: '3.39'
, instead of corejs: 3
, since with corejs: 3
will not be injected modules which were added in minor core-js
releases.
useBuiltIns: 'entry'
replaces imports of core-js
to import only required for a target environment modules. So, for example,import 'core-js/stable';
with chrome 71
target will be replaced just to:
import 'core-js/modules/es.array.unscopables.flat';
import 'core-js/modules/es.array.unscopables.flat-map';
import 'core-js/modules/es.object.from-entries';
import 'core-js/modules/web.immediate';
It works for all entry points of global version of core-js
and their combinations, for example for
import 'core-js/es';
import 'core-js/proposals/set-methods';
import 'core-js/full/set/map';
with chrome 71
target you will have as the result:
import 'core-js/modules/es.array.unscopables.flat';
import 'core-js/modules/es.array.unscopables.flat-map';
import 'core-js/modules/es.object.from-entries';
import 'core-js/modules/esnext.set.difference';
import 'core-js/modules/esnext.set.intersection';
import 'core-js/modules/esnext.set.is-disjoint-from';
import 'core-js/modules/esnext.set.is-subset-of';
import 'core-js/modules/esnext.set.is-superset-of';
import 'core-js/modules/esnext.set.map';
import 'core-js/modules/esnext.set.symmetric-difference';
import 'core-js/modules/esnext.set.union';
useBuiltIns: 'usage'
adds to the top of each file import of polyfills for features used in this file and not supported by target environments, so for:// first file:
let set = new Set([1, 2, 3]);
// second file:
let array = Array.of(1, 2, 3);
if the target contains an old environment like IE 11
we will have something like:
// first file:
import 'core-js/modules/es.array.iterator';
import 'core-js/modules/es.object.to-string';
import 'core-js/modules/es.set';
var set = new Set([1, 2, 3]);
// second file:
import 'core-js/modules/es.array.of';
var array = Array.of(1, 2, 3);
By default, @babel/preset-env
with useBuiltIns: 'usage'
option only polyfills stable features, but you can enable polyfilling of proposals by the proposals
option, as corejs: { version: '3.39', proposals: true }
.
Important
In the case of useBuiltIns: 'usage'
, you should not add core-js
imports by yourself, they will be added automatically.
@babel/runtime
⬆@babel/runtime
with corejs: 3
option simplifies work with the core-js-pure
. It automatically replaces the usage of modern features from the JS standard library to imports from the version of core-js
without global namespace pollution, so instead of:
import from from 'core-js-pure/stable/array/from';
import flat from 'core-js-pure/stable/array/flat';
import Set from 'core-js-pure/stable/set';
import Promise from 'core-js-pure/stable/promise';
from(new Set([1, 2, 3, 2, 1]));
flat([1, [2, 3], [4, [5]]], 2);
Promise.resolve(32).then(x => console.log(x));
you can write just:
Array.from(new Set([1, 2, 3, 2, 1]));
[1, [2, 3], [4, [5]]].flat(2);
Promise.resolve(32).then(x => console.log(x));
By default, @babel/runtime
only polyfills stable features, but like in @babel/preset-env
, you can enable polyfilling of proposals by proposals
option, as corejs: { version: 3, proposals: true }
.
Warning
If you use @babel/preset-env
and @babel/runtime
together, use corejs
option only in one place since it's duplicate functionality and will cause conflicts.
Fast JavaScript transpiler swc
contains integration with core-js
, that optimizes work with the global version of core-js
. Like @babel/preset-env
, it has 2 modes: usage
and entry
, but usage
mode still works not so well as in babel
. Example of configuration in .swcrc
:
{
"env": {
"targets": "> 0.25%, not dead",
"mode": "entry",
"coreJs": "3.39"
}
}
By default, core-js
sets polyfills only when they are required. That means that core-js
checks if a feature is available and works correctly or not and if it has no problems, core-js
uses native implementation.
But sometimes core-js
feature detection could be too strict for your case. For example, Promise
constructor requires the support of unhandled rejection tracking and @@species
.
Sometimes we could have an inverse problem - a knowingly broken environment with problems not covered by core-js
feature detection.
For those cases, we could redefine this behavior for certain polyfills:
const configurator = require('core-js/configurator');
configurator({
useNative: ['Promise'], // polyfills will be used only if natives are completely unavailable
usePolyfill: ['Array.from', 'String.prototype.padEnd'], // polyfills will be used anyway
useFeatureDetection: ['Map', 'Set'], // default behavior
});
require('core-js/actual');
It does not work with some features. Also, if you change the default behavior, even core-js
internals may not work correctly.
For some cases could be useful to exclude some core-js
features or generate a polyfill for target engines. You could use core-js-builder
package for that.
core-js
tries to support all possible JS engines and environments with ES3 support. Some features have a higher lower bar - for example, some accessors can properly work only from ES5, promises require a way to set a microtask or a task, etc.
However, I have no possibility to test core-js
absolutely everywhere - for example, testing in IE7- and some other ancient was stopped. The list of definitely supported engines you can see in the compatibility table by the link below. Write if you have issues or questions with the support of any engine.
core-js
project provides (as core-js-compat
package) all required data about the necessity of core-js
modules, entry points, and tools for work with it - it's useful for integration with tools like babel
or swc
. If you wanna help, you could take a look at the related section of CONTRIBUTING.md
. The visualization of compatibility data and the browser tests runner is available here, the example:
CommonJS entry points:
core-js(-pure)
CommonJS entry points:
core-js(-pure)/es
Modules es.object.assign
, es.object.create
, es.object.define-getter
, es.object.define-property
, es.object.define-properties
, es.object.define-setter
, es.object.entries
, es.object.freeze
, es.object.from-entries
, es.object.get-own-property-descriptor
, es.object.get-own-property-descriptors
, es.object.get-own-property-names
, es.object.get-prototype-of
, es.object.group-by
, es.object.has-own
, es.object.is
, es.object.is-extensible
, es.object.is-frozen
, es.object.is-sealed
, es.object.keys
, es.object.lookup-setter
, es.object.lookup-getter
, es.object.prevent-extensions
, es.object.proto
, es.object.to-string
, es.object.seal
, es.object.set-prototype-of
, es.object.values
.
class Object {
toString(): string; // ES2015+ fix: @@toStringTag support
__defineGetter__(property: PropertyKey, getter: Function): void;
__defineSetter__(property: PropertyKey, setter: Function): void;
__lookupGetter__(property: PropertyKey): Function | void;
__lookupSetter__(property: PropertyKey): Function | void;
__proto__: Object | null; // required a way setting of prototype - will not in IE10-, it's for modern engines like Deno
static assign(target: Object, ...sources: Array<Object>): Object;
static create(prototype: Object | null, properties?: { [property: PropertyKey]: PropertyDescriptor }): Object;
static defineProperties(object: Object, properties: { [property: PropertyKey]: PropertyDescriptor })):