How do I install the babel-polyfill library?

后端 未结 7 1338
旧时难觅i
旧时难觅i 2020-12-02 06:13

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 pr

7条回答
  •  一生所求
    2020-12-02 07:03

    First off, the obvious answer that no one has provided, you need to install Babel into your application:

    npm install babel --save
    

    (or babel-core if you instead want to require('babel-core/polyfill')).

    Aside from that, I have a grunt task to transpile my es6 and jsx as a build step (i.e. I don't want to use babel/register, which is why I am trying to use babel/polyfill directly in the first place), so I'd like to put more emphasis on this part of @ssube's answer:

    Make sure you require it at the entry-point to your application, before anything else is called

    I ran into some weird issue where I was trying to require babel/polyfill from some shared environment startup file and I got the error the user referenced - I think it might have had something to do with how babel orders imports versus requires but I'm unable to reproduce now. Anyway, moving import 'babel/polyfill' as the first line in both my client and server startup scripts fixed the problem.

    Note that if you instead want to use require('babel/polyfill') I would make sure all your other module loader statements are also requires and not use imports - avoid mixing the two. In other words, if you have any import statements in your startup script, make import babel/polyfill the first line in your script rather than require('babel/polyfill').

提交回复
热议问题