-
-
Notifications
You must be signed in to change notification settings - Fork 926
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
"TypeError: require
is not defined" runtime error in Webpack bundles coming from mithril.mjs
#2336
Comments
Test repo: https://github.com/ArthurClemens/polythene-mithril-setup/tree/next (so branch "next").
|
Not sure if related but this flems gives the required error. |
Ignore me, this flems is mithril@1 |
For me, the fix was a two-parter:
I have no idea if this was a good way to go about a fix (because frankly Webpack is one of the greatest modern mysteries), but for my environment it worked out fine. |
This repo fails when I update to the latest version 2.0.0-rc.3. |
I can confirm that adding these lines to webpack config solves the issue:
|
^^^^ Works for me as well. Maybe Webpack just needs to add a built-in rule for .mjs files? |
BTW, I filed webpack/webpack#8491 to give Webpack something to track. |
require
is not undefined" runtime error in Webpack bundles coming from mithril.mjs
require
is not defined" runtime error in Webpack bundles coming from mithril.mjs
This is blocked on webpack/webpack#8491. |
Just as a heads up, this is apparently a bug in Webpack v4 that's fixed in v5. I'm still working on getting it fixed in v4 if possible. |
- See: MithrilJS#2336 - See: webpack/webpack#8491 - Note: revert this once the above issue is resolved
- See: MithrilJS#2336 - See: webpack/webpack#8491 - Note: revert this once the above issue is resolved
I found an alternative approach. In const baseDir = process.cwd();
module.exports = {
// ...
resolve: {
// Make sure that Mithril is included only once
alias: {
"mithril/stream": path.resolve(baseDir, "node_modules/mithril/stream/stream.js"),
"mithril": path.resolve(baseDir, "node_modules/mithril/mithril.js"),
},
}
// ...
} By adding this, the extra rule with |
Whenever the next Webpack update comes out, please see if this reproduces, since I've gotten a patch merged that should help avoid this bug. |
I've confirmed the fix using Webpack v 4.28.0. |
Mithril Version: v2.0.0-rc.1-3
Expected Behavior
import m from "mithril"
should work identically regardless of whethermithril
is resolved to themithril.js
ormithril.mjs
bundles.Current Behavior
Errors like this when loading the bundle:
Note that
mithril.mjs
itself has zero instances ofrequire
, which is what makes this so odd. I suspect it's a Webpack bug, but I need a repro to then reduce and send their way first, so I can isolate the problem.Possible Solution
Not sure, but anything that instructs Webpack to ignore Mithril's
"module": "mithril.mjs"
field is sufficient to fix it for users.But of course, if that's the standard advice, then why are we offering
.mjs
bundles in the first place?Steps to Reproduce
Edit: @ArthurClemens' repro
npm install
npm run dev
Context
It's breaking migration for several users.
Additional Information
Your Environment
The text was updated successfully, but these errors were encountered: