当我安装SWC时,我收到这个错误:找不到模块‘Path-to-project/src/app.module’

我正在使用:

  • node JS:18.17.1
  • 雀巢: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

我try 更改基本URL,即创建.swcrc

推荐答案

通过将版本降级至@swc/core@1.3.78(@swc/cli保持最新版本,0.1.62)解决了类似问题.

Node.js相关问答推荐

链接Inbox类方法,范围在哪里以及为什么发生变化?

利用Gemini:通过Vertex AI还是通过Google/generative-ai?

带有apache Couch-db和Nano的推荐引擎:过滤特定用户的视图

为什么即使数据库确实更新了,此 POST 也无法解析并返回 200 代码?

Prisma 和 Nextjs:重新部署之前内容不会更新

如何配置 Nginx React 和 Express

每秒从套接字传来的数据有哪些存储方式?

Axios GET 返回不可读的响应

在express js模型中将js转换为Typescript 时Typescript 错误

如何在 mongoDB 中进行全动态搜索?

Web3.js 脚本在监听 CreatedPairs 时退出

搜索MongoDB条目的正确方法是'_id';在 node 中

Puppeteer 错误:未下载 Chromium 修订版

在 Node.js 与 Cron 作业(job)中设置间隔?

使用 Webpack 和 font-face 加载字体

create-react-app,安装错误(找不到命令)

chart.js 无法创建图表:无法从给定项目获取上下文

Passport 的 req.isAuthenticated 总是返回 false,即使我硬编码 done(null, true)

Node应用程序中相同npm包的两个版本

响应分块时获取整个响应正文?