Skip to content

Fails to update index.js when using webpack-dev-server #15477

Closed
webpack/webpack.js.org
#6063
@ghost

Description

Bug report

I was following the webpack documentation guide.
After installing webpack-dev-server, It's supposed to refresh in place.
However, that is not happening. I got the following error:

Uncaught TypeError: Cannot set properties of undefined (setting './src/index.js')
at self.webpackHotUpdatewebkack_guide (jsonp chunk loading:44:1)
at index.17c225e1ecf28bdb9b74.hot-update.js:2:38

HMR] Update failed: ChunkLoadError: Loading hot update chunk index failed.
(missing: http://localhost:8080/index.17c225e1ecf28bdb9b74.hot-update.js)

I do not understand what could be wrong. It only works after I refresh the page.
Interestingly, I could change any other file than index.js and it refreshes correctly.
The problem appears when I change anything inside index.js

Any suggestion?

Other relevant information:
node v14.16.1
// package.json
{
"name": "webkack-guide",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"watch": "webpack --watch",
"build": "webpack",
"start": "webpack serve --open"
},
"keywords": [],
"author": "",
"license": "ISC",
"private": true,
"devDependencies": {
"html-webpack-plugin": "^5.5.0",
"webpack": "^5.70.0",
"webpack-cli": "^4.9.2",
"webpack-dev-server": "^4.7.4"
},
"dependencies": {
"lodash": "^4.17.21"
}
}

// index.js
import _ from "lodash";

import printMe from "./print";

function component() {
const element = document.createElement("div");
const btn = document.createElement("button");

element.innerHTML = _.join(["hello", "webpack"], " ");

btn.innerHTML = "click me and check the console";
btn.onclick = printMe;

element.appendChild(btn);

return element;
}

document.body.appendChild(component());

// print.js
export default function printMe() {
console.log("I get called from print.js 5");
}

// webpack.config
const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");

module.exports = {
mode: "development",
entry: {
index: "./src/index.js",
print: "./src/print.js",
},
devServer: {
static: "./dist",
},
devtool: "inline-source-map",
plugins: [new HtmlWebpackPlugin({ title: "Output Management" })],
output: {
filename: "[name].bundle.js",
path: path.resolve(__dirname, "dist"),
clean: true,
},
};

Activity

vankop

vankop commented on Mar 7, 2022

@vankop
Member

please create reproducible repo. from your description unclear what is a problem, maybe you just need to remove devServer option from config

avishek143

avishek143 commented on Mar 26, 2022

@avishek143

This is a valid documentation issue and most likely belongs to the Documentation repository.

In the Development Guide under Using Watch Mode section the content says:

The only downside is that you have to refresh your browser in order to see the changes. It would be much nicer if that would happen automatically as well, so let's try webpack-dev-server which will do exactly that.

The follow-on section about webpack-dev-server is expected to provide guidance about creating a setup that would reload new code as it is written and refresh the browser to show the changes. But the code example doesnot do so. The problem is in the webpack.config.js code example. The devServer configuration as mentioned will not refresh the browser. To actually refresh the page the minimal configuration that needs to be added to devServer is
watchFiles: ['src/**/*.*'],

added a commit that references this issue on Mar 26, 2022
f78b89e
alexander-akait

alexander-akait commented on Mar 28, 2022

@alexander-akait
Member

What do you mean - refresh? watchFiles only for reload when you change externals files (i.e. not used in bundle, for example .php/.py/etc)

avishek143

avishek143 commented on Mar 28, 2022

@avishek143

What do you mean - refresh?

refresh the browser means reload the browser with the new changed code.

watchFiles only for reload when you change externals files (i.e. not used in bundle, for example .php/.py/etc)

I don't think that is the case. If it is so the documentation must state the same. And there is no code anywhere which will prevent us from mentioning a file that is bundled in the watchFiles list.

Anyways, let's focus on the issue at hand. It is simple. The mentioned Guide and associated example doesn't reload the browser with changed code as stated. It needs to be rectified. My PR tries to do that. If there's a different solution let's apply that and rectify the Guide.

alexander-akait

alexander-akait commented on Mar 28, 2022

@alexander-akait
Member

Please create reproducible repo with example of a problem

webpack-bot

webpack-bot commented on Jun 27, 2022

@webpack-bot
Contributor

This issue had no activity for at least three months.

It's subject to automatic issue closing if there is no activity in the next 15 days.

webpack-bot

webpack-bot commented on Jul 12, 2022

@webpack-bot
Contributor

Issue was closed because of inactivity.

If you think this is still a valid issue, please file a new issue with additional information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @avishek143@alexander-akait@vankop@webpack-bot

      Issue actions

        Fails to update index.js when using webpack-dev-server · Issue #15477 · webpack/webpack