使用Babel转换指向箭头函数的const

时间:2017-08-28 18:38:53

标签: javascript express babeljs

我正在创建一个快速应用程序启动器。现在写我正在努力配置Babel来编译指向箭头函数的常量:

const initializeMiddleware = app => {
    app.use(morgan('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());
};

编译完成正常但是当我尝试运行应用程序时出现以下错误:

TypeError: initializeMiddleware is not a function
    at Object.<anonymous> (/Projects/Starters/starters-node-express/public/app.js:21:1)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Function.Module.runMain (module.js:605:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:575:3

Babel配置(.babelrc):

{
    "presets": [ "es2015", "es2016", "stage-1" ],
    "plugins": [ "transform-es2015-arrow-functions" ]
}

编译功能具有以下结构:

var initializeMiddleware = function initializeMiddleware(app) {
    app.use(morgan('dev'));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(cookieParser());
};

有人可以帮我解决这个问题吗?

0 个答案:

没有答案