typescriptnestjsmonorepoyarn-workspacesyarn-berry

run nestjs application with yarn berry workspace monorepo


I want to configure monorepo with yarn berry workspaces. I create nest application with cli that nest g application . in packages/mono-api-server folder here is my file tree

.
├── .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

nest application starts well when dist folder is under the packages/mono-api-server but i want to take it out to the root folder. so, i configure "outDir" tsconfig.json in the packages/mono-api-server from "./dist" to "../../dist/mono-api-server". Then it causes error below

/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)
......

I was suspicious that only @nestjs/core could not be loaded, so I tried importing something else on top of it such as lodash.

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

and it works until it met import { NestFactory } from '@nestjs/core'; and prints "hi"

So my assumption is since @nestjs-core-virtual-c9663b0b91 is under the .yarn/unplugged folder, it should be configured separately.

I guess the configure packagesExtension in .yarnrc.yml can be a solution. But i don't know how to set it up.

Any one can help me to running nestjs application?? upload package.json for reference.

package.json in packages/mono-api-server

{
  "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 in root

{
  "packageManager": "yarn@3.3.0",
  "workspaces": [
    "packages/*"
  ]
}


Solution

  • Have you tried placing your dist folder within the packages/mono-api-server folder? By doing this, when it imports @next/core, it won't be recognized as a dependency of your root workspace.