nestjs / nest

A progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript/JavaScript 🚀
https://nestjs.com
MIT License
66.91k stars 7.55k forks source link

Cannot find module '*' (SWC Compilation Error) #11891

Closed siamahnaf closed 1 year ago

siamahnaf commented 1 year ago

Is there an existing issue for this?

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?

Other

No response

micalevisk commented 1 year ago

Looks like your repository is private

siamahnaf commented 1 year ago

I can solve existing problem. But I am facing another issue. I already plaace a question on discussion into swc. Can you please see the question if you can please.

https://github.com/swc-project/swc/discussions/7577

kamilmysliwiec commented 1 year ago

https://docs.nestjs.com/recipes/swc#common-pitfalls

siamahnaf commented 1 year ago

Yes, I can solve it now. But I am seeing import issue. Could you please help on that?

siamahnaf commented 1 year ago

I already ask a question on swc GitHub discussion. Here is link-https://github.com/swc-project/swc/discussions/7577

micalevisk commented 1 year ago

@siamahnaf for that one, see #11863

siamahnaf commented 1 year ago

I already see this issues. But I can't figure out?

siamahnaf commented 1 year ago

my folder structure is- image

in src folder I have structure like-

src > student > model > student.entity src > user > model > user.entity

I import user.entity into student.entity like -

import { User } from "@/user/model/user.entity";

I am getting an error like-

image

I create a .swcrc file like-

{
    "$schema": "https://json.schemastore.org/swcrc",
    "jsc": {
        "baseUrl": "./"
    }
}

and my tsconfig.json-

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "paths": {
      "@/*": [
        "./src/*"
      ]
    },
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
  }
}

Where I am wrong?

kamilmysliwiec commented 1 year ago

In @nestjs/cli@10.0.3 SWC paths & baseUrl had to be specified manually in the .swcrc file.

In @nestjs/cli@10.0.4 paths & baseUrl will be auto-populated from the tsconfig.json

siamahnaf commented 1 year ago

Hello, @kamilmysliwiec

I update @nestjs/cli to version 10.0.4. But I am still getting an error.

here is my tsconfig-

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": false,
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false
  }
}

here is my nest-cli.json-

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

I do not create any .swcrc as you say it will be automatically!

Here is my project link-https://github.com/siamahnaf/swc-error

Here is the error-

image

I am on the windows

kamilmysliwiec commented 1 year ago

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

siamahnaf commented 1 year ago

I already provide my git repository in my last comment!

kamilmysliwiec commented 1 year ago

I can't reproduce your issue on my machine

image
siamahnaf commented 1 year ago

But in my environment I am still seeing the error.

I am trying from a windows pc

Your environment?

jmcdo29 commented 1 year ago

This is most likely an swc issue and not related to NestJS directly.

@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?

llong2195 commented 1 year ago

i try it but not work https://stackblitz.com/edit/nestjs-typescript-starter-xdrtdr

kamilmysliwiec commented 1 year ago

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

siamahnaf commented 1 year ago

This is most likely an swc issue and not related to NestJS directly.

Your highlighted issues all are closed as solve. But there is no proper solution. SWC said that they buy a windows pc and try to reproduce the issue. They don't find any issue, that's why they close the issue. But with nestjs we facing the same issue.

jmcdo29 commented 1 year ago

It's a problem with swc on Windows. In unix environments or works as expected. If I had a Windows name I would try to figure out what swc configuration to change to get it to work

siamahnaf commented 1 year ago

Okay, thank you. Should I create a issue on SWC?

kamilmysliwiec commented 1 year ago

I just double-checked and it seems to be working as expected (windows)

@siamahnaf can you try to reproduce this issue with swc cli directly?

llong2195 commented 1 year ago

@kamilmysliwiec when i override path in .swcrc, build not work image ERR image

onselakin commented 1 year ago

Just to chime in on the discussion, I'm facing a similar issue with my project. When I turn off SWC, the module aliases seem to work perfectly, but the moment I switch it back on, everything falls apart. Here's my setup:

nest-cli.json:

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

tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "baseUrl": "./",
    "declaration": true,
    "emitDecoratorMetadata": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "incremental": true,
    "module": "commonjs",
    "moduleResolution": "Node",
    "noFallthroughCasesInSwitch": false,
    "noImplicitAny": false,
    "outDir": "./dist",
    "paths": {
      "@/*": ["src/*"]
    },
    "removeComments": true,
    "rootDir": "./src",
    "skipLibCheck": true,
    "sourceMap": true,
    "strict": true,
    "strictBindCallApply": false,
    "strictNullChecks": false,
    "target": "ES2017"
  }
}

Running start:dev, I get the following error:

⠋ TSC Initializing type checker...Debugger attached. Error: Cannot find module '@/profile/profile.module'

Which originates from this line in the compiled JS:

const _profilemodule = require("@/profile/profile.module");

Disabling SWC and switching to tsc produces the correct path:

const profile_module_1 = require("../profile/profile.module");
kamilmysliwiec commented 1 year ago

@onselakin can you please provide a minimum reproduction repository?

siamahnaf commented 1 year ago

Exactly same issue I am facing in my project. But it is working as expected in @kamilmysliwiec environment! I am now totally puzzled what's going on! Someone says this is the issue from swc(From Discord).

@kamilmysliwiec, Should I create a new issue in swc.

siamahnaf commented 1 year ago

@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?

andrucz commented 1 year ago

@siamahnaf I just ran into the same issue trying to build my project on Windows. It works on my Mac.

toimc commented 1 year ago

same here

linkthai commented 1 year ago

I'm running it on Mac and got the same error, it's a problem on an .entity file too, I'm using MikroORM.

duyphamsalto commented 1 year ago

I got the same issue

kamilmysliwiec commented 1 year ago

Please, report this issue in the SWC repository https://github.com/swc-project/swc