網站搜尋

typescript - 使用 yarn berry 工作區單倉儲運行 nestjs 應用程序


我想使用 yarn berry 工作區配置 monorepo。我使用 CLI 在 packages/mono-api-server 資料夾中建立了一個 Nest 應用程式,命令是 nest g application .,以下是我的檔案樹

.
├── .pnp.cjs
├── .pnp.loader.mjs
├── .yarn
│   ├── cache
│   │   └── many zip files..
│   ├── install-state.gz
│   ├── plugins
│   │   └── @yarnpkg
│   ├── releases
│   │   └── yarn-3.3.0.cjs
│   └── unplugged
│       ├── @nestjs-core-virtual-c9663b0b91
│       ├── fsevents-patch-2882183fbf
│       └── node-gyp-npm-9.3.0-21c41a4dfd
├── .yarnrc.yml
├── dist
│   └── compiled files...
├── package.json
├── packages
│   └── mono-api-server
│       ├── .eslintrc.js
│       ├── .prettierrc
│       ├── README.md
│       ├── nest-cli.json
│       ├── package.json
│       ├── src
│       ├── tsconfig.build.json
│       └── tsconfig.json
├── tsconfig.build.json
├── tsconfig.json
└── yarn.lock

當 dist 資料夾位於 packages/mono-api-server 下時,nest 應用程式啟動正常,但我想將它移到根目錄。因此,我在 packages/mono-api-server 的 tsconfig.json 中將 "outDir" 從 "./dist" 配置為 "../../dist/mono-api-server"。然後它導致以下錯誤

/Users/cain/cain/monorepo/.pnp.cjs:17444
      Error.captureStackTrace(firstError);
            ^
Error: Your application tried to access @nestjs/core, but it isn't declared in your dependencies; this makes the require call ambiguous and unsound.

Required package: @nestjs/core
Required by: /Users/cain/cain/monorepo/dist/mono-api-server/

Require stack:
- /Users/cain/cain/monorepo/dist/mono-api-server/main.js
    at Function.require$$0.Module._resolveFilename (/Users/cain/cain/monorepo/.pnp.cjs:17444:13)
......

我懷疑只有 @nestjs/core 無法被加載,所以我嘗試在它之上導入其他東西,比如 lodash。

import _ from 'lodash';
console.log("hi");
import { NestFactory } from '@nestjs/core';

它運作正常,直到遇到 import { NestFactory } from '@nestjs/core'; 並且打印出 "hi"

所以我的假設是,既然 @nestjs-core-virtual-c9663b0b91 位於 .yarn/unplugged 資料夾下,它應該需要單獨配置。

我猜在 .yarnrc.yml 中配置 packagesExtension 可能是一個解決方案。但我不知道如何設置它。

有沒有人可以幫我運行 NestJS 應用程式嗎?上傳 package.json 作為參考。

packages/mono-api-server 中的 package.json

{
  "name": "mono-api-server",
  "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": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/core": "^9.0.0",
    "@nestjs/platform-express": "^9.0.0",
    "lodash": "^4.17.21",
    "reflect-metadata": "^0.1.13",
    "rimraf": "^3.0.2",
    "rxjs": "^7.2.0"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "28.1.4",
    "@types/node": "^16.0.0",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "28.1.2",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "28.0.5",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.0.0",
    "tsconfig-paths": "4.0.0",
    "typescript": "~4.8.4"
  }
}

根目錄下的 package.json

{
  "packageManager": "[email ",
  "workspaces": [
    "packages/*"
  ]
}

答案#1

你有試過將你的 dist 資料夾放在 packages/mono-api-server 資料夾內嗎?這樣做的話,當它匯入 @next/core 時,就不會被認為是你根工作區的依賴了。