How do I install the babel-polyfill library?

Shlomi picture Shlomi · Jun 29, 2015 · Viewed 131.3k times · Source

I just started to use Babel to compile my ES6 javascript code into ES5. When I start to use Promises it looks like it's not working. The Babel website states support for promises via polyfills.

Without any luck, I tried to add:

require("babel/polyfill");

or

import * as p from "babel/polyfill";

With that I'll get the following error on my app bootstrapping:

Cannot find module 'babel/polyfill'

I searched for the module but it seems I'm missing some fundamental thing here. I also tried to add the old and good bluebird NPM but it looks like it's not working.

How to use the polyfills from Babel?

Answer

vdclouis picture vdclouis · Nov 6, 2015

This changed a bit in babel v6.

From the docs:

The polyfill will emulate a full ES6 environment. This polyfill is automatically loaded when using babel-node.

Installation:
$ npm install babel-polyfill

Usage in Node / Browserify / Webpack:
To include the polyfill you need to require it at the top of the entry point to your application.
require("babel-polyfill");

Usage in Browser:
Available from the dist/polyfill.js file within a babel-polyfill npm release. This needs to be included before all your compiled Babel code. You can either prepend it to your compiled code or include it in a <script> before it.

NOTE: Do not require this via browserify etc, use babel-polyfill.