babel polyfill being included, but forEach still doesn't work in IE11 on NodeLists












3















I've got Webpack working with Babel and including the @babel/polyfill, yet IE11 is still throwing a SCRIPT438 error when trying to use .forEach on a NodeList.



Here's my package.json



{
...
"scripts": {
"build:js": "webpack --config ./_build/webpack.config.js"
},
...
"browserslist": [
"IE 11",
"last 3 versions",
"not IE < 11"
],
"babel": {
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage"
}
]
]
},
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"babel-loader": "^8.0.4",
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2"
},
"dependencies": {
"@babel/polyfill": "^7.0.0"
}
}


My webpack.config.js:



const path = require('path');
const webpack = require('webpack');

module.exports = (env, argv) => {

const javascript = {
test: /.js$/,
use: {
loader: 'babel-loader'
}
};

// config object
const config = {
entry: {
main: './_src/js/main.js',
},
devtool: 'source-map',
output: {
path: path.resolve(__dirname, '../js'),
filename: '[name].js',
},
module: {
rules: [javascript]
}
}

return config;
}


And finally /_src/main.js that I'm running through webpack and babel:



const testList = document.querySelectorAll('.test-list li');

testList.forEach(item => {
console.log(item.innerHTML);
})


The docs at https://babeljs.io/docs/en/babel-polyfill say that you don't need to import or require polyfill when loading it via Webpack with useBuiltIns: "usage". But even if I remove that option and manually import the whole polyfill at the top of main.js (making my bundle huge), it still errors out in IE11.



So...what am I doing wrong?










share|improve this question

























  • Babel doesn't polyfill that.

    – connexo
    Nov 16 '18 at 6:46











  • Possible duplicate of IE Doesn't support forEach even with Polyfill.

    – connexo
    Nov 16 '18 at 6:51
















3















I've got Webpack working with Babel and including the @babel/polyfill, yet IE11 is still throwing a SCRIPT438 error when trying to use .forEach on a NodeList.



Here's my package.json



{
...
"scripts": {
"build:js": "webpack --config ./_build/webpack.config.js"
},
...
"browserslist": [
"IE 11",
"last 3 versions",
"not IE < 11"
],
"babel": {
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage"
}
]
]
},
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"babel-loader": "^8.0.4",
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2"
},
"dependencies": {
"@babel/polyfill": "^7.0.0"
}
}


My webpack.config.js:



const path = require('path');
const webpack = require('webpack');

module.exports = (env, argv) => {

const javascript = {
test: /.js$/,
use: {
loader: 'babel-loader'
}
};

// config object
const config = {
entry: {
main: './_src/js/main.js',
},
devtool: 'source-map',
output: {
path: path.resolve(__dirname, '../js'),
filename: '[name].js',
},
module: {
rules: [javascript]
}
}

return config;
}


And finally /_src/main.js that I'm running through webpack and babel:



const testList = document.querySelectorAll('.test-list li');

testList.forEach(item => {
console.log(item.innerHTML);
})


The docs at https://babeljs.io/docs/en/babel-polyfill say that you don't need to import or require polyfill when loading it via Webpack with useBuiltIns: "usage". But even if I remove that option and manually import the whole polyfill at the top of main.js (making my bundle huge), it still errors out in IE11.



So...what am I doing wrong?










share|improve this question

























  • Babel doesn't polyfill that.

    – connexo
    Nov 16 '18 at 6:46











  • Possible duplicate of IE Doesn't support forEach even with Polyfill.

    – connexo
    Nov 16 '18 at 6:51














3












3








3








I've got Webpack working with Babel and including the @babel/polyfill, yet IE11 is still throwing a SCRIPT438 error when trying to use .forEach on a NodeList.



Here's my package.json



{
...
"scripts": {
"build:js": "webpack --config ./_build/webpack.config.js"
},
...
"browserslist": [
"IE 11",
"last 3 versions",
"not IE < 11"
],
"babel": {
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage"
}
]
]
},
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"babel-loader": "^8.0.4",
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2"
},
"dependencies": {
"@babel/polyfill": "^7.0.0"
}
}


My webpack.config.js:



const path = require('path');
const webpack = require('webpack');

module.exports = (env, argv) => {

const javascript = {
test: /.js$/,
use: {
loader: 'babel-loader'
}
};

// config object
const config = {
entry: {
main: './_src/js/main.js',
},
devtool: 'source-map',
output: {
path: path.resolve(__dirname, '../js'),
filename: '[name].js',
},
module: {
rules: [javascript]
}
}

return config;
}


And finally /_src/main.js that I'm running through webpack and babel:



const testList = document.querySelectorAll('.test-list li');

testList.forEach(item => {
console.log(item.innerHTML);
})


The docs at https://babeljs.io/docs/en/babel-polyfill say that you don't need to import or require polyfill when loading it via Webpack with useBuiltIns: "usage". But even if I remove that option and manually import the whole polyfill at the top of main.js (making my bundle huge), it still errors out in IE11.



So...what am I doing wrong?










share|improve this question
















I've got Webpack working with Babel and including the @babel/polyfill, yet IE11 is still throwing a SCRIPT438 error when trying to use .forEach on a NodeList.



Here's my package.json



{
...
"scripts": {
"build:js": "webpack --config ./_build/webpack.config.js"
},
...
"browserslist": [
"IE 11",
"last 3 versions",
"not IE < 11"
],
"babel": {
"presets": [
[
"@babel/preset-env",
{
"useBuiltIns": "usage"
}
]
]
},
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"babel-loader": "^8.0.4",
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2"
},
"dependencies": {
"@babel/polyfill": "^7.0.0"
}
}


My webpack.config.js:



const path = require('path');
const webpack = require('webpack');

module.exports = (env, argv) => {

const javascript = {
test: /.js$/,
use: {
loader: 'babel-loader'
}
};

// config object
const config = {
entry: {
main: './_src/js/main.js',
},
devtool: 'source-map',
output: {
path: path.resolve(__dirname, '../js'),
filename: '[name].js',
},
module: {
rules: [javascript]
}
}

return config;
}


And finally /_src/main.js that I'm running through webpack and babel:



const testList = document.querySelectorAll('.test-list li');

testList.forEach(item => {
console.log(item.innerHTML);
})


The docs at https://babeljs.io/docs/en/babel-polyfill say that you don't need to import or require polyfill when loading it via Webpack with useBuiltIns: "usage". But even if I remove that option and manually import the whole polyfill at the top of main.js (making my bundle huge), it still errors out in IE11.



So...what am I doing wrong?







javascript webpack babel npm-scripts babel-polyfill






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 7:22









connexo

23k83762




23k83762










asked Nov 16 '18 at 3:52









Robert_QSSRobert_QSS

4817




4817













  • Babel doesn't polyfill that.

    – connexo
    Nov 16 '18 at 6:46











  • Possible duplicate of IE Doesn't support forEach even with Polyfill.

    – connexo
    Nov 16 '18 at 6:51



















  • Babel doesn't polyfill that.

    – connexo
    Nov 16 '18 at 6:46











  • Possible duplicate of IE Doesn't support forEach even with Polyfill.

    – connexo
    Nov 16 '18 at 6:51

















Babel doesn't polyfill that.

– connexo
Nov 16 '18 at 6:46





Babel doesn't polyfill that.

– connexo
Nov 16 '18 at 6:46













Possible duplicate of IE Doesn't support forEach even with Polyfill.

– connexo
Nov 16 '18 at 6:51





Possible duplicate of IE Doesn't support forEach even with Polyfill.

– connexo
Nov 16 '18 at 6:51












1 Answer
1






active

oldest

votes


















10














babel-polyfill doesn't polyfill missing web API/prototype methods like NodeList.prototype.forEach.



Also please note that your question title is misleading as NodeList.prototype.forEach is not an ES6 feature. forEach on iterable collections is currently only a candidate recommendation (as of August 2018).



Simply include your own polyfill at the top level of your Javascript:



if (window.NodeList && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = Array.prototype.forEach;
}


This might change as soon as core-js 3 is stable: https://github.com/zloirock/core-js/issues/329



You can also go without any polyfill if you start to adopt the common pattern being used in ES6 times:



const testList = [...document.querySelectorAll('.test-list li')];


or



const testList = Array.from(document.querySelectorAll('.test-list li'));


The other option you have is to use for...of instead:



const lis = document.querySelectorAll('.test-list li');
for (const li of lis) {
// li.addEventListener(...) or whatever
}


Finally, you can also adopt the common ES5 pattern:



var testList = document.querySelectorAll('.test-list li');
Array.prototype.forEach.call(testList, function(li) { /*whatever*/ });





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331180%2fbabel-polyfill-being-included-but-foreach-still-doesnt-work-in-ie11-on-nodelis%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    10














    babel-polyfill doesn't polyfill missing web API/prototype methods like NodeList.prototype.forEach.



    Also please note that your question title is misleading as NodeList.prototype.forEach is not an ES6 feature. forEach on iterable collections is currently only a candidate recommendation (as of August 2018).



    Simply include your own polyfill at the top level of your Javascript:



    if (window.NodeList && !NodeList.prototype.forEach) {
    NodeList.prototype.forEach = Array.prototype.forEach;
    }


    This might change as soon as core-js 3 is stable: https://github.com/zloirock/core-js/issues/329



    You can also go without any polyfill if you start to adopt the common pattern being used in ES6 times:



    const testList = [...document.querySelectorAll('.test-list li')];


    or



    const testList = Array.from(document.querySelectorAll('.test-list li'));


    The other option you have is to use for...of instead:



    const lis = document.querySelectorAll('.test-list li');
    for (const li of lis) {
    // li.addEventListener(...) or whatever
    }


    Finally, you can also adopt the common ES5 pattern:



    var testList = document.querySelectorAll('.test-list li');
    Array.prototype.forEach.call(testList, function(li) { /*whatever*/ });





    share|improve this answer






























      10














      babel-polyfill doesn't polyfill missing web API/prototype methods like NodeList.prototype.forEach.



      Also please note that your question title is misleading as NodeList.prototype.forEach is not an ES6 feature. forEach on iterable collections is currently only a candidate recommendation (as of August 2018).



      Simply include your own polyfill at the top level of your Javascript:



      if (window.NodeList && !NodeList.prototype.forEach) {
      NodeList.prototype.forEach = Array.prototype.forEach;
      }


      This might change as soon as core-js 3 is stable: https://github.com/zloirock/core-js/issues/329



      You can also go without any polyfill if you start to adopt the common pattern being used in ES6 times:



      const testList = [...document.querySelectorAll('.test-list li')];


      or



      const testList = Array.from(document.querySelectorAll('.test-list li'));


      The other option you have is to use for...of instead:



      const lis = document.querySelectorAll('.test-list li');
      for (const li of lis) {
      // li.addEventListener(...) or whatever
      }


      Finally, you can also adopt the common ES5 pattern:



      var testList = document.querySelectorAll('.test-list li');
      Array.prototype.forEach.call(testList, function(li) { /*whatever*/ });





      share|improve this answer




























        10












        10








        10







        babel-polyfill doesn't polyfill missing web API/prototype methods like NodeList.prototype.forEach.



        Also please note that your question title is misleading as NodeList.prototype.forEach is not an ES6 feature. forEach on iterable collections is currently only a candidate recommendation (as of August 2018).



        Simply include your own polyfill at the top level of your Javascript:



        if (window.NodeList && !NodeList.prototype.forEach) {
        NodeList.prototype.forEach = Array.prototype.forEach;
        }


        This might change as soon as core-js 3 is stable: https://github.com/zloirock/core-js/issues/329



        You can also go without any polyfill if you start to adopt the common pattern being used in ES6 times:



        const testList = [...document.querySelectorAll('.test-list li')];


        or



        const testList = Array.from(document.querySelectorAll('.test-list li'));


        The other option you have is to use for...of instead:



        const lis = document.querySelectorAll('.test-list li');
        for (const li of lis) {
        // li.addEventListener(...) or whatever
        }


        Finally, you can also adopt the common ES5 pattern:



        var testList = document.querySelectorAll('.test-list li');
        Array.prototype.forEach.call(testList, function(li) { /*whatever*/ });





        share|improve this answer















        babel-polyfill doesn't polyfill missing web API/prototype methods like NodeList.prototype.forEach.



        Also please note that your question title is misleading as NodeList.prototype.forEach is not an ES6 feature. forEach on iterable collections is currently only a candidate recommendation (as of August 2018).



        Simply include your own polyfill at the top level of your Javascript:



        if (window.NodeList && !NodeList.prototype.forEach) {
        NodeList.prototype.forEach = Array.prototype.forEach;
        }


        This might change as soon as core-js 3 is stable: https://github.com/zloirock/core-js/issues/329



        You can also go without any polyfill if you start to adopt the common pattern being used in ES6 times:



        const testList = [...document.querySelectorAll('.test-list li')];


        or



        const testList = Array.from(document.querySelectorAll('.test-list li'));


        The other option you have is to use for...of instead:



        const lis = document.querySelectorAll('.test-list li');
        for (const li of lis) {
        // li.addEventListener(...) or whatever
        }


        Finally, you can also adopt the common ES5 pattern:



        var testList = document.querySelectorAll('.test-list li');
        Array.prototype.forEach.call(testList, function(li) { /*whatever*/ });






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 3 '18 at 16:41

























        answered Nov 16 '18 at 6:49









        connexoconnexo

        23k83762




        23k83762
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331180%2fbabel-polyfill-being-included-but-foreach-still-doesnt-work-in-ie11-on-nodelis%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."