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

@swc-node/register@1.8.0 /esm-register does not recognise tsconfig.json paths #753

Closed
mylesj opened this issue Feb 5, 2024 · 7 comments · Fixed by #754
Closed

@swc-node/register@1.8.0 /esm-register does not recognise tsconfig.json paths #753

mylesj opened this issue Feb 5, 2024 · 7 comments · Fixed by #754

Comments

@mylesj
Copy link

mylesj commented Feb 5, 2024

I was playing with the latest and greatest and notice that when running via:

node --import @swc-node/register/esm-register src/index.ts

tsconfig.json path aliases are not recognised, e.g.

{
  "compilerOptions": {
    "paths": {
      "@/*": ["./src/*"]
    }
  },
}

minimal reproducible example

Stack trace
node:internal/process/esm_loader:34
      internalBinding('errors').triggerUncaughtException(
                                ^
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/src/common' imported from /Users/myles/dev/issue-swc-node-1/src/index.ts
    at finalizeResolution (node:internal/modules/esm/resolve:264:11)
    at moduleResolve (node:internal/modules/esm/resolve:917:10)
    at defaultResolve (node:internal/modules/esm/resolve:1130:11)
    at nextResolve (node:internal/modules/esm/hooks:865:28)
    at resolve (file:///Users/myles/dev/issue-swc-node-1/node_modules/.pnpm/@swc-node+register@1.8.0_@swc+core@1.3.107_@swc+types@0.1.5_typescript@5.3.3/node_modules/@swc-node/register/esm/esm.mjs:44:12)
    at nextResolve (node:internal/modules/esm/hooks:865:28)
    at Hooks.resolve (node:internal/modules/esm/hooks:303:30)
    at MessagePort.handleMessage (node:internal/modules/esm/worker:196:24)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:826:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28) {
  code: 'ERR_MODULE_NOT_FOUND',
  url: 'file:///src/common'
}

Also of note: I am aware that native ESM would typically expect file-extensions to be specified in the import, but including them doesn't help in this case - though I think the expectation of swc would be not to care either way?

yeliex added a commit to yeliex/swc-node that referenced this issue Feb 5, 2024
this commit convert fileUrl to path before compile in esm, this may cause path alias resolve issue

close swc-project#753
@yeliex
Copy link
Contributor

yeliex commented Feb 5, 2024

it is a bug caused by #722

@Brooooooklyn maybe we should consider revert it temporarily, it is a major breaking change and should add more test cases. it caused many unexpected results about resolve and compile. relate: #752 #734 #727.

i tried to fix these issues (#754) but cause more complication issues with @swc/core

@cm-ayf #727 (comment)

@galaxy-s10
Copy link

same problem

@c3cris
Copy link

c3cris commented Feb 23, 2024

It might be the same as #758

@alfaproject
Copy link

I've just stumbled upon this )':

@cghiurea
Copy link

cghiurea commented Mar 1, 2024

I've just stumbled upon this )':

@alfaproject, you could use this node --watch --no-warnings=ExperimentalWarning --loader ts-node/esm src/server.ts as it will work depending on your project.

@alfaproject
Copy link

Well, TS paths resolution doesn't work and that's why I was trying swc-node to begin with:

❯ node --no-warnings=ExperimentalWarning --loader ts-node/esm ./apps/lambda-api/src/extract-schema.ts

node:internal/process/esm_loader:34
      internalBinding('errors').triggerUncaughtException(
                                ^
Error: Cannot find module '/Users/joaodias/the-mill/lambda-gaming.nosync/apps/lambda-api/src/schemas/schema' imported from /Users/joaodias/the-mill/lambda-gaming.nosync/apps/lambda-api/src/extract-schema.ts

@yeliex
Copy link
Contributor

yeliex commented Mar 3, 2024

#754 should fix this, i would test later. feedback please.

@kdy1 kdy1 closed this as completed in #754 Mar 4, 2024
kdy1 pushed a commit that referenced this issue Mar 4, 2024
* fix: convert fileUrl to path before compile

this commit convert fileUrl to path before compile in esm, this may cause path alias resolve issue

close #753

* Create changeset

* chore: update test:module to use node --import

* fix: remove baseUrl from esm to keep module import specifier, cause it use tsc resolver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

6 participants