node.jsnestjsswc

nestjs swc Error: Cannot find module 'path-to-project/src/app.module'


When I installed the swc, I get this Error: Cannot find module 'path-to-project/src/app.module'

I'm using:

"dist/main.js"

"use strict";
Object.defineProperty(exports, "__esModule", {
  value: true,
});
const _cors = /*#__PURE__*/ _interop_require_default(require("@fastify/cors"));
const _csrfprotection = /*#__PURE__*/ _interop_require_default(
  require("@fastify/csrf-protection"),
);
const _helmet = /*#__PURE__*/ _interop_require_default(
  require("@fastify/helmet"),
);
const _common = require("@nestjs/common");
const _config = require("@nestjs/config");
const _core = require("@nestjs/core");
const _platformfastify = require("@nestjs/platform-fastify");
const _swagger = require("@nestjs/swagger");
const _appmodule = require("path-to-projects/src/app.module");
function _interop_require_default(obj) {
  return obj && obj.__esModule
    ? obj
    : {
        default: obj,
      };
}
async function bootstrap() {
  const app = await _core.NestFactory.create(
    _appmodule.AppModule,
    new _platformfastify.FastifyAdapter(),
  );
  app.useGlobalPipes(
    new _common.ValidationPipe({
      transform: true,
      whitelist: true,
    }),
  );
  const config = app.get(_config.ConfigService);
  {
    const options = new _swagger.DocumentBuilder()
      .setTitle(config.get("API_TITLE"))
      .setDescription(config.get("API_DESCRIPTION"))
      .setVersion(config.get("API_VERSION"))
      .build();
    const document = _swagger.SwaggerModule.createDocument(app, options);
    _swagger.SwaggerModule.setup("docs", app, document);
  }
  await app.register(_cors.default);
  await app.register(_csrfprotection.default);
  await app.register(_helmet.default);
  await app.listen(config.get("API_PORT"), "0.0.0.0");
  console.log(`🚀 Application is running on: ${await app.getUrl()}`);
}
bootstrap();

"src/main.ts"

import fastifyCors from "@fastify/cors";
import fastifyCsrf from "@fastify/csrf-protection";
import fastifyHelmet from "@fastify/helmet";
import { ValidationPipe } from "@nestjs/common";
import { ConfigService } from "@nestjs/config";
import { NestFactory } from "@nestjs/core";
import {
  FastifyAdapter,
  NestFastifyApplication,
} from "@nestjs/platform-fastify";
import { DocumentBuilder, SwaggerModule } from "@nestjs/swagger";
import { AppModule } from "./app.module";

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );
  app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }));
  const config = app.get(ConfigService);

  {
    const options = new DocumentBuilder()
      .setTitle(config.get("API_TITLE"))
      .setDescription(config.get("API_DESCRIPTION"))
      .setVersion(config.get("API_VERSION"))
      .build();
    const document = SwaggerModule.createDocument(app, options);
    SwaggerModule.setup("docs", app, document);
  }

  await app.register(fastifyCors);
  await app.register(fastifyCsrf);
  await app.register(fastifyHelmet);
  await app.listen(config.get("API_PORT"), "0.0.0.0");
  console.log(`🚀 Application is running on: ${await app.getUrl()}`);
}
bootstrap();

"package.json"

{
  "name": "test",
  "version": "0.0.1",
  "description": "",
  "author": "",
  "private": true,
  "license": "UNLICENSED",
  "scripts": {
    "prebuild": "rimraf dist",
    "build": "nest build",
    "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
    "start": "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": {
    "@fastify/cors": "^8.3.0",
    "@fastify/csrf-protection": "^6.3.0",
    "@fastify/helmet": "^11.0.0",
    "@fastify/static": "^6.10.2",
    "@nestjs/common": "^10.0.0",
    "@nestjs/config": "^3.0.1",
    "@nestjs/core": "^10.0.0",
    "@nestjs/platform-express": "^10.0.0",
    "@nestjs/platform-fastify": "^10.2.4",
    "@nestjs/swagger": "^7.1.10",
    "@prisma/client": "^5.2.0",
    "bcrypt": "^5.1.1",
    "class-transformer": "^0.5.1",
    "class-validator": "^0.14.0",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^5.0.1",
    "rxjs": "^7.8.1"
  },
  "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.81",
    "@types/bcrypt": "^5.0.0",
    "@types/express": "^4.17.17",
    "@types/jest": "^29.5.2",
    "@types/node": "^20.3.1",
    "@types/supertest": "^2.0.12",
    "@typescript-eslint/eslint-plugin": "^6.0.0",
    "@typescript-eslint/parser": "^6.0.0",
    "eslint": "^8.42.0",
    "eslint-config-prettier": "^9.0.0",
    "eslint-plugin-prettier": "^5.0.0",
    "jest": "^29.5.0",
    "prettier": "^3.0.0",
    "prisma": "^5.2.0",
    "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"
  },
  "prisma": {
    "schema": "src/database/prisma/schema.prisma"
  }
}

"tsconfig.json"

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

"tsconfig.build.json"

{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

"nest-cli.json"

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

structure

I tried to change the base Url, create .swcrc


Solution

  • Resolved similar issue by downgrading version to @swc/core@1.3.78 (@swc/cli stayed latest, 0.1.62).