Webpack/Babel/React-“未捕获的SyntaxError:意外令牌:" [英] Webpack/Babel/React - "Uncaught SyntaxError: Unexpected token :"
问题描述
我有一个正在运行的webpack-dev-server可以编译并提供一些Babel/React代码.我已经尽力让它在localhost:3001
上提供已编译的client.js
.
I have a webpack-dev-server running that compiles and serves some Babel/React code. I have gotten as far as to have it serve the compiled client.js
over localhost:3001
.
但是当我尝试在HTML中包含脚本时,在Chrome的开发者控制台中出现以下错误:
But when I try to include the script in my HTML, I get the following error in Chrome's developer console:
routerWarning.js:19 Uncaught SyntaxError: Unexpected token :
该行属于react-router,并包含以下代码:
That line belongs to react-router and contains the following code:
process.env.NODE_ENV !== 'production' ? _warning2['default'].apply(undefined, [falseToWarn, message].concat(args)) : undefined;
首先,我看不到该行代码将如何导致语法错误.其次,我不明白它是如何进入的,因为对我来说,这看起来像一段经过编译(精化)的代码.最后,我不知道该如何解决! :(
First, I don't see how that line of code would cause a syntax error. And second, I don't understand how it got in, because this looks like a piece of compiled (babelified) code to me. And finally, I don't know how to fix it! :(
任何帮助将不胜感激.
推荐答案
我正在使用webpack的DefinePlugin来设置process.env.BABEL_ENV
,如下所示:
I was using webpack's DefinePlugin to set process.env.BABEL_ENV
like this:
new DefinePlugin({
'process.env': {
BABEL_ENV: JSON.stringify('client')
}
});
这导致webpack将代码中的所有process.env
实例替换为{"BABEL_ENV":"client"}
. SyntaxError是在此部分而不是在三元表达式中引起的.
This resulted in webpack replacing all instances of process.env
in the code with {"BABEL_ENV":"client"}
. The SyntaxError was caused in this part, not in the ternary expression.
我通过这样设置process.env.BABEL_ENV
来修复它:
I fixed it by setting process.env.BABEL_ENV
like this:
new DefinePlugin({
'process.env.BABEL_ENV': JSON.stringify('client')
});
这篇关于Webpack/Babel/React-“未捕获的SyntaxError:意外令牌:"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!