最终结果
新建package.json
{ "name": "Worktile Pro IM", "version": "1.0.0", "main": "app.js", "scripts": { "run": "babel-node app.js", "build": "babel . -d .dist --ignore=\"node_modules\"" }, "devDependencies": { "babel-cli": "*", "babel-core": "*", "babel-preset-es2015-node5": "*", "babel-preset-stage-3": "*", "babel-register": "*" } }
安装依赖
cnpm install --save
配置 .babelrc
{ "presets": ["es2015-node5", "stage-3"], "plugins": [] }
配置入口文件bin.js
// bin.js require('babel-polyfill'); require('babel-register'); require('./main.js'); // 这个地方引入您的项目的启动文件
试一下ES6的模块导出导入功能
lib.js
export const name = 'calc'; export const add = (x, y) => { return x + y; };
main.js
import * as calc from './lib'; const x = 3; const y = 5; console.log(`${calc.name}`); const result_add = calc.add(x, y); console.log(`${x} + ${y} = ${result_add}`);
运行
$ node bin.js
输出
calc 3 + 5 = 8