swc: `baseUrl` not working in Nest 10 with `swc`

Describe the bug

When I run npm run start (nest start) in my project with NestJs 10 and swc enabled, an error is thrown:

> test-nestjs@0.0.1 start
> nest start

โœ”  TSC  Initializing type checker...
>  TSC  Found 0 issues.
>  SWC  Running...
Successfully compiled: 5 files with swc (26.57ms)
Error: Cannot find module 'app.module'
Require stack:
- /home/kirian/Projects/Perso/test-nestjs/dist/main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Function.Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/home/kirian/Projects/Perso/test-nestjs/dist/main.js:6:20)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Function.Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)

Input code

No response

Config

{
    "$schema": "https://json.schemastore.org/swcrc",
    "sourceMaps": true,
    "jsc": {
        "parser": {
            "syntax": "typescript",
            "decorators": true,
            "dynamicImport": true
        },
        "baseUrl": "./src"
    },
    "minify": false
}

Playground link

https://github.com/KirianCaumes/nestjs-bug-swc-import

Expected behavior

It should work the same way as without swc enabled and with baseUrl being used.

// src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from 'app.module'; // ๐Ÿ‘ˆ

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(5002);
}
bootstrap();
// nest-cli.json
{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "builder": "swc",
    "typeCheck": true
  }
}
// tsconfig.json
{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./src",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}
//package.json
{
  "scripts": {
    "start": "nest start",
    "start:dev": "nest start --watch",
    "start:debug": "nest start --debug --watch"
  },
  "dependencies": {
    "@nestjs/common": "^10.0.3",
    "@nestjs/core": "^10.0.3",
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.5",
    "@swc/cli": "^0.1.62",
    "@swc/core": "^1.3.67",
    "typescript": "^5.1.6"
  },
}

Actual behavior

Import with non-relative modules are not found

Version

1.3.67

Additional context

I already posted an issue on NestJs, but they redirected me here.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 5
  • Comments: 56 (20 by maintainers)

Most upvoted comments

I told you.

I changed .swcrc ,paths: [ ] settings then I deleted node_modules and package-lock.json and then run npm i. Now itโ€™s working Thanks.

@KirianCaumes Iโ€™m working on a fix for it with https://github.com/swc-project/swc/pull/7852

It now works perfectly with version 1.3.80! I also tested it with a larger NestJs project, and I did not find any issue at all. Thank you so much for your time an effort @kdy1! ๐Ÿ‘

@KirianCaumes have you managed to make it work?

@KirianCaumes I donโ€™t find the duplicate neither