0

I’m trying to run webpack with my React Project, but when I run my project with the commend webpack-dev-serve I get the error:

Cannot GET /

I try looking into the http://localhost:8080/webpack-dev-serve to check if the routing to the build is incorrect but I don’t see a problem.

Can someone help me with this problem? I am looking around for hours now

webpack.config.js

const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
    entry: './src/index.tsx',
    devtool: 'eval-source-map',
    resolve: {
        extensions: ['.tsx', '.ts', '.js'],
    },
    module: {
        rules:[
            {
                test: /\.(ts|js)x?$/,
                exclude: /node_modules/,
                use: [
                    {
                        loader:'babel-loader',
                    },
                ],
                include:[path.resolve(__dirname, 'src')],
            },{
                test: /\.css$/,
                use:['style-loader', 'css-loader'],
                include:[path.resolve(__dirname, 'src')],
            },{
                test: /\.(?:ico|gif|png|jpg|jpeg)$/i,
                type:'asset/resource',
                include:[path.resolve(__dirname, 'src')],
            },
        ],
    },
    output: {
        publicPath: '/build/',
        filename: 'bundle.js',
        path: path.resolve(__dirname, '/build/'),
    },
    mode: 'development',
    plugins: [
        new htmlWebpackPlugin({
            template: path.resolve(__dirname, 'build', 'index.html')
        }),
    ],
}

package.json

{
  "name": "react-shopping-bag",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "serve": "webpack-dev-server",
    "build": "webpack",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/core": "^7.15.8",
    "@babel/plugin-transform-runtime": "^7.15.8",
    "@babel/preset-env": "^7.15.8",
    "@babel/preset-react": "^7.14.5",
    "@babel/preset-typescript": "^7.15.0",
    "@types/react": "^17.0.32",
    "@types/react-dom": "^17.0.10",
    "babel-loader": "^8.2.3",
    "css-loader": "^6.4.0",
    "html-webpack-plugin": "^5.4.0",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "style-loader": "^3.3.1",
    "typescript": "^4.4.4",
    "webpack": "^5.59.1",
    "webpack-cli": "^4.9.1",
    "webpack-dev-server": "^4.3.1"
  }
}

http://localhost:8080/webpack-dev-server

http://localhost:8080/build/bundle.js

http://localhost:8080/build/index.html

file structure

  • Hi, do you have a html file in ./build/index.html? Maybe you should use ./src/index.html? – Cucunber Oct 25 '21 at 19:29
  • Yes, I have a index.html in my build but I can try to do src insteed – Octopus-Charm Oct 25 '21 at 19:39
  • @Cucunber shouldn't the output be the build? and not the src? since you want webpack to use build? P.S I added a img from my file structure – Octopus-Charm Oct 25 '21 at 19:41
  • Remove publicPath in output or set it to '/', after that all routings should work properly. Make different configs for the build and for the test servers – Cucunber Oct 25 '21 at 22:13
  • @Cucunber I understand what you are saying but if I remove the publicPath the serve does work but it doesn’t rebuild when I change my code. And it doesn’t make sense to me that every time I make a change in my code I need to use a command to rebuild it again, don’t you want to make this process automated? – Octopus-Charm Oct 26 '21 at 06:39
  • use nodemon. It will automatically rebuild your project when something has been changed. https://stackoverflow.com/questions/35545093/webpack-watch-and-launching-nodemon – Cucunber Oct 26 '21 at 07:27
  • @Cucunber I looked into it, but I get different errors now, not it says that my var element = document.createElement(“style”) (what comes from the style-loader) refferenceError: document is not defined – Octopus-Charm Oct 28 '21 at 17:59
  • As I understand, you write on Node.js, so in Node.js there is now object document. Use JSDom for this (https://www.npmjs.com/package/jsdom) GLOBAL.document = new JSDOM(html).window.document; – Cucunber Oct 29 '21 at 14:24

0 Answers0