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

4

7

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

I'm using:

  • nodejs: 18.17.1
  • nestjs: 10.1.16
  • @swc/cli: 0.1.62
  • @swc/core: 1.3.81

"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

Adamandeve answered 1/9, 2023 at 3:27 Comment(0)
S
7

Resolved similar issue by downgrading version to @swc/[email protected] (@swc/cli stayed latest, 0.1.62).

Selia answered 1/9, 2023 at 11:48 Comment(4)
I have the same issue but your solution didn't worked in my case – Hildegaard
Worked for me, thx! Do you know if there's a related issue on Nest or SWC repositories? – Scamander
OK, finally I add to open a new issue as they didn't want to reopen an existing one: github.com/swc-project/swc/issues/7990 – Scamander
Drama: kdy1 commented 4 hours ago Closing as this is not about the latest version @kdy1 kdy1 closed this as completed 4 hours ago @kdy1 kdy1 reopened this 4 hours ago @kdy1 kdy1 added this to the Planned milestone 2 hours ago ------ At least, tsc works good, just remove ` -b swc` from scripts.... – Selia
S
2

Downgrading to @swc/[email protected] worked for me

Shreeves answered 21/9, 2023 at 18:24 Comment(0)
C
2

Solution for me is to use absolute path instead of relative one:

For example: ../../app.module instead of path-to-project/src/app.module

Now starting point is not directory: path-to-project, but the file where you are importing file: app.module

Cronin answered 31/10, 2023 at 17:29 Comment(0)
D
1

Dont need to downgrading, this solution worked for me:

  1. create .swcrc in root folder:
{
  "$schema": "https://json.schemastore.org/swcrc",
  "sourceMaps": true,
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "decorators": true,
      "dynamicImport": true
    },
    "baseUrl": "./"
  },
  "minify": false
}
  1. change at nest-cli.json:
{
  "compilerOptions": {
    "tsConfigPath": "tsconfig.build.json",
    "builder": "swc",
    "deleteOutDir": true
  }
}
  1. tsconfig.json:
{
  "compilerOptions": {
    "paths": {
      "@src/*": ["src/*"]
    },
  }
}
  1. Replace all src/** at import line to @src/**
Dissect answered 20/2 at 17:7 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.