Subhendu Mondal
Subhendu Mondal

Reputation: 131

React & Webpack: Loading failed for the <script> with source http://localhost:8080/app/bundle.js

I am new in reactjs. I tried to configure react with basic index page including index.js(containing a console.log()) but when i tried to run server index.html showing properly but bundle.js is not loading. I search it a lot but not getting proper answer can any one help me please.

my webpack.config.js is

    // Webpack config js.

var webpack = require("webpack");
var path = require("path");

var DIST_VAR = path.resolve(__dirname, "dist");
var SRC_VAR = path.resolve(__dirname, "src");


var config = {
    entry : SRC_VAR + "\\app\\index.js",
    output: {
            path: DIST_VAR + "\\app\\",
            filename: "bundle.js",
            publicPath : "\\app\\",
    },
    module: {
            rules: [
                {
                    test: /\.js?/,
                    include: SRC_VAR,
                    loader: "babel-loader",
                    query: {
                        presets: [ "react" , "es2015" , "stage-2"]
                    }
                }
            ]
    }

};

module.exports = config;

Error is showing in console: Loading failed for the with source “http://localhost:8080/app/bundle.js”.

Edit:

Folder Listing added.. Folder PATH listing Volume serial number is BE9C-4E51

C:.
|   package-lock.json
|   package.json
|   webpack.config.js
|   
+---dist
|   |   index.html
|   |   
|   \---app
|           bundle.js
|           
+---node_modules
|    <Here the node_modules>
\---src
    |   index.html
    |   
    \---App
            index.js

Upvotes: 4

Views: 10870

Answers (2)

nativelectronic
nativelectronic

Reputation: 862

after installing

npm init -y 

and

npm install --save-dev webpack webpack-dev-server webpack-cli

and your structure files

src/
build/
webpack.config.js
package.json

go to package.json, and add build command:

"scripts": {
    "start":"webpack serve --mode development",
    "build":"webpack"
  },

in webpack.config.js

const path = require('path');
 
module.exports = {
  entry: path.resolve(__dirname, './src/index.js'),
  output: {
    path: path.resolve(__dirname, './build'),
    filename: 'bundle.js',
  },
  devServer: {
    contentBase: path.resolve(__dirname, './build'),
  },
};

so,in your build/index.html

<script type="text/javascript" src="./bundle.js"></script>

Upvotes: 0

paulzmuda
paulzmuda

Reputation: 181

I'll make some assumptions without seeing your project folder structure. Looks like it could be your publicPath. Unless that's what you intended, the /app folder shouldn't be visible and since your console is showing "localhost:8080/app/bundle.js" that means it's looking for "project-root/src/app/app/bundle.js" instead of "project-root/src/app/bundle.js"

In the webpack docs they're telling you to default to root '/' and looking at my own webpack file thats what mine is currently set to as well.

Reference: https://webpack.js.org/guides/public-path/

Edit: Here's an example using Webpack 3. Version 4 just came out and this will not work, so I'd be careful where you're getting your config examples from if you are using Webpack 4.

const webpack = require('webpack');
const path = require('path');

module.exports = {
    plugins: [
      // new webpack.NamedModulesPlugin(),
      // new webpack.HotModuleReplacementPlugin()
    ],
    context: path.join(__dirname, 'src'),
    entry: [
      //  'webpack/hot/dev-server',
      //  'webpack-hot-middleware/client',
      //  'babel-polyfill',
      //  'history',
      './index.js'
    ],
    output: {
      path: path.join(__dirname, 'www'),
      filename: 'bundle.js',
      publicPath: '/'
    },
    module: {
      loaders: [{
        test: /\.js$/,
        exclude: /node_modules/,
        loaders: ['react-hot-loader/webpack', 'babel-loader']
      }],
      resolve: {
        modules: [
          path.join(__dirname, 'node_modules'),
        ],
      },
    };

Upvotes: 1

Related Questions