webpack 5 module.hot is undefined

Viewed 886

I recently upgraded our react app to webpack5 and everything works fine but the browser error saying Uncaught ReferenceError: module is not defined on this code:

if (module.hot) {
    module.hot.accept('./Layout/Layout', () => {
        render();
    });
}

can anyone explain what happened and what I could do to fix this?

1 Answers

in webpack5 all nodejs variable like process or module is removed so to get module.hot in webpack 5 you need to use import.meta.webpackHot and change your code to this:

    if (import.meta.webpackHot) {
        import.meta.webpackHot.accept('./Layout/Layout', () => {
            render();
        });
    }
Related