babel-node es6 “Modules aren't supported in the REPL”

前端 未结 2 1429
傲寒
傲寒 2021-01-13 05:17

babel-preset-es2015 is installed, and is OK with es6 feature just like below let a = 2;.
But can not work with es6 modules feature import

相关标签:
2条回答
  • 2021-01-13 05:23

    Got it from the official doc:http://babeljs.io/docs/usage/cli/

    ES6-style module-loading may not function as expected
    Due to technical limitations ES6-style module-loading is not fully supported in a babel-node REPL.
    
    0 讨论(0)
  • 2021-01-13 05:28

    The error message is exactly what it says. You cannot use ES6 module syntax in the REPL, it is unsupported. You can create a small adapter that imports as ES6 and exports as CommonJS:

    # es6-to-common.js
    import MyThing from './somewhere';
    module.exports = MyThing;
    

    Now inside your usual babel-node prompt:

    > MyThing = require('./es6-to-common')
    
    0 讨论(0)
提交回复
热议问题