#SWC tsconfig alias "cannot find module"

27 messages · Page 1 of 1 (latest)

signal nymph
#

hello, I'm trying to update to nestjs 10 and i want to use swc compiler, but when I'm building my docker container, i get error:
(same happens when i just run "yarn start:dev" -> nest start --watch -b swc)

my-app  | Error: Cannot find module './#AppModule'
my-app  | Require stack:
my-app  | - /app/apps/my-app/dist/main.js
my-app  |     at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)....

I found that when i change import from #AppModule to ./AppModule, error fixes, and i get different error that is related to # alias too, like example it cannot find module "#Utils" (where i import logger from Utils folder index.ts), there is my configs:

.swcrc:

{
  "$schema": "https://json.schemastore.org/swcrc",
  "module": {
    "type": "commonjs"
  },
  "jsc": {
    "target": "es2017",
    "parser": {
      "syntax": "typescript"
    },
    "baseUrl": "./",
    "paths": {
      "#*": ["src/*"]
    }
  },
  "minify": false
}

tsconfig.json:

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "esModuleInterop": true,
    "isolatedModules": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "paths": {
      "#*": ["src/*"]
    },
    "incremental": true,
    "resolveJsonModule": true,
    "skipLibCheck": true
  },
  "exclude": ["node_modules", "dist", "jest*.ts"],
  "include": ["src/**/*"]
}

nest-cli.json:

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "builder": "swc",
    "typeCheck": true
  }
}

And I have noticed that before using swc compiler, my dist was: my-app/dist/src/(my-app-files) and now my-app/dist/(my-app-files), maybe this could be related to the issue that I'm countering? And how to solve it?

naive leaf
#

same here 😖 up

storm fable
#

Try "baseUrl": "." and paths: "*": ["*", "src/*"]

signal nymph
#

in .swcrc?

storm fable
#

Doesn't matter. The config values for both are the same.

signal nymph
#

same error still

naive leaf
#

i have indentical issue, changing paths didnt help me too, maybe i should add something to my dockerfile or dockercompose.yml for using swc with nest?

storm fable
#

What is the error exactly? @signal nymph

signal nymph
storm fable
#

How can there be a # before the AppModule?

signal nymph
#

because i use alias imports, i configured it in tsconfig, and when i compile with tsc it works fine, and i import it like

#

because app module is in src folder

storm fable
#

And are you adding the alias behind what I gave you?

signal nymph
#

i added them to swcrc and nothing has changed

storm fable
#

Nest doesn't use the swcrc file, if you are doing nest start.

signal nymph
#

im doing nest start -d swc --watch

storm fable
#

The paths with baseUrl is your problem. Keep playing with them. You'll get it. 🙂

signal nymph
#

alrigt, and i found that when code compiles, import inside compiled code is:
const _AppModule = require("./#AppModule");
and not #AppModule, for some reason ./ comes before it, its related to baseUrl?

storm fable
#

Like I said, keep playing with those values. I use what I gave you above and all works fine.

naive leaf
#

its optional to pass that -d swc in start script? when i provide compiler in cli

storm fable
storm fable
naive leaf
storm fable
#

Correct