gpt4 book ai didi

reactjs - 未找到入口模块错误 - 在 Webpack 配置文件中

转载 作者:行者123 更新时间:2023-12-03 14:06:34 27 4
gpt4 key购买 nike

我正在尝试为 ReactJs 设置 webpack。我不知道我的 Webpack 配置文件有什么问题,这给了我

ERROR in Entry module not found: Error: Can't resolve './src' in 'D:\wd\javascript\Projects\reactjs-basics

代码在这里 - 两个文件“Webpack.config.js”和“Package.json”

Webpack.config.js code is :

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 File is

{
"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"
}
}

**

for reference, Folder Structure with Webpack config code i have attache an image below

**

Please Click here for folder structure, code and folder structure is juxtaposed

最佳答案

您必须修改一些内容

  • 在您的 webpack.config.js 中,您有 module.export。这是不正确的。它必须是module.exports
  • 您正在将 babel-core@6.26.3babel-loader@8.0.5 结合使用。 babel-loader@8.*babel-core@6.* 不兼容。您必须使用 babel-loader@7。使用 npm uninstall -D babel-loader 卸载现有的 babel-loader 并使用 npm install -D 安装 babel-loader@7 babel-loader@7

我注意到的另一件事是,您在 webpack.config.js 中指定了 mode: 'development'。最好通过运行时参数将模式设置为开发或生产

更新

webpack.config.js 中删除 modewatch

mode: 'development',
watch: true

使用以下详细信息更新您的 package.json

开发模式您无需设置 watchmode,因为 webpack-dev-server 会在您运行 npm run 时为您执行此操作开发

"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"
}

要启动本地服务器,您需要在webpack.config.js中添加以下配置。记下 devserver 指向的目录名称

devServer: {
contentBase: path.join(__dirname, "dist/"),
port: 9000
},

生产模式执行npm run prod以在生产模式下构建项目

当你运行npm run dev时,你可以看到localhost处于工作状态。该服务器由webpack-dev-server库启动。对于生产构建,您必须配置自己的服务器

请告诉我这是否有帮助

关于reactjs - 未找到入口模块错误 - 在 Webpack 配置文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55557233/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com