2017-04-12 68 views
0

整个练习的目的是在浏览器中运行ES2015 javascript。我有一个测试的设置是这样的:仅当写入文件时,babel-node才会导入错误

foo.js:

export class Foo {} 

main.js:

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

最后我还有.babelrc文件:

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

这代码运行良好

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

第一步(并请纠正我,如果我错了)是从ES2015 transpile这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 

只是为了保持完整性,我的最后一步将是

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

任何帮助为什么在这个问题上,将不胜感激

+1

如果你使用的是Browserify,你会希望https://github.com/babel/babelify – loganfsmyth

+0

thnx,babelify是我需要的,thnx! –

回答

2

babel-node只是一个交互式repl预编译输入源。

编译器ES6并发出ES5,这样做:

babel main.js > main-es5.js

顺便说一句,你还需要export类中foo.js,使其在main.js可用。在你的例子中,Foo是未定义的。