Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
3 of 15 tasks
siamahnaf opened this issue Jun 24, 2023 · 31 comments
Closed
3 of 15 tasks

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

siamahnaf opened this issue Jun 24, 2023 · 31 comments

Comments

@siamahnaf
Copy link

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

  • I don't know. Or some 3rd-party package
  • @nestjs/common
  • @nestjs/core
  • @nestjs/microservices
  • @nestjs/platform-express
  • @nestjs/platform-fastify
  • @nestjs/platform-socket.io
  • @nestjs/platform-ws
  • @nestjs/testing
  • @nestjs/websockets
  • Other (see below)

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

@siamahnaf siamahnaf added the needs triage This issue has not been looked into label Jun 24, 2023
@siamahnaf siamahnaf changed the title ReferenceError: Cannot access 'User' before initialization ReferenceError: Cannot access 'User' before initialization(SWC Errors) Jun 24, 2023
@micalevisk
Copy link
Member

Looks like your repository is private

@siamahnaf
Copy link
Author

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.

swc-project/swc#7577

@kamilmysliwiec
Copy link
Member

@siamahnaf
Copy link
Author

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

@siamahnaf
Copy link
Author

I already ask a question on swc GitHub discussion. Here is link-swc-project/swc#7577

@micalevisk
Copy link
Member

@siamahnaf for that one, see #11863

@siamahnaf
Copy link
Author

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

@siamahnaf
Copy link
Author

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
Copy link
Member

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
Copy link
Author

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

@siamahnaf siamahnaf reopened this Jun 26, 2023
@siamahnaf siamahnaf changed the title ReferenceError: Cannot access 'User' before initialization(SWC Errors) Cannot find module '*' (SWC Compilation Error) Jun 26, 2023
@kamilmysliwiec
Copy link
Member

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

@kamilmysliwiec kamilmysliwiec added needs clarification and removed needs triage This issue has not been looked into labels Jun 26, 2023
@siamahnaf
Copy link
Author

I already provide my git repository in my last comment!

@kamilmysliwiec
Copy link
Member

I can't reproduce your issue on my machine

image

@siamahnaf
Copy link
Author

But in my environment I am still seeing the error.

I am trying from a windows pc

Your environment?

@jmcdo29
Copy link
Member

jmcdo29 commented Jun 26, 2023

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
Copy link

@kamilmysliwiec
Copy link
Member

kamilmysliwiec commented Jun 27, 2023

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
Copy link
Author

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
Copy link
Member

jmcdo29 commented Jun 28, 2023

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
Copy link
Author

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

@kamilmysliwiec
Copy link
Member

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
Copy link

llong2195 commented Jun 28, 2023

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

@onselakin
Copy link

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
Copy link
Member

@onselakin can you please provide a minimum reproduction repository?

@siamahnaf
Copy link
Author

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
Copy link
Author

@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
Copy link

andrucz commented Jul 5, 2023

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

@toimc
Copy link

toimc commented Jul 7, 2023

same here

@linkthai
Copy link

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
Copy link

I got the same issue

@kamilmysliwiec
Copy link
Member

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

@nestjs nestjs locked and limited conversation to collaborators Jul 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants