错误信息
ERROR in ./src/index.js
Module build failed: SyntaxError: Unexpected token (6:4)
已有配置信息
package.json
{
"name": "react-xipiker-v1",
"version": "1.0.0",
"description": "react-xipiker-v1",
"main": "index.js",
"scripts": {
"build": "webpack --config ./build/webpack.dev.config.js",
"start": "webpack-dev-server --config ./build/webpack.dev.config.js --color --progress"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"webpack": "^3.10.0",
"webpack-dev-server": "^2.9.7"
},
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^4.3.1"
}
}
webpack.dev.config
const path = require('path')
module.exports = {
entry: path.join(__dirname, '../src/index.js'),
output: {
path: path.join(__dirname, '../dist'),
filename: 'bundle.js'
},
module: {
rules: [{
test: /\.js$/,
use: ['babel-loader?cacheDirectory=true'],
exclude: path.resolve(__dirname, 'node_modules')
}],
},
devServer: {
port: 8080,
contentBase: path.join(__dirname, './dist'),
historyApiFallback: true,
host: '127.0.0.1'
}
}
解决方法
在项目根目录新建.babelrc
{
"presets": [
"react",
"es2015"
]
}