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.9k stars 7.56k forks source link

SWC Error: Cannot find module library #12771

Closed maksimdrosdov closed 10 months ago

maksimdrosdov commented 10 months ago

Is there an existing issue for this?

Current behavior

nest g library sms

nest-cli file

{
  "$schema": "https://json.schemastore.org/nest-cli",
  "collection": "@nestjs/schematics",
  "sourceRoot": "src",
  "compilerOptions": {
    "builder": "swc",
    "typeCheck": true,
    "assets": [
      {
        "include": "locales/**/*",
        "watchAssets": true
      }
    ]
  },
  "projects": {
    "sms": {
      "type": "library",
      "root": "libs/sms",
      "entryFile": "index",
      "sourceRoot": "libs/sms/src",
      "compilerOptions": {
        "tsConfigPath": "libs/sms/tsconfig.lib.json"
      }
    }
  }
}

tsconfig file

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

tsconfig libs file

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "declaration": true,
    "outDir": "../../dist/libs/sms"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}

libs folder does not compile to dist

i get error

> nest start --watch
>  SWC  Running...
-  TSC  Initializing type checker...
Successfully compiled: 579 files with swc (496.86ms)
Watching for file changes.
Error: Cannot find module '../libs/sms/src'
Require stack:
- /usr/src/app/dist/app.module.js
- /usr/src/app/dist/main.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1053:15)
    at Function.Module._load (node:internal/modules/cjs/loader:898:27)
    at Module.require (node:internal/modules/cjs/loader:1120:19)
    at require (node:internal/modules/helpers:112:18)
    at Object.<anonymous> (/usr/src/app/dist/app.module.js:57:14)
    at Module._compile (node:internal/modules/cjs/loader:1239:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1293:10)
    at Module.load (node:internal/modules/cjs/loader:1096:32)
    at Function.Module._load (node:internal/modules/cjs/loader:935:12)
    at Module.require (node:internal/modules/cjs/loader:1120:19)
✔  TSC  Initializing type checker...

Minimum reproduction code

none

Steps to reproduce

No response

Expected behavior

none

Package

Other package

No response

NestJS version

10.1.2

Packages versions

{
  "name": "florachain",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"libs/**/*.ts\"",
    "start": "npx prisma generate && nest start",
    "start:dev": "nest start --watch",
    "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": {
    "@aws-sdk/client-s3": "^3.374.0",
    "@aws-sdk/types": "^3.370.0",
    "@bull-board/api": "^5.7.1",
    "@bull-board/express": "^5.7.1",
    "@bull-board/nestjs": "^5.7.1",
    "@casl/ability": "^6.5.0",
    "@elastic/elasticsearch": "^8.8.1",
    "@googlemaps/google-maps-services-js": "^3.3.34",
    "@maxmind/geoip2-node": "^4.2.0",
    "@nestjs/axios": "^3.0.0",
    "@nestjs/bull": "^10.0.1",
    "@nestjs/common": "^10.1.2",
    "@nestjs/config": "^3.0.0",
    "@nestjs/core": "^10.1.2",
    "@nestjs/cqrs": "^10.0.3",
    "@nestjs/elasticsearch": "^10.0.1",
    "@nestjs/jwt": "^10.1.0",
    "@nestjs/passport": "^10.0.0",
    "@nestjs/platform-express": "^10.1.2",
    "@nestjs/platform-socket.io": "^10.1.2",
    "@nestjs/swagger": "^7.1.2",
    "@nestjs/throttler": "^4.2.1",
    "@nestjs/websockets": "^10.1.2",
    "@prisma/client": "^5.6.0",
    "@sentry/node": "^7.60.0",
    "@sentry/tracing": "^7.60.0",
    "@types/socket.io": "^3.0.2",
    "@types/uuid": "^9.0.2",
    "agentkeepalive": "^4.3.0",
    "axios": "^1.4.0",
    "bcrypt": "^5.1.1",
    "bull": "^4.10.4",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "cookie-parser": "^1.4.6",
    "ioredis": "^5.3.2",
    "moment": "^2.29.4",
    "ms": "^2.1.3",
    "nestjs-i18n": "^10.2.6",
    "passport-jwt": "^4.0.1",
    "qrcode": "^1.5.3",
    "reflect-metadata": "^0.1.13",
    "retry-axios": "^3.0.0",
    "rimraf": "^5.0.1",
    "rxjs": "^7.8.1",
    "sharp": "^0.32.5",
    "slugify": "^1.6.6",
    "smsaero": "^2.0.0",
    "twilio": "^4.14.0",
    "uuid": "^9.0.0"
  },
  "devDependencies": {
    "@faker-js/faker": "^8.0.2",
    "@nestjs/cli": "^10.1.10",
    "@nestjs/schematics": "^10.0.1",
    "@nestjs/testing": "^10.1.2",
    "@swc/cli": "^0.1.62",
    "@swc/core": "^1.3.74",
    "@types/bcrypt": "^5.0.0",
    "@types/cookie-parser": "^1.4.3",
    "@types/express": "^4.17.17",
    "@types/jest": "29.5.3",
    "@types/multer": "^1.4.7",
    "@types/node": "^20.5.3",
    "@types/passport-jwt": "^3.0.9",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^6.2.0",
    "@typescript-eslint/parser": "^6.2.0",
    "eslint": "^8.45.0",
    "eslint-config-prettier": "^8.8.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "29.6.1",
    "prettier": "^3.0.0",
    "prisma": "^5.6.0",
    "source-map-support": "^0.5.21",
    "supertest": "^6.3.3",
    "ts-jest": "29.1.1",
    "ts-loader": "^9.4.4",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "4.2.0",
    "typescript": "^5.1.6"
  },
  "jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": ".",
    "testRegex": ".*\\.spec\\.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "collectCoverageFrom": [
      "**/*.(t|j)s"
    ],
    "coverageDirectory": "./coverage",
    "testEnvironment": "node",
    "roots": [
      "<rootDir>/src/",
      "<rootDir>/libs/"
    ],
    "moduleNameMapper": {
      "^@app/sms(|/.*)$": "<rootDir>/libs/sms/src/$1"
    }
  },
  "prisma": {
    "seed": "ts-node prisma/seed.ts"
  }
}

Node.js version

20.8.0

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 10 months ago

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

maksimdrosdov commented 10 months ago

@kamilmysliwiec sent you invite to repo

kamilmysliwiec commented 10 months ago

Please, create a public minimum reproduction repository.

maksimdrosdov commented 10 months ago

Please, create a public minimum reproduction repository.

public repo

kamilmysliwiec commented 10 months ago

nest g library sms converts your project to a monorepo

For monorepos, you should use the swc-loader as shown in the documentation https://docs.nestjs.com/recipes/swc#monorepo