0

I am trying to run my javascript app with webpack dev server and I don't get my index.html back, instead I get Cannot GET / with this error inside the console GET http://localhost:8080/ 404 (Not Found). I've looked around for solutions. I tried to remove spaces from the app path and to make sure that the path inside webpack config output is the same as the one I am linking in my script tag inside index.html. But still cannot find the right answer

webpack.config.js

const path = require('path')

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'public/scripts'),
        filename: 'bundle.js'
    },
    module: {
        rules: [{
            test: /\.m?js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
                options: {
                    presets: ['@babel/preset-env']
                }
            }
        }]
    },
    devServer: {
        static: {
            directory: path.resolve(__dirname, 'public'),
            publicPath: '/scripts/'
        }
    }
}

package.json

{
  "name": "boilerplate",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev-server": "webpack-dev-server --mode development",
    "build": "webpack --mode production"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "@babel/cli": "^7.15.7",
    "@babel/core": "^7.15.8",
    "@babel/preset-env": "^7.15.8",
    "babel-loader": "^8.2.3",
    "live-server": "^1.2.1",
    "webpack": "^5.60.0",
    "webpack-cli": "^4.9.1",
    "webpack-dev-server": "^4.3.1"
  }
}

  • What do you get when you go to `localhost:8080/scripts/`? – Reyno Oct 27 '21 at 07:39
  • index.html renders normally but for some reason I get `GET http://localhost:8080/scripts/bundle.js net::ERR_ABORTED 404 (Not Found)` inside the console instead of loading bundle.js script file @Reyno – Muhammad Abd-Elsattar Oct 27 '21 at 07:54
  • I tweaked the src attr inside my script tag and now everything is working properly. But how can I get the same result for `localhost:8080/` instead of `localhost:8080/scripts` ? @Reyno – Muhammad Abd-Elsattar Oct 27 '21 at 07:59
  • You should remove the `publicPath: '/scripts/'`. Then everything should be available at `/` – Reyno Oct 27 '21 at 08:00
  • Does this answer your question? [Cannot GET / error running hello world in webpack](https://stackoverflow.com/questions/37954809/cannot-get-error-running-hello-world-in-webpack) – Thomas May 05 '22 at 17:15

1 Answers1

2

Using HtmlWebpackPlugin

Packages like html-webpack-plugin simplify the creation of HTML files to serve into webpack bundles.

Usage in webpack.config.js:

const htmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
  entry: "./src/index.js",
  output: { path: `${__dirname}/build`, filename: "bundle.js" },
  resolve: { modules: ["src", "node_modules"] },
  module: {
    rules: [
      { test: /\.m?js$/, exclude: /node_modules/, loader: "babel-loader" },
    ],
  },
  plugins: [new htmlWebpackPlugin({ template: "./src/index.html" })],
};

Please note that my answer assumes that all static files are in src folder.

Thomas
  • 147
  • 10