更新:
代码已推送到https://github.com/gsouvik/react_spa_experiment
初始帖子:
我知道那里有数百个线程,一些在webpack配置中有错别字,一些以错误的方式使用了加载程序,一些得到了解决,有些仍然处于打开状态。但是经过无数次尝试,我仍然无法使用Webpack 4,React js进行简单的“ Hello World”工作。
我做什么
我正在逐行关注此视频教程: React & Webpack 4 from scratch
我的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页面显示“我的第一个React Webpack项目”(来自组件/components/App.js)
实际行为
./ src / index.js 5:16中的错误 模块解析失败:意外的令牌(5:16) 您可能需要适当的加载程序来处理此文件类型。 |从“ ./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 main 2
如果需要,我可以通过git repo共享代码库。任何帮助将不胜感激。
答案 0 :(得分:1)
问题出在webpack配置文件中的错字。
您有不正确的module.export。应该是module.exports
工作示例
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
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'
})
]
};