我有使用Babel transpiler在客户端上用ES6编写的同构应用程序。我希望我的快速服务器具有与客户端代码相同的ES6语法。
不幸的require('babel/register')是行不通..
require('babel/register')
require('babel/register'); // doesn't work // require('babel-core/register); doesn't work.. const env = process.env.NODE_ENV || 'development'; const port = process.env.NODE_PORT || 1995; const http = require('http'); const express = require('express'); const address = require('network-address'); let app = express(); app.set('port', port); app.use(express.static(path.join(__dirname, 'public'))); app.get('*', (req, res) => { res.send('Hello!'); }); http.createServer(app).listen(app.get('port'), function () { console.info('Demo app is listening on "%s:%s" env="%s"', address(), app.get('port'), env); });
require('babel/register')不转译从其调用的文件。如果你想包括在上即时transpilation server.js,你应该执行它babel- node(巴别塔的CLI替代的node)。
babel- node
node