swc not resolving import path aliases
Asked Answered
C

2

8

I have a typescript project which I'm transpiling with swc. I'm having trouble getting swc to resolve path aliases defined in either tsconfig.json or .swcrc. The module aliases are resolved properly by eslint and when running ts-node when swc is not specified as the compiler.

Given this pull request, it seems that swc is both supposed to resolve tsconfig.json and has support for path aliases defined in .swcrc. However, it seems people experienced similar issues to mine following this feature, though it was two years ago and I can't imagine there hasn't been a solution since.

Also on a weird sort of side-note the author of the PR which implements TSConfigResolver that I linked earlier mentions in an issue a year after his PR was merged that swc "doesn't look at tsconfig". Though this shouldn't be the source of my problem though because I'm specifying the same path aliases in .swcrc

The swc documentation is horribly vague and doesn't mention anything about module aliases or resolving tsconfig, besides that jsc.baseUrl and jsc.paths are compilation options and they match tsconfig syntax.

Any help or guidance would be appreciated, thank you!

tsconfig.json

{
  "compilerOptions": {
    "target": "es2020",
    "module": "ESNext",
    "moduleResolution": "node",
    "baseUrl": "./",
    "paths": {
      "$lib/*": ["src/lib/*"]
    },
    "outDir": "./dist",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  },
  "ts-node": {
    "swc": true
  },
  "exclude": ["node_modules", "dist"]
}

.swcrc

{
  "$schema": "https://json.schemastore.org/swcrc",
  "jsc": {
    "baseUrl": "./",
    "paths": {
      "$lib/*": ["src/lib/*"]
    }
  },
  "module": {
    "type": "es6"
  }
}

package.json

{
  "type": "module",
  "devDependencies": {
    "@swc/cli": "^0.1.62",
    "@swc/core": "^1.3.35",
    "ts-node": "^10.9.1",
    "typescript": "^4.9.5"
  }
}

I receive the following error when running swc . -d ./dist from the project root directory:

CustomError: Cannot find package '$lib' imported from /root/projects/example/src/index.ts

Corkage answered 21/2, 2023 at 16:1 Comment(4)
did you have any luck finding an answer to this? I’m experiencing the same issueRammer
nothing yet unfortunately. I suspect it's a bug with swc's module resolution, or maybe I'm just misusing the config but the docs are not helpful atm. Apparently people have been experiencing similar issues for a long time. It might be worth a new github issue.Corkage
I experienced a similar issue. I also have my code inside src and transpiling into dist. In my tsconfig.json file im using this config: "baseUrl": "./src", "paths": { "@cli/*": ["./cli/*"],}. In .swcrc on the other side i need to specify the baseUrl where the transpilen code goes to. In my case dist. This gives me following config for .swcrc: "baseUrl": "dist", "paths": { "@cli/*": ["./cli/*"],}. I hope this helps you in any case.Corker
github.com/swc-project/swc-node/pull/666 apparently tsconfig paths is supported hrmManley
E
3

It was fixed in recent pull request. Make sure you specified your paths in both tsconfig.json and .swcrc https://github.com/swc-project/swc-node/pull/723

Effervesce answered 18/8, 2023 at 9:2 Comment(1)
This helped me. I set ./src in my .swcrc file and then same in tsconfig.json ,"baseUrl": "./src". It is workign when I run the app. It shows some terminal erros, but works fine. But If I try to npm run build, it fails and throws a large number of error. One of them is failed to handle: base_dir(./src) must be absolute. Please ensure that 'jsc.baseUrl' is specified correctly. This cannot be deduced by SWC itself because SWC is a transpiler and it does not try to resolve project details. In other works, SWC does not know which directory should be used as a base directory. Help!.Paraselene
D
0

To resolve Cannot find module when building typescript projects with SWC, you will have to add your baseUrl and paths to your .swcrc file as well.

For eg:

tsconfig.json

"baseUrl": "./",
"paths": {
  "*": ["node_modules", "src/*"]
},

This configuration from the tsconfig.json file should be also added to your swcrc file's jsc block.

jwcrc

"jsc": {
"parser": {
  "syntax": "typescript"
},
"baseUrl": "./",
"paths": {
  "*": ["node_modules", "src/*"]
}
},
Doityourself answered 10/9, 2023 at 4:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.