Babel-loader: babel-polyfill - error Error: only one instance of babel-polyfill is allowed

Created on 1 Mar 2017  路  24Comments  路  Source: babel/babel-loader

I'm submitting a bug report

Webpack Version:
1.14.0

Babel Core Version:
6.23.1

Babel Loader Version:
6.3.2

Please tell us about your environment:
Windows 10

Current behavior:
I try to use babel-polyfill for ie11 with webpack

entry: {
    app: ['babel-polyfill','./src/main.js']
}

After ie11 reconize Array.includes but in some pages i have error in other browsers like Firefox
error Error: only one instance of babel-polyfill is allowed

Most helpful comment

There was another 3rd party library which included the babel-polyfill in their bundle. To fix this I had to remove the babel-polyfill from the entry in webpack.config and add a conditional require to the top of my index.js which is the entry point.

Before webpack.config:

module.exports = {
    entry : [
        'babel-polyfill', './src/index'
    ]
}

After webpack.config:

module.exports = {
    entry : [
        './src/index'
    ]
}

Added to the top of ./src/index.js:

if (!global._babelPolyfill) {
    require('babel-polyfill');
}

Now everything works without issue.

All 24 comments

Have you imported with ./src/main.js already?

yes but i have the same message

Please you exclude babel-polyfill from your codes or exclude from webpack.entry.
There can only be one instance of babel-polyfill.

as @abouthiroppy already said, you either load the app bundle twice or you import babel-polyfill somewhere else in your code. Ensure it is only used once.

I only included it once but i have always this message. But not every time, just on some components.
It must be my fault

I came across this error and found that I was loading my javascript inside the body of my application instead of the head.

I came across this error and found that I was loading my javascript inside the body of my application instead of the head.

Really, so moving into <head> fixed it? That seems strange..

I had a similar problem using webpacker. I was loading two javascript apps on the same page using the javascript_pack_tag. Each one loads an instance of babel-polyfill. I was able to resolve the issue by merging both apps and using a single loading point.

I close my issue, think you for yours answers

I was also seeing this error, but after checking my html, I was also including my bundled JS twice. Fixed by updating my HTML to ensure I only include my bundle once

There was another 3rd party library which included the babel-polyfill in their bundle. To fix this I had to remove the babel-polyfill from the entry in webpack.config and add a conditional require to the top of my index.js which is the entry point.

Before webpack.config:

module.exports = {
    entry : [
        'babel-polyfill', './src/index'
    ]
}

After webpack.config:

module.exports = {
    entry : [
        './src/index'
    ]
}

Added to the top of ./src/index.js:

if (!global._babelPolyfill) {
    require('babel-polyfill');
}

Now everything works without issue.

I had a Chrome extension that was using babel-polyfill, which was causing this error. Took forever to track that down!

Another way to fix this is by setting global._babelPolyfill = false; at the top of your index.js file.

@lipemat I did the first part of what you said for a different project (https://github.com/richardtallent/vue-simple-calendar/tree/2.2.1) but without the if(!global.etc).. part added and it worked fine. However that may be the requirements of that package in this case.

Idempotent Babel Polyfill can be imported multiple times

npm install --save idempotent-babel-polyfill
import 'idempotent-babel-polyfill';

Excuse me!
I would like to know where to put import 'idempotent-babel-polyfill';?
In my index.js or in webpack.config.js?

You should replace every "babel-polyfill" (either in imports, or in your webpack config's entry option) with "idempotent-babel-polyfill"

When you need to actually import 'babel-polyfill' instead of require it, you can achieve @lipemat's solution by adding a new file require-babel-polyfill.js with following contents:

export default (() => {
  if (!global || !global._babelPolyfill) {
    require('babel-polyfill')
  }
})()

and import that file in at the top of your apps entry point (index.js):

import './require-babel-polyfill.js'

@visualjerk, aka idempotent-babel-polyfill

@codejamninja Yeah, seems like your module is achieving the same. I actually did not try it out, because the solution was pretty simple.

Thanks for putting this on npm, making it even easier to use.

@visualjerk, it also works in the browser, because babel-polyfill attaches to the window object in a browser.

@lipemat this approach is the good way

@swarajban thanks man it helped me too

thanks

Was this page helpful?
0 / 5 - 0 ratings