我是新的反应/ redux / webpack,我正在尝试从头开始设置一个新项目,我遇到模块构建失败:SyntaxError:意外的令牌
我已经尝试过类似问题的答案,但还没能解决。
堆栈跟踪
ERROR in ./Counter.js
Module build failed: SyntaxError: Unexpected token (16:6)
14 | render() {
15 | return (
> 16 | <h1>Hello</h1>
| ^
17 | );
18 | }
19 | }
@ ./app.js 3:0-32
@ multi ./app.js
.bablelrc
{
"presets": ["react", "es2015"]
}
webpack.config.js
const path = require('path');
module.exports = {
context: path.join(__dirname, 'src'),
entry: [
'./app.js',
],
output: {
path: path.join(__dirname, 'www'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: [
'babel-loader',
],
},
],
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: {
presets: [ 'react', 'es2015' ]
}
}
]
},
resolve: {
modules: [
path.join(__dirname, 'node_modules'),
],
},
};
的package.json
{
"name": "starter",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"compile": "webpack",
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-latest": "^6.24.1",
"babel-preset-react": "^6.24.1",
"express": "^4.15.3",
"immutable": "^3.8.1",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-redux": "^5.0.5",
"redux": "^3.6.0",
"webpack": "^2.6.0",
"webpack-dev-middleware": "^1.10.2"
},
"devDependencies": {
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"webpack": "^2.6.0"
}
}
app.js
import React from 'react';
import ReactDOM from 'react-dom';
import Counter from './Counter';
document.addEventListener('DOMContentLoaded', function() {
ReactDOM.render(
React.createElement(Counter),
document.getElementById('mount')
);
});
counter.js
import React from 'react';
class Counter extends React.Component {
constructor() {
super();
this.state = {
count: 0,
};
}
render() {
return (
<h1>Hello</h1>
);
}
}
export default Counter;
答案 0 :(得分:1)
用以下代码修改了.babelrc
代码,并解决了该问题:
{
"presets": [
"env",
"react",
"stage-2"
]
}
答案 1 :(得分:0)
使用以下代码修改了我的webpack.config.js并解决了它。
const path = require('path');
module.exports = {
context: path.join(__dirname, 'src'),
entry: [
'./app.js',
],
output: {
path: path.join(__dirname, 'www'),
filename: 'bundle.js',
},
module: {
loaders: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: [ 'react', 'es2015' ]
}
}
]
},
resolve: {
modules: [
path.join(__dirname, 'node_modules'),
],
},
};