更新:
代码已推送到https://github.com/gsouvik/react_spa_experiment
初始帖子:
我知道有成百上千个主题,有些在webpack配置中有拼写错误,有些错误地使用了加载器,有些得到了解决,但有些仍然未解决。但是在尝试多次后,我仍然无法使用Webpack 4和React js创建一个简单的“Hello World”。
我所做的事情
我一行一行地按照这个视频教程进行操作: 从零开始学习React & Webpack 4
我的package.json
{
"name": "my_react_experiment_2",
"version": "1.0.0",
"description": "Basic react with webpack ",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server --mode development --hot",
"build": "webpack --mode production"
},
"author": "Souvik Ghosh",
"license": "ISC",
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"html-webpack-plugin": "^3.2.0",
"webpack": "^4.17.1",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
}
}
我的webpack.config.js文件
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.export = {
entry: './src/index.js',
output: {
path: path.join(__dirname, '/build'),
filename: 'index_bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/templates/index.html'
})
]
};
我的 .babelrc
{
"presets": ["env", "react"]
}
我的目录结构
期望的行为
我启动开发服务器 npm run dev
,希望看到我的 React js 页面显示 "My first React Webpack project" (来自组件 /components/App.js)。
实际的行为
ERROR in ./src/index.js 5:16 Module parse failed: Unexpected token (5:16) You may need an appropriate loader to handle this file type. | import App from "./components/App"; | ReactDOM.render(, document.getElementById('root')); | //ReactDOM.render('Hello User ', document.getElementById('root')); @ multi (webpack)-dev-server/client?http://localhost:8080 (webpack)/hot/dev-server.js ./src main2
如有需要,我可以通过 git 仓库分享代码库。非常感谢您的帮助。