react-refresh-webpack-plugin: require is not defined with es6 native modules => wrong import path and require fonction in webpack output

I get a

πŸ”΄ ReferenceError require is not defined

πŸ“I use esm native modules. πŸ“My config matches the readme

but webpack generate a weird import line for __webpack_require__.$Refresh$.runtime for every module:

                            WRONG REQUIRE v                         WRONG PATH v
__webpack_require__.$Refresh$.runtime = require('C:/Users/myusername/dev/devops-cli/node_modules/react-refresh/runtime.js');

πŸ”it is as if it was not using the correct require, nor rewriting properly the path.

You can find more details below as well as a longer extract from webpack output. What else can I provide ?

Any help or workaround appreciated ! πŸ‘


config:

plugins: [
    new webpack.HotModuleReplacementPlugin(),
    new ReactRefreshWebpackPlugin(),
]
....
module: {
    rules: [
        {
            // https://github.com/pmmmwh/react-refresh-webpack-plugin
            test: /\.js$/,
            exclude: /node_modules/,
            use: [
                {
                    loader: require.resolve('babel-loader'),
                    options: { plugins: [require.resolve('react-refresh/babel')], },
                },
            ],
        },
    ]
}

misc packages versions:

/***/ "./packages/test/counterUI1.js":
/*!*************************************!*\
  !*** ./packages/test/counterUI1.js ***!
  \*************************************/
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "CounterUI": () => /* binding */ CounterUI,
/* harmony export */   "foo": () => /* binding */ foo
/* harmony export */ });
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "./node_modules/react/index.js");
/* harmony import */ var _emotion_react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @emotion/react */ "./node_modules/@emotion/react/dist/emotion-react.browser.esm.js");
/* provided dependency */ var __react_refresh_utils__ = __webpack_require__(/*! ./node_modules/@pmmmwh/react-refresh-webpack-plugin/lib/runtime/RefreshUtils.js */ "./node_modules/@pmmmwh/react-refresh-webpack-plugin/lib/runtime/RefreshUtils.js");
/* provided dependency */ var __react_refresh_error_overlay__ = __webpack_require__(/*! ./node_modules/@pmmmwh/react-refresh-webpack-plugin/overlay/index.js */ "./node_modules/@pmmmwh/react-refresh-webpack-plugin/overlay/index.js");
                                            v HERE v 
__webpack_require__.$Refresh$.runtime = require('C:/Users/rvion/dev/devops-cli/node_modules/react-refresh/runtime.js');
__webpack_require__.$Refresh$.setup(module.id);

Thanks

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 23 (8 by maintainers)

Most upvoted comments

I’ll try to push out the WIP new runtime on this tonight.

Thanks. I’m using the latest version of Webpack 5 yes.

i can confirm that this is still broken in 0.5.0-rc.5

This has something to do with monorepo compatibility, it will be addressed in the next RC.

Hi, unfortunately the semantics needed for the fix is a bit more complex than expected.

The runtime is currently in a half-baked state where some tests pass and some don’t (hopefully some progress in a PR soon). I’m continuously working on it currently.

In particular, some blockers:

  • HMR error recovery semantics support webpack/webpack#12826 (currently fixed but unreleased: webpack/webpack#12845)
  • Proper variable declarations according to current runtime requirements (var, const, let)

A fix should be ready this week. I’m planning to ship 0.5.0 within February.

Hi, thanks for reporting this bug. I think this has something to do with our way of injecting dependencies via a loader. I can publish a beta version that might fix this, would you mind trying it out to see if it works or provide a minimal native ESM webpack project for reproduction?