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

Unable to use iitm ESM loader with @apollo/server #31

Closed barryhagan closed 8 months ago

barryhagan commented 12 months ago

Expected Behavior

Using import-in-the-middle/hook.mjs as a loader in an ESM project works when @apollo/server is used by the project.

Actual Behavior

The node application failed to start. The behavior varies based on the version of node in use.

Node v18: exit code 13 Node v20: exit code 1 with SyntaxError: Unexpected token '*'

Steps to Reproduce the Problem

  1. clone https://github.com/barryhagan/iitm-apollo-server-repro
  2. npm i
  3. npm build
  4. npm run start
barryhagan commented 12 months ago

Likely related to this: https://github.com/DataDog/dd-trace-js/issues/3595

bizob2828 commented 11 months ago

It appears the issue is in Node 20 the exports are obtained via AST parsing. Export is not a valid export it just exports all data from the file it's importing so wrapping `` causes the syntax error. I'm not sure of the best way to fix this because pre Node 20 this was a stateful wrap. @Qard and @bengl any thoughts on the best way to fix? It looks like this was discussed here. The test for this isn't as complete as it needs to be to expose the issue @barryhagan has logged

bizob2828 commented 11 months ago

Likely related to this: DataDog/dd-trace-js#3595

This does appear to be related in pre Node 20. However in Node 20 the proxying of exports outputs invalid syntax.

jsumners-nr commented 8 months ago

This should be resolved by #43.