Skip to content

Crash when using import-in-the-middle with module.exports = {...require('$dependency')} #157

Open
@juan-fernandez

Description

@juan-fernandez

Expected Behavior

Using a myloader.mjs file like this

import * as module from 'module'

module.register('import-in-the-middle/hook.mjs', import.meta.url)

and using NODE_OPTIONS='--import ./myloader.mjs' $my_command does not crash.

Actual Behavior

Running

NODE_OPTIONS='--import ./myloader.mjs' npm run test:vitest

results in

(node:81156) Error: 'import-in-the-middle' failed to wrap 'file:///Users/juan.fernandezdealba/Desktop/dev/starter-kit-juan-fork/node_modules/@prisma/client/default.js'

Steps to Reproduce the Problem

  1. Clone my starter-kit fork:
git clone [email protected]:juan-fernandez/starter-kit.git
cd starter-kit
  1. Install dependencies
npm i
  1. Run vitest test command with --import flag:
NODE_OPTIONS='--import ./myloader.mjs' npm run test:vitest

See this error:

(node:81156) Error: 'import-in-the-middle' failed to wrap 'file:///Users/juan.fernandezdealba/Desktop/dev/starter-kit-juan-fork/node_modules/@prisma/client/default.js'

Specifications

Running npx envinfo

  System:
    OS: macOS 14.7
    CPU: (10) arm64 Apple M1 Max
  Binaries:
    Node: 20.17.0 - ~/.volta/tools/image/node/20.17.0/bin/node
    npm: 10.8.2 - ~/.volta/tools/image/node/20.17.0/bin/npm

Investigation

If you look at node_modules/@prisma/client/default.js the file looks like this:

module.exports = {
  ...require('.prisma/client/default'),
}

Funnily enough, if you change the file contents to this:

const res = require('.prisma/client/default')
module.exports = {
  ...res,
}

the crash does not happen anymore.

Context

DataDog/dd-trace-js#4713

Extra repro case

https://github.com/juan-fernandez/iitm-issue-repro is lighter. It does not throw the same exact error but seems to be caused with a very similar setup, so it's likely to be related

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions