Import es6 module in repl

Brian McCutchon

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.

Brian McCutchon

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']
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related