未找到Entry Module中的错误 - 在Webpack配置文件中

问题描述 投票:2回答:1

我正在尝试为ReactJs设置webpack。我无法解释我的Webpack配置文件出了什么问题

找不到Entry模块中的错误:错误:无法解析'D:\ wd \ javascript \ Projects \ reactjs-basics中的'./src'

代码在这里 - 两个文件“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

reactjs webpack webpack-dev-server webpack-2 webpack-4
1个回答
2
投票

你要修改一些东西

  • 在你的webpack.config.js你有module.export。这是不正确的。它必须是module.exports
  • 你正在使用[email protected][email protected]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删除watchwebpack.config.js

mode: 'development',
watch: true

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

开发模式你不需要设置watchmode,因为当你运行webpack-dev-servernpm 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,您必须配置自己的服务器

如果这有帮助,请告诉我

© www.soinside.com 2019 - 2024. All rights reserved.