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

时间:2019-04-07 08:55:19

标签: reactjs webpack webpack-dev-server webpack-2 webpack-4

我正在尝试为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

1 个答案:

答案 0 :(得分:2)

您需要修改的内容

  • 您的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。使用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-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,您必须配置自己的服务器

让我知道这是否有帮助