Wednesday, 12 April 2017

babel-node gives import error when only when writing to file

The purpose of this whole exercise is to run ES2015 javascript in the browser. I have a test setup like this:

foo.js:

class Foo {}

main.js:

import {Foo} from './foo';
console.log('ready')

Finally I have also a .babelrc file:

{
    "presets": [
        "es2015",
        "stage-0"
    ]
}

This code runs fine

$> ./node_modules/.bin/babel-node main.js
ready

The first step (and please correct me if I'm wrong) is to transpile this from ES2015 to ES5

$> ./node_modules/.bin/babel-node main.js -o main-es5.js
/Users/dev/test/main.js:1
(function (exports, require, module, __filename, __dirname) { import {Foo} from './foo'; 
                                                              ^^^^^^
SyntaxError: Unexpected token import

Just for completeness, my last step would be

$> ./node_modules/.bin/browserify main-es5.js bundle.js

Any help why on this matter would be appreciated



via Jeanluca Scaljeri

No comments:

Post a Comment