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
60 stars 22 forks source link

Cannot handle node import assertions #24

Closed jamesone closed 1 year ago

jamesone commented 1 year ago

Expected Behavior

Import assertions should be parsed correctly.

i.e. import contracts from './package.json' assert { type: 'json' };

Actual Behavior

When using the dd-trace/loader-hook.mjs, the nodejs server is unable to start due to the import-in-the-middle package failing to parse import assertions. Here is the related issue https://github.com/DataDog/dd-trace-js/issues/2221#issuecomment-1243122554

TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///Users/james/dev/myproject/build/package.json" needs an import assertion of type "json"
    at new NodeError (node:internal/errors:372:5)
    at validateAssertions (node:internal/modules/esm/assert:82:15)
    at defaultLoad (node:internal/modules/esm/load:24:3)
    at load (file:///Users/james/dev/myproject/node_modules/import-in-the-middle/hook.mjs:154:10)
    at ESMLoader.load (node:internal/modules/esm/loader:407:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:326:22)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:345:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:304:34)
    at async Promise.all (index 0) {
  code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING'
}

Note: When running node without the dd-trace/loader-hook.mjs, it runs fine (however, this issue still remains)

Steps to Reproduce the Problem

  1. node --experimental-specifier-resolution=node --experimental-loader dd-trace/loader-hook build/src/index.js - Use the dd-trace/loader-hook in an esm environment. In your build, ensure you have an import assertion, i.e. import contracts from './package.json' assert { type: 'json' };
  2. Run the node command above ^ and

Specifications

Further notes

tlhunter commented 1 year ago

I was able to reproduce this locally. Will be looking into it today.

jamesone commented 1 year ago

@tlhunter I can confirm this is working (I copied the change into my node_modules). Would you be able to update the dd-trace-js library to use the new version of import-in-the-middle? https://github.com/DataDog/dd-trace-js/issues/2221#issuecomment-1244794142 (related issue that will be fixed with this change)

I tried making the change to dd-trace myself but it looks like 1.3.4. is not yet available on npm.

tlhunter commented 1 year ago

@jamesone version 1.3.4 has just now been published! I'm now working on getting the change available in the next release of dd-trace.

tlhunter commented 1 year ago

We'll still probably release dd-trace soon, but I believe reinstalling the package should grab the latest version of import-in-the-middle since we don't pin the version in dd-trace:

    "import-in-the-middle": "^1.3.3",
tlhunter commented 1 year ago

FYI, new minimum version for import-in-the-middle has been released to dd-trace versions v2.16.1 and v3.3.1.

jamesone commented 1 year ago

Awesome @tlhunter I can confirm that the dd-trace issue was fixed by this change