Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ghost opened this issue Mar 6, 2022 · 7 comments · May be fixed by webpack/webpack.js.org#6063
Closed

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

ghost opened this issue Mar 6, 2022 · 7 comments · May be fixed by webpack/webpack.js.org#6063

Comments

@ghost
Copy link

ghost commented Mar 6, 2022

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,
},
};

@vankop
Copy link
Member

vankop commented Mar 7, 2022

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

@avishek143
Copy link

avishek143 commented Mar 26, 2022

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/**/*.*'],

avishek143 added a commit to avishek143/webpack.js.org that referenced this issue Mar 26, 2022
In the [Development Guide under Using Watch Mode section] (https://webpack.js.org/guides/development/#using-watch-mode) 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 minimum configuration that needs to be added to devServer is
`watchFiles: ['./src/**/*.*']`

Fixes: webpack/webpack#15477
@alexander-akait
Copy link
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
Copy link

avishek143 commented Mar 28, 2022

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
Copy link
Member

Please create reproducible repo with example of a problem

@webpack-bot
Copy link
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
Copy link
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants