Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1.9.1 is not working with ESM #785

Closed
P4sca1 opened this issue Jun 3, 2024 · 11 comments
Closed

Version 1.9.1 is not working with ESM #785

P4sca1 opened this issue Jun 3, 2024 · 11 comments

Comments

@P4sca1
Copy link

P4sca1 commented Jun 3, 2024

I added a second --import statement to initialize the Sentry SDK like so:
node --import ./instrument.js --import @swc-node/register/esm-register -r dotenv/config ./src/main.ts.

This results in the following error:

TypeError [Error]: Cannot read properties of undefined (reading 'swc')
    at load (file:///Users/pascal/code/ips-hosting/api/node_modules/.pnpm/@swc-node+register@1.9.1_@swc+core@1.5.24_@swc+helpers@0.3.17__@swc+types@0.1.7_typescript@5.4.5/node_modules/@swc-node/register/esm/esm.mjs:52:51)
    at nextLoad (node:internal/modules/esm/hooks:833:28)
    at Hooks.load (node:internal/modules/esm/hooks:416:26)
    at handleMessage (node:internal/modules/esm/worker:168:24)
    at Immediate.checkForMessages (node:internal/modules/esm/worker:117:28)
    at process.processImmediate (node:internal/timers:478:21)
@P4sca1
Copy link
Author

P4sca1 commented Jun 3, 2024

It turned out that adding the second --import flag was not the issue. The issue also occurs without it. I was using @swc-node/register version 1.9.1. After downgrading to 1.8.0, swc is working again.

@P4sca1 P4sca1 changed the title Using multiple --import not working Version 1.9.1 is not working with ESM Jun 3, 2024
@weyert
Copy link

weyert commented Jun 3, 2024

I think it might be fixed by this PR: #781

Are you sure downgrading to just v1.9.0 is not solving it for you?

Oh yeah, if you are using Node v20 or later you don't need the dotenv/config loader, you can use the built-in --env-file argument: https://nodejs.org/en/blog/release/v20.6.0#built-in-env-file-support

@P4sca1
Copy link
Author

P4sca1 commented Jun 5, 2024

Thank you for the tip that .env support is now built-in to Node.js. Totally missed that in the Changelog.
I can confirm that downgrading to 1.9.0 fixes the issue. However, when I add the second import flag again, I get a new error:

[nodemon] starting `node --env-file=.env --import @swc-node/register/esm-register --import ./instrument.js ./src/main.ts`
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/pascal/code/ips-hosting/api/src/main.ts
    at new NodeError (node:internal/errors:406:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:99:9)
    at defaultGetFormat (node:internal/modules/esm/get_format:142:36)
    at defaultLoad (node:internal/modules/esm/load:120:20)
    at nextLoad (node:internal/modules/esm/hooks:833:28)
    at load (file:///Users/pascal/code/ips-hosting/api/node_modules/.pnpm/@swc-node+register@1.8.0_@swc+core@1.5.24_@swc+helpers@0.3.17__@swc+types@0.1.7_typescript@5.4.5/node_modules/@swc-node/register/esm/esm.mjs:58:16)
    at nextLoad (node:internal/modules/esm/hooks:833:28)
    at load (/Users/pascal/code/ips-hosting/api/node_modules/.pnpm/import-in-the-middle@1.7.4/node_modules/import-in-the-middle/hook.js:343:12)
    at nextLoad (node:internal/modules/esm/hooks:833:28)
    at Hooks.load (node:internal/modules/esm/hooks:416:26) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'

@weyert
Copy link

weyert commented Jun 5, 2024

@P4sca1 Sorry, for keep asking you to try things. If I followed along a discussion on OpenTelemetry repos correctly. You might want to give import-in-the-middle version 1.8.0 a try. It's has a lot of fixes, see for fixes here:
nodejs/import-in-the-middle#91

@P4sca1
Copy link
Author

P4sca1 commented Jun 5, 2024

tried version 1.8.0 and it does not solve the issue.

@weyert
Copy link

weyert commented Jun 5, 2024

@P4sca1 It was worth a try. I am curious if these loaders need to be build in a special manner to support multiple loaders at the same time. I do think import-in-the-middle does a bit of code generation itself

@P4sca1
Copy link
Author

P4sca1 commented Jun 5, 2024

To verifiy wheter this is an issue with the SWC loader not supporting multiple loaders, I created a simple JavaScript file, which just logs to the console. I can confirm that it works together with the SWC loader without issues. So this is likely an issue with the Sentry SDK and import-in-the-middle.

@weyert
Copy link

weyert commented Jun 5, 2024

import-in-the-middle is becoming a nodejs/diagnostics-project [1] so that's exciting :)

[1] nodejs/diagnostics#634

@timfish
Copy link

timfish commented Jun 5, 2024

It could be an issue with import-in-the-middle or it could be the order of the loaders. Since iitm parses the loaded code and can't parse TypeScript, the order of the loaders matters. However, I've been unable to test it myself!

The following fails for me with Node v22 and @swc-node/register v1.8.0, v1.9.0 and v1.9.1

node --import @swc-node/register/esm-register ./test.ts

With the following error:

Error: Cannot find module '@swc/core'
Require stack:
- /Users/tim/Documents/Repositories/repro/node_modules/@swc-node/core/lib/index.js
- /Users/tim/Documents/Repositories/repro/node_modules/@swc-node/register/lib/register.js
    at Function._resolveFilename (node:internal/modules/cjs/loader:1186:15)
    at Function._load (node:internal/modules/cjs/loader:1012:27)
    at Module.require (node:internal/modules/cjs/loader:1271:19)
    at require (node:internal/modules/helpers:123:16)
    at Object.<anonymous> (/Users/tim/Documents/Repositories/repro/node_modules/@swc-node/core/lib/index.js:4:16)
    at Module._compile (node:internal/modules/cjs/loader:1434:14)
    at Object..js (node:internal/modules/cjs/loader:1518:10)
    at Module.load (node:internal/modules/cjs/loader:1249:32)
    at Function._load (node:internal/modules/cjs/loader:1065:12)
    at Module.require (node:internal/modules/cjs/loader:1271:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/tim/Documents/Repositories/repro/node_modules/@swc-node/core/lib/index.js',
    '/Users/tim/Documents/Repositories/repro/node_modules/@swc-node/register/lib/register.js'
  ]
}

@yeliex
Copy link
Contributor

yeliex commented Jun 6, 2024

I think #781 should have fixed the multiple import hooks problem, please try and feedback if you like

@P4sca1
Copy link
Author

P4sca1 commented Jun 7, 2024

@timfish I tested using Node v22.2.0, @swc-node/register 1.9.0, and import-in-the-middle 1.8.0. The order of the loaders indeed makes a difference. Importing the JavaScript module before the SWC loader solves the ERR_UNKNOWN_FILE_EXTENSION issue.
node --env-file=.env --import ./instrument.js --import @swc-node/register/esm-register ./src/main.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants