Yarn Berry Monorepo中NestJS产物移至根目录后启动报错求助
Yarn Berry Workspaces Monorepo 中 NestJS 构建产物移至根目录后启动失败
背景
我用 Yarn Berry Workspaces 配置 Monorepo,在 packages/mono-api-server 目录下通过 Nest CLI 执行 nest g application . 创建了 NestJS 应用。原本构建产物 dist 目录位于子包目录下时,应用可以正常启动,但我希望将 dist 统一放在根目录下,于是修改了 packages/mono-api-server/tsconfig.json 中的 outDir 配置,从 ./dist 改为 ../../dist/mono-api-server,之后启动应用就出现了依赖加载错误。
当前目录结构
. ├── .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
启动错误信息
/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 无法加载,于是做了测试:
import _ from 'lodash'; console.log("hi"); import { NestFactory } from '@nestjs/core';
结果代码正常打印了 hi,直到执行到 import { NestFactory } from '@nestjs/core'; 时才抛出上述错误。
推测方向
我猜测问题出在 @nestjs-core-virtual-c9663b0b91 位于 .yarn/unplugged 目录下,需要额外配置。听说可以通过 .yarnrc.yml 中的 packagesExtension 解决,但不清楚具体配置方式。
参考配置文件
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/*" ] }
内容的提问来源于stack exchange,提问作者gun bos
相关产品推荐
相关产品推荐

