我正在尝试为ReactJs设置webpack。我不知道我的Webpack配置文件出了什么问题
找不到入口模块中的错误:错误:无法解析“ ./src” 'D:\ wd \ javascript \ Projects \ reactjs-basics
此处有代码-两个文件“ Webpack.config.js”和“ Package.json”
Webpack.config.js代码为:
var webpack = require('webpack');
var path = require('path');
var DIST_DIR = path.resolve(__dirname,'dist');
var SRC_DIR = path.resolve(__dirname,'src');
var config = {
entry: SRC_DIR+'/app/index.js',
output:{
path:DIST_DIR+'/app',
filename:'bundle.js',
publicPath:'/app/'
},
module:{
rules: [
{
test: /\.js?/,
include: SRC_DIR,
use:{
loader:'babel-loader',
query:{
presets:['react','es2015','stage-2']
}
}
}
]
},
mode: 'development',
watch: true
}
module.export = config;
Package.json文件为
{
"name": "reactjs-basics",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": " npm run build",
"build": "webpack -d && copy src\\app/index.html dist\\index.html && webpack-dev-server --content-base src\\ --inline --hot",
"build:prod": "webpack -p && cp src\\app/index.html dist\\index.html"
},
"author": "",
"license": "ISC",
"dependencies": {
"react": "^16.8.6",
"react-dom": "^16.8.6"
},
"devDependencies": {
"2015": "0.0.1",
"babel-core": "^6.26.3",
"babel-loader": "^8.0.5",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"webpack": "^4.29.6",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.2.1"
}
}
**
作为参考,带有Webpack配置代码的文件夹结构我在下面附加了一个图片
**
Please Click here for folder structure, code and folder structure is juxtaposed
答案 0 :(得分:2)
您需要修改的内容
webpack.config.js
中有module.export
。这是不正确的。它必须是module.exports
babel-core@6.26.3
与babel-loader@8.0.5
一起使用。 babel-loader@8.*
与babel-core@6.*
不兼容。您必须使用babel-loader@7
。使用babel-loader
卸载现有的npm uninstall -D babel-loader
并使用babel-loader@7
安装npm install -D babel-loader@7
我注意到的另一件事是,您在mode: 'development'
中指定了webpack.config.js
。最好通过运行时参数设置mode to development or production
更新
从您的mode
中删除watch
和webpack.config.js
mode: 'development',
watch: true
使用以下详细信息更新您的package.json
。
开发模式
您无需将watch
和mode
设置为webpack-dev-server
即可运行npm run dev
"scripts": {
"webpack": "webpack",
"dev": "mkdir -p dist && cp ./src/index.html dist/index.html && webpack-dev-server",
"prod": "mkdir -p dist && cp ./src/index.html dist/index.html && npm run webpack -- --mode production"
}
要启动local server
,您需要在webpack.config.js
中添加以下配置。请注意directory name
指向的devserver
。
devServer: {
contentBase: path.join(__dirname, "dist/"),
port: 9000
},
生产模式执行npm run prod
以生产模式构建项目
运行npm run dev
时,您可以看到localhost处于工作状态。此服务器由webpack-dev-server
库启动。对于production build
,您必须配置自己的服务器
让我知道这是否有帮助