-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Problem: Cannot find module hook.js
When setting debug: true, you can see the following error when starting the application:
Sentry Logger [warn]: Failed to register ESM hook Error: Cannot find module './hook.js'
Require stack:
- /[...]/app-dir/.output/server/node_modules/import-in-the-middle/hook.mjs
.....import-in-the-middle is missing the hook.js file in the folder and thus is not able to import it.
Why the file is missing - Node File Trace
Both files (hook.mjs and hook.js) are included in node_modules/import-in-the-middle of the root directory. Nitro uses @vercel/nft to trace the files from the node_modules to include only the needed ones in the node_modules of the build output.
The file is missing in .output/server/node_modules/import-in-the-middle. The folder should contain a hook.js and a hook.mjs file, but only the hook.mjs file is there.
Changes in import-in-the-middle version 1.14.3
The import was changed from
import { createHook } from './hook.js'toconst { createHook } = require('./hook.js')(PR diff here)
and it seems like @vercel/nft does not trace and include files imported with require (from createRequire(import.meta.url). This file is needed to make tracing work on the server-side.
Upcoming Fix
It looks like @vercel/nft only looks for the exact string of module.createRequire and there is an upcoming fix in import-in-the-middle which will change it in a way so it can get picked up by @vercel/nft.
nodejs/import-in-the-middle#207
Workaround
This is something that most possibly @vercel/nft needs to fix and for now you can add a version override to use an older version:
// npm
"overrides": {
"import-in-the-middle": "1.14.2"
}// yarn
"resolutions": {
"import-in-the-middle": "1.14.2"
}// pnpm
"pnpm": {
"overrides": {
"import-in-the-middle": "1.14.2"
}
}