简体   繁体   English

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

[英]Error in Entry Module not found - in Webpack Config file

I am trying to setup webpack for ReactJs. 我正在尝试为ReactJs设置webpack。 I am clueless what is wrong with my Webpack Config File which gives me 我无法解释我的Webpack配置文件出了什么问题

ERROR in Entry module not found: Error: Can't resolve './src' in 'D:\\wd\\javascript\\Projects\\reactjs-basics 找不到Entry模块中的错误:错误:无法解析'D:\\ wd \\ javascript \\ Projects \\ reactjs-basics中的'./src'

CODE IS HERE - Two files "Webpack.config.js " and "Package.json" 代码在这里 - 两个文件“Webpack.config.js”和“Package.json”

Webpack.config.js code is : 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 File is 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"
  }
}

** **

for reference, Folder Structure with Webpack config code i have attache an image below 作为参考,文件夹结构与Webpack配置代码我附上一个图像

** **

Please Click here for folder structure, code and folder structure is juxtaposed 请点击此处查看文件夹结构,代码和文件夹结构并列

You've to modify few things 你要修改一些东西

  • In your webpack.config.js you have module.export . 在你的webpack.config.js你有module.export This is incorrect. 这是不正确的。 it has to be module.exports 它必须是module.exports
  • You're using babel-core@6.26.3 with babel-loader@8.0.5 . 你正在使用babel-core@6.26.3babel-loader@8.0.5 babel-loader@8.* is not compatible with babel-core@6.* . babel-loader@8.*babel-core@6.*不兼容。 You've to use babel-loader@7 . 你要使用babel-loader@7 Uninstall your existing babel-loader using npm uninstall -D babel-loader and install babel-loader@7 using npm install -D babel-loader@7 卸载现有的babel-loader使用npm uninstall -D babel-loader和安装babel-loader@7使用npm install -D babel-loader@7

Another thing i noted is, you have specified mode: 'development' in your webpack.config.js . 我注意到的另一件事是,你在webpack.config.js指定了mode: 'development' its better to set mode to development or production via runtime parameters 最好通过运行时参数将mode to development or production设置mode to development or production

Update 更新

Remove mode & watch from your webpack.config.js webpack.config.js删除modewatch

mode: 'development',
watch: true

Update your package.json with below details. 使用以下详细信息更新您的package.json

Development mode You don't need to set watch & mode as webpack-dev-server will do that for you when you run npm run dev 开发模式您不需要设置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"
}

To launch local server you need to add below configuration in your webpack.config.js . 要启动local server您需要在webpack.config.js添加以下配置。 Note the directory name to which the devserver points to. 请注意devserver指向的directory name

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

Production mode Execute npm run prod to build your project in production mode 生产模式执行npm run prod以生产模式构建项目

You can see localhost in working state when your run npm run dev .This server is launched by webpack-dev-server library. 运行npm run dev时,您可以看到localhost处于工作状态。此服务器由webpack-dev-server库启动。 For production build you have to configure your own server 对于production build您必须配置自己的服务器

Let me know if this helps 如果这有帮助,请告诉我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM