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

IITM Throws Error When an NPM Package is Not Installed in the Root Folder #97

Closed aryamohanan closed 6 days ago

aryamohanan commented 3 weeks ago

I am encountering an issue while using prisma module in my test folder along with the import-in-the-middle. When attempting to import the prisma module from a subdirectory, I receive the following error:

node:internal/process/esm_loader:40 internalBinding('errors').triggerUncaughtException( ^ Error: Cannot find module '.prisma/client/default' Require stack: /prismabug/node_modules/import-in-the-middle/lib/get-exports.js /prismabug/node_modules/import-in-the-middle/hook.js at Module._resolveFilename (node:internal/modules/cjs/loader:1134:15) at Function.resolve (node:internal/modules/helpers:188:19) at /prismabug/node_modules/import-in-the-middle/lib/get-exports.js:26:33) at Array.map () at getFullCjsExports (/prismabug/node_modules/import-in-the-middle/lib/get-exports.js:23:40) at getExports (/prismabug/node_modules/import-in-the-middle/lib/get-exports.js:76:12) at async processModule (/prismabug/node_modules/import-in-the-middle/hook.js:131:23) at async getSource (/prismabug/node_modules/import-in-the-middle/hook.js:249:23) at async load (/prismabug/node_modules/import-in-the-middle/hook.js:280:26) at async nextLoad (node:internal/modules/esm/hooks:864:22) { code: 'MODULE_NOT_FOUND', requireStack: [ '/prismabug/node_modules/import-in-the-middle/lib/get-exports.js', '/prismabug/node_modules/import-in-the-middle/hook.js' ]

This occurs when I install and use the prisma package in a subdirectory. Notably, the issue only arises with Node.js version 18.19 and above. Versions 18 and below do not exhibit this problem. Additionally, the issue does not occur when the import-in-the-middle hook is not used.

If I add the prismato the root package.json, the error does not occur. However, due to project compliance, I am not allowed to include the the package in the root directory.

Expected Behavior

The prisma package should be successfully imported and executed without errors. When I run the command node --loader ./hook.mjs packages/a/test/prisma/app.mjs it should not throw any error.

Actual Behavior

The error Cannot find module '.prisma/client/default' is thrown.

Steps to Reproduce the Problem

  1. Unzip prismabug.zip.
  2. Run npm i.
  3. Navigate to the Prisma test directory: cd packages/a/test/prisma/
  4. Run npm i within this directory.
  5. Return to the root directory: cd ../../../../
  6. Execute the following command node --loader ./hook.mjs packages/a/test/prisma/app.mjs or node --import ./register packages/a/test/prisma/app.mjs both are throwing errors.

    Specifications

    • Version: 1.8.0
    • Platform: arm64
    • Subsystem: Node 18.19
timfish commented 3 weeks ago

I'm currently looking into this!

Related: https://github.com/getsentry/sentry-javascript/issues/12325

aryamohanan commented 3 weeks ago

@timfish Thank you for responding promptly. Looking forward to your updates.

timfish commented 3 weeks ago

As Prisma is CJS, I think this is fixed by https://github.com/DataDog/import-in-the-middle/pull/96 but I need to confirm.

aryamohanan commented 2 weeks ago

Hello @timfish

I just used the latest version 1.8.1, but the issue still persists. I am now encountering the following error:

Error: 'import-in-the-middle' failed to wrap 'file:///artifacts/packages/collector/test/tracing/database/prisma/node_modules/@prisma/client/default.js' at getSource (/artifacts/packages/core/node_modules/import-in-the-middle/hook.js:304:21) at async load (/artifacts/packages/core/node_modules/import-in-the-middle/hook.js:319:26) at async nextLoad (node:internal/modules/esm/hooks:866:22) at async Hooks.load (node:internal/modules/esm/hooks:449:20) at async MessagePort.handleMessage (node:internal/modules/esm/worker:196:18) { cause: TypeError: emitImportAssertionWarning is not a function at defaultLoad (node:internal/modules/esm/load:119:5) at nextLoad (node:internal/modules/esm/hooks:866:28) at getExports (/artifacts/packages/core/node_modules/import-in-the-middle/lib/get-exports.js:80:27) at processModule (/artifacts/packages/core/node_modules/import-in-the-middle/hook.js:131:29) at getSource (/artifacts/packages/core/node_modules/import-in-the-middle/hook.js:269:31) at load (/artifacts/packages/core/node_modules/import-in-the-middle/hook.js:319:32) at nextLoad (node:internal/modules/esm/hooks:866:28) at Hooks.load (node:internal/modules/esm/hooks:449:26) at MessagePort.handleMessage (node:internal/modules/esm/worker:196:24) at [nodejs.internal.kHybridDispatch] (node:internal/event_target:825:20) } import { PrismaClient } from '@prisma/client'; ^^^^^^^^^^^^ SyntaxError: The requested module '@prisma/client' does not provide an export named 'PrismaClient' at ModuleJob._instantiate (node:internal/modules/esm/module_job:134:21) at async ModuleJob.run (node:internal/modules/esm/module_job:217:5) at async ModuleLoader.import (node:internal/modules/esm/loader:316:24) at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:123:5)

timfish commented 2 weeks ago

A warning is logged to the console but it no longer throws.

108 actually fixes the prisma issue

aryamohanan commented 6 days ago

@timfish, could you please let me know when the fix is released? Thank you.

jsumners-nr commented 6 days ago

https://github.com/nodejs/import-in-the-middle/releases https://github.com/nodejs/import-in-the-middle/releases.atom