nodejs / import-in-the-middle

Like `require-in-the-middle`, but for ESM import
https://www.npmjs.com/package/import-in-the-middle
Apache License 2.0
52 stars 20 forks source link

`getFullCjsExports` does not resolve re-exports from external dependencies #95

Closed timfish closed 3 weeks ago

timfish commented 1 month ago

This had been fixed for ESM but not for CJS.

The following code should result in a default export test.cjs

module.exports = require('util').deprecate;

Instead it results in the following error:

node:internal/process/esm_loader:40
      internalBinding('errors').triggerUncaughtException(
                                ^
Error: ENOENT: no such file or directory, open '/Users/tim/Documents/Repositories/import-in-the-middle/test/check-exports/util'
    at Object.readFileUtf8 (node:internal/fs/sync:25:18)
    at Object.readFileSync (node:fs:441:19)
    at getExports (/Users/tim/Documents/Repositories/import-in-the-middle/lib/get-exports.js:69:17)
    at async Promise.all (index 0)
    at async getFullCjsExports (/Users/tim/Documents/Repositories/import-in-the-middle/lib/get-exports.js:23:9)
    at async processModule (/Users/tim/Documents/Repositories/import-in-the-middle/hook.js:131:23)
    at async getSource (/Users/tim/Documents/Repositories/import-in-the-middle/hook.js:249:23)
    at async load (/Users/tim/Documents/Repositories/import-in-the-middle/hook.js:280:26)
    at async nextLoad (node:internal/modules/esm/hooks:833:22)
    at async Hooks.load (node:internal/modules/esm/hooks:416:20) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/Users/tim/Documents/Repositories/import-in-the-middle/test/check-exports/util'
}

I think this is due to this code which doesn't support resolving bare specifiers or node built-ins? https://github.com/DataDog/import-in-the-middle/blob/a8da141be1e0fe802cea9bdb83d39a61c3aea7f7/lib/get-exports.js#L20-L30