Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 869
Subhendu Mondal Avatar asked May 15 '26 23:05

Subhendu Mondal


1 Answers

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'),
        ],
      },
    };
like image 170
paulzmuda Avatar answered May 18 '26 12:05

paulzmuda



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!