Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
192 views
in Technique[技术] by (71.8m points)

reactjs - React: Webpack returns cannot / Get

I'm newbie with Webpack and I'm trying to serve a page from development but I've got the error

Cannot / GET

The configuration of my webpack.config.babel.js is:

import webpack from "webpack";
import path from "path";
import HtmlWebpackPlugin from "html-webpack-plugin";

console.log("devMode: " + process.env.NODE_ENV);

const currentPath = path.join(__dirname);

module.exports = {
    entry: "./src/index.js",
    output: {
        path: path.resolve("dist/assets"),
        filename: "js/[name].bundle.js",
        chunkFilename: "js/[name].bundle.js",
        //publicPath: "/"   //It's mandatory to define this publicPath to get access to the website when we reload     
    },
    devtool: "source-map",
    devServer: {
        inline: true,
        contentBase: './dist', 
        port: 3000,
        open: true, //Open web browser by default
        historyApiFallback: true
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: "./src/index.html",    //where is our template
            filename: "../index.html",              //where we are going to put our index.html inside the output directory
            //favicon: "./src/client/img/favicon-96x96.png",
            minify: {
                collapseWhitespace: true,
                removeComments: true,
                removeRedundantAttributes: true,
                removeScriptTypeAttributes: true,
                removeStyleLinkTypeAttributes: true,
                useShortDoctype: true
            }            
        }),
    ],
    module: {
        rules: [
            {
                test: /.js$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader: "babel-loader",
                        options: {
                            presets: ["@babel/preset-react"]
                        }
                    }
                ]
            }
        ]
    }
};

The structure of files is:

enter image description here

And my package.json code file is:

{
  "scripts": {
    "clean": "rm -rf ./dist",
    "_comment0": "Script para arrancar webpack como servidor de desarrollo del front-page",
    "dev-webpack": "NODE_ENV=development webpack serve --mode development --env development --hot",
  },
  "dependencies": {
    "morgan": "^1.10.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1"
  },
  "devDependencies": {
    "@babel/core": "^7.12.13",
    "@babel/preset-env": "^7.12.13",
    "@babel/preset-react": "^7.12.13",
    "@babel/register": "^7.12.13",
    "babel-loader": "^8.2.2",
    "html-webpack-plugin": "^5.0.0",
    "jshint": "^2.12.0",
    "path": "^0.12.7",
    "regenerator-runtime": "^0.13.7",
    "serve": "^11.3.2",
    "webpack": "^5.20.1",
    "webpack-cli": "^4.5.0",
    "webpack-dev-server": "^3.11.2"
  }
}

To start the webpack server I use this command: npm run dev-webpack

What am I doing wrong?

question from:https://stackoverflow.com/questions/66051890/react-webpack-returns-cannot-get

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...