简体   繁体   中英

Import es6 module in repl

I wrote an es6 module that I'm transpiling with Babel.js and packaging with Webpack. I would like to be able to load this module into a repl so that I can use it there, so I installed babel-cli to get babel-node . I saw the notice saying that "ES6-style module-loading may not function as expected," so I wasn't surprised by this:

> import Point from './js/model/Point'
SyntaxError: repl: Modules aren't supported in the REPL
> 1 | import Point from './js/model/Point'
    | ^

But I was surprised by this:

> var Point = require('./js/model/Point')
/Users/brianmc7/workspace/tetris-crush/js/model/Point.js:2
export default class Point {
^^^^^^
SyntaxError: Unexpected token export

What gives? Doesn't babel-node transpile files when I require them in the repl? Is there a JavaScript repl that does, or some easy workaround? Webpack compiles my project just fine, so I know there are no syntax errors.

It turns out that just having your babel configuration in your webpack.config.js doesn't work for babel-node (obvious in retrospect). Goliadkin 's comment on my question lead me to create a .babelrc , which fixed my problem. This minimal configuration was all I needed:

{
    presets: ['es2015']
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM