nest: Cannot find module '*' (SWC Compilation Error)

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I am trying to update my existing project to nestjs 10 for using 20x fast compiler. I set my swc in nest-cli.json-

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

And am I am getting a error like

ReferenceError: Cannot access 'User' before initialization

image

Minimum reproduction code

https://github.com/siamahnaf/test-project

Steps to reproduce

No response

Expected behavior

I don’t know what is the problem!

Package

Other package

No response

NestJS version

No response

Packages versions

{
  "name": "server",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "nest start",
    "start:dev": "nest start",
    "start:debug": "nest start --debug --watch",
    "start:prod": "node dist/main",
    "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
    "test": "jest",
    "test:watch": "jest --watch",
    "test:cov": "jest --coverage",
    "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
    "test:e2e": "jest --config ./test/jest-e2e.json"
  },
  "dependencies": {
    "@apollo/server": "^4.7.4",
    "@nestjs/apollo": "^12.0.3",
    "@nestjs/axios": "^3.0.0",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.0.0",
    "@nestjs/core": "^10.0.0",
    "@nestjs/graphql": "^12.0.3",
    "@nestjs/jwt": "^10.1.0",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/schedule": "^3.0.1",
    "@nestjs/typeorm": "^10.0.0",
    "@types/bcrypt": "^5.0.0",
    "@types/speakeasy": "^2.0.7",
    "axios": "^1.4.0",
    "bcrypt": "^5.1.0",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "cookie-parser": "^1.4.6",
    "geoip-lite": "^1.4.7",
    "graphql": "^16.7.1",
    "graphql-pg-subscriptions": "^2.0.5",
    "nestjs-typeorm-paginate": "^4.0.4",
    "next-useragent": "^2.8.0",
    "pg": "^8.11.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.8.1",
    "speakeasy": "^2.0.0",
    "typeorm": "^0.3.17"
  },
  "devDependencies": {
    "@nestjs/cli": "^10.0.0",
    "@nestjs/schematics": "^10.0.0",
    "@nestjs/testing": "^10.0.0",
    "@swc/cli": "^0.1.62",
    "@swc/core": "^1.3.66",
    "@types/cookie-parser": "^1.4.3",
    "@types/cron": "^2.0.1",
    "@types/express": "^4.17.17",
    "@types/geoip-lite": "^1.4.1",
    "@types/jest": "^29.5.2",
    "@types/node": "^20.3.1",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^5.59.11",
    "@typescript-eslint/parser": "^5.59.11",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-prettier": "^4.2.1",
    "jest": "^29.5.0",
    "prettier": "^2.8.8",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "^29.1.0",
    "ts-loader": "^9.4.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "^4.2.0",
    "typescript": "^5.1.3"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}

Node.js version

18.16.0

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 3
  • Comments: 31 (12 by maintainers)

Most upvoted comments

@kamilmysliwiec , I have a question!

I open a file from dist folder, that was already build.

image

In the picture I can I see that-

const _studententity = require("G:/Codestation21/e-campus/server/src/student/model/student.entity");

SWC compile path like above. Why swc compile in this way where default typescript compiler compile like-

const _studententity = require("../student/model/student.entity");

Why swc give the full path of the file when compilation?

SWC won’t work on web containers (stackblitz/codesandbox), just FYI.

@kamilmysliwiec correect meif I’m wrong here, but Nest doesn’t do anything special to the config or resolution when using SWC, we just pass it directly to swc for transpilation, unlike with tsc how we hook into tsconfig-paths, correct?

In the latest version of @nestjs/cli, we copy the paths and baseUrl configuration values from tsconfig.json to .swcrc, but other than that yeah, no changes/hooks