typescript 用yarn berry workspace monorepo运行nestjs应用程序

brtdzjyr  于 2023-06-07  发布在  TypeScript
关注(0)|答案(1)|浏览(319)

我想用yarn berry工作区配置monorepo。我用cli在packages/mono-api-server文件夹中创建了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中从“./dist”配置“outDir”tsconfig.json到“../../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-c9663 b 0 b 91**在.yarn/unplugged文件夹下,因此应该单独配置。
我猜在.yarnrc.yml中的configurepackagesExtension可以是一个解决方案。但我不知道怎么设置。
谁能帮我运行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": "yarn@3.3.0",
  "workspaces": [
    "packages/*"
  ]
}
ryevplcw

ryevplcw1#

您是否尝试过将dist文件夹放在packages/mono-api-server文件夹中?通过这样做,当它导入@next/core时,它不会被识别为根工作区的依赖项。

相关问题