[英]Error using ES7 async/await with node, webpack and babel-loader
I'm trying to use javascript ES7 syntax on the server using node.js with webpack and babel-loader (es2015 + stage-0 presets). 我正在尝试使用带有webpack和babel-loader(es2015 + stage-0预设)的node.js在服务器上使用javascript ES7语法。 I've gotten it to work with babel-node but when I run webpack I get the following error at the async keyword (9:22 is after the async keyword): 我已经让它与babel-node一起工作但是当我运行webpack时,我在async关键字上得到以下错误(9:22在async关键字之后):
ERROR in ./src/server.js Module parse failed: C:\dev\node-async-sample\src\server.js
Unexpected token (9:22) You may need an appropriate loader to handle this file type.
SyntaxError: Unexpected token (9:22)
I've put the code on github at https://github.com/qubitron/node-async-sample , any ideas on how to get this to work? 我把代码放在github上的https://github.com/qubitron/node-async-sample ,有关如何使其工作的任何想法?
Here is the relevant snippet from src/server.js: 以下是src / server.js的相关代码段:
import express from 'express';
import http from 'request-promise';
let server = express();
server.get('/', async function(request, response) {
let result = await http('http://www.google.com');
response.send(result);
});
.babelrc: .babelrc:
{
"presets": [
"es2015",
"node5",
"stage-0"
],
"plugins": [
"transform-runtime"
]
}
and webpack.config.js: 和webpack.config.js:
module.exports = {
entry: [
'babel-polyfill',
'./src/server.js'
],
output: {
path: __dirname + '/dist',
filename: 'server_bundle.js'
},
resolve: {
extensions: ['', '.js', '.jsx']
},
module: {
loaders: [
{
test: /\.jsx?$/,
include: __dirname + '/src',
loader: 'babel-loader'
}
]
}
};
I saw a similar issue here but it has a different error message and was fixed in babel:master: ES7 async await functions with babel-loader not working 我在这里看到了类似的问题,但它有一个不同的错误信息并在babel中修复:master: ES7异步等待函数与babel-loader无法正常工作
Your src path was incorrect. 你的src路径不正确。 You should never (like never :)) join pathes using string concatenation there is path.join
for that. 你永远不应该( 从来没有 :))使用字符串连接加入path.join
,其中有path.join
。
{
test: /\.jsx?$/,
include: path.join(__dirname, 'src'),
loader: 'babel-loader'
}
BTW, this will fix parsing issue but you still gonna need to handle .json
file loading by adding corresponding extension to resolve section and using json-loader
顺便说一句,这将解决解析问题,但你仍然需要通过添加相应的扩展来解决部分和使用json-loader
来处理.json
文件json-loader
{ test: /\.json$/, loader: 'json-loader' }
Also you'll need to handle missing modules warning. 此外,您还需要处理丢失的模块警告。 For example fs
and net
. 例如fs
和net
。
So I do recommend you to use babel-cli
to precompile server code. 所以我建议你使用babel-cli
来预编译服务器代码。
babel src --out-dir dist
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.