sorry for all the spam, haha

sorry for all the spam, haha
workerd/io/worker.c++:1816: info: uncaught exception; source = Uncaught (in promise); exception = TypeError: Cannot use require() to import an ES Module.
workerd/io/io-context.c++:359: info: uncaught exception; exception = workerd/jsg/_virtual_includes/jsg/workerd/jsg/value.h:1343: fa

workerd/io/worker.c++:1816: info: uncaught exception; source = Uncaught (in promise); exception = TypeError: Cannot use require() to import an ES Module.
workerd/io/io-context.c++:359: info: uncaught exception; exception = workerd/jsg/_virtual_includes/jsg/workerd/jsg/value.h:1343: fa

that error is not really actionable 😦
28 Replies
MrBBot
MrBBot9mo ago
No worries! Thanks for being an early adopter! 😅 Could you paste your output with NODE_DEBUG=vitest-pool-workers:module-fallback again?
chronark
chronarkOP9mo ago
gotcha, looks like another package that's not esm ready
MrBBot
MrBBot9mo ago
Are you using pg by chance?
chronark
chronarkOP9mo ago
no we're using planetscale + drizzle
MrBBot
MrBBot9mo ago
Ah ok 👍
chronark
chronarkOP9mo ago
do you think there's a way to make this work with cjs modules? cause that seems to be a pretty large blocker for us at least I could probalby spend a few more hours patching modules manually, but at some point it becomes hard to reproduce 😄
MrBBot
MrBBot9mo ago
You could try force those deps to be "inlined": https://vitest.dev/config/#server-deps-inline. This will force them to be transformed by Vite first, rather than loading them as is. Something like test: { server: { deps: { inline: [/@hono\/zod-validator/] } } }
chronark
chronarkOP9mo ago
tried that for a bit, after adding a few, I've arrived at a new error 😄
Error: require() is not yet implemented in Workers
Error: require() is not yet implemented in Workers
I'm guessing it's still trying to import some cjs
MrBBot
MrBBot9mo ago
Interesting, that error suggests something is trying to import createRequire() from the node:module module. Does it give a stack trace?
chronark
chronarkOP9mo ago
workerd/io/worker.c++:1816: info: uncaught exception; source = Uncaught (in promise); exception = Error: require() is not yet implemented in Workers
workerd/io/io-context.c++:359: info: uncaught exception; exception = workerd/jsg/_virtual_includes/jsg/workerd/jsg/value.h:1343: failed: jsg.Error: require
workerd/io/worker.c++:1816: info: uncaught exception; source = Uncaught (in promise); exception = Error: require() is not yet implemented in Workers
workerd/io/io-context.c++:359: info: uncaught exception; exception = workerd/jsg/_virtual_includes/jsg/workerd/jsg/value.h:1343: failed: jsg.Error: require
not really tried with your debug flag as well but couldn't find anything that points me into a direction maybe I'll slap a bounty on this issue and some wizard can figure it out 😄
MrBBot
MrBBot9mo ago
Maybe try add __console.log(new Error("trace").stack) to the createRequire() function in node_modules/@cloudflare/vitest-pool-workers/dist/worker/lib/node/module.mjs?
chronark
chronarkOP9mo ago
or maybe I should start from scratch and slowly add our stuff, right now it's a wild guessing game sure nice, that gives me some new pointers to dig both lead to use of crypto if that rings a bell
MrBBot
MrBBot9mo ago
hmmm, what happens if you log the argument to createRequire() too? Or the arguments passed to the function returned from createRequire()?
chronark
chronarkOP9mo ago
oh damn, createRequire is called in like 20 files 😄
MrBBot
MrBBot9mo ago
All part of the same package? Some of those will be harmless It's only when the function returned by createRequire() is called you have a problem
chronark
chronarkOP9mo ago
no, all over the place in vitest and our own code
chronark
chronarkOP9mo ago
I don't see any logs from the return function 🙂 which is weird, cause it throws that error 😄
MrBBot
MrBBot9mo ago
Yeah 😅
chronark
chronarkOP9mo ago
let me throw adifferent error
MrBBot
MrBBot9mo ago
You're definitely using __console not console? (Vitest patches console with some fancy annotation stuff, __console is the original unpatched console that logs direct to stdout)
chronark
chronarkOP9mo ago
yeah , file:///Users/andreasthomas/github/unkeyed/unkey/node_modules/.pnpm/@[email protected][email protected]/node_modules/@asteasolutions/zod-to-openapi/dist/index.js it's running here, but I thought I inlined that
MrBBot
MrBBot9mo ago
Oh interesting, I wonder if inlining is what's causing the issue. Usually require() is only called inside "non-inlined" modules returned by the fallback service. How are you marking this module as inline? I wonder if it's not inlining some of its internal files.
chronark
chronarkOP9mo ago
like you suggested above
export default defineWorkersConfig({
test: {
server: {
deps: {
inline: [
/@hono\/zod-validtor/,
/@hono\/zod-openapi/,
/zod-to-openapi/,
/base-x/,
/@asteasolutions\/zod-to-openapi/,
],
},
},
...
export default defineWorkersConfig({
test: {
server: {
deps: {
inline: [
/@hono\/zod-validtor/,
/@hono\/zod-openapi/,
/zod-to-openapi/,
/base-x/,
/@asteasolutions\/zod-to-openapi/,
],
},
},
...
MrBBot
MrBBot9mo ago
hmmm ok Hmmm, could you enable dumpModules maybe https://vitest.dev/config/#server-debug-dumpmodules and see what the output for zod-to-openapi is? I've got to head off now, but thank you for working through this 🙂
chronark
chronarkOP9mo ago
I might just give up for now 😄 I'll push what I have and add a bounty to it, so someone smarter than me can figure it out 😄 that's one nice thing about oss after all oh sure, hang on
chronark
chronarkOP9mo ago
it's using require, which I'm guessing is the issue here?
MrBBot
MrBBot9mo ago
Yeah, interesting the require() isn't transformed by Vite 🤔
Want results from more Discord servers?
Add your server