简体   繁体   中英

How to use monorepo packages with nestjs using turborepo?

Here's what a real simple monorepo with nestjs using turborepo looks like:

.
├── README.md
├── apps
│   └── nest
│       ├── README.md
│       ├── nest-cli.json
│       ├── package.json
│       ├── src
│       │   ├── app.controller.spec.ts
│       │   ├── app.controller.ts
│       │   ├── app.module.ts
│       │   ├── app.service.ts <---- importing class here
│       │   └── main.ts
│       ├── test
│       │   ├── app.e2e-spec.ts
│       │   └── jest-e2e.json
│       ├── tsconfig.build.json
│       └── tsconfig.json
├── package.json
├── packages
│   └── lib
│       ├── index.ts <------- exporting class here
│       ├── package.json
│       └── tsconfig.json
├── pnpm-lock.yaml
├── pnpm-workspace.yaml
└── turbo.json

A class is defined in packages/lib/index.ts and is then imported in apps/nest/src/app.service.ts .

But doing so leads to the following error when trying to import this index.ts :

nest:dev: 
nest:dev: [1:40:25 AM] Found 0 errors. Watching for file changes.
nest:dev: 
nest:dev: /Users/hercule/Workspace/monorepo-nestjs-package/packages/lib/index.ts:3
nest:dev:   public hello() {
nest:dev:          ^^^^^
nest:dev: 
nest:dev: SyntaxError: Unexpected identifier
nest:dev:     at Object.compileFunction (node:vm:360:18)
nest:dev:     at wrapSafe (node:internal/modules/cjs/loader:1088:15)
nest:dev:     at Module._compile (node:internal/modules/cjs/loader:1123:27)
nest:dev:     at Object.Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
nest:dev:     at Module.load (node:internal/modules/cjs/loader:1037:32)
nest:dev:     at Function.Module._load (node:internal/modules/cjs/loader:878:12)
nest:dev:     at Module.require (node:internal/modules/cjs/loader:1061:19)
nest:dev:     at require (node:internal/modules/cjs/helpers:103:18)
nest:dev:     at Object.<anonymous> (/Users/hercule/Workspace/monorepo-nestjs-package/apps/nest/src/app.service.ts:2:1)
nest:dev:     at Module._compile (node:internal/modules/cjs/loader:1159:14)

Therefore, How do we import a typescript package into nestjs that will be compiled / parsed properly?

Note 1: I tried importing this lib ( index.ts ) to a ne x tjs app and angular app, it works without any issue. The problem only arises with nestjs

Note 2: Above's example can be reproduced using following repository: https://github.com/beneccli/monorepo-nestjs-package , once cloned, at root of the project, simply run pnpm i and then pnpm dev (or npm or yarn).

The solution is actually simple.

Issue was that the dependency was not transpiled from typescript to javascript.

To solve this it is required to tell how to build the dependency and where to find the result files. This is done using the package.json of the lib dependency:

// packages/lib/package.json
{
  "name": "lib",
  "version": "0.0.0",
  "main": "./dist/index", <----- NEW
  "types": "./dist/index", <----- NEW
  "scripts": {
    "build": "tsc --build --force tsconfig.json" <----- NEW
  },
  "devDependencies": {
    "typescript": "^4.5.2"
  }
}

I updated the previously given repository accordingly.

What is important here is to not forget to run pnpm build after pnpm install in order to get the proper built dependency. Then nestjs can be started in either dev or prod.

I clone a repo and see the following things in your packages/lib/package.json:

  • Your packages ( lib exactly ) does not have a build script so I added one.
  • Lib main does not point to the dist/index.js build so I modified the main too.

Final lib's package.json is here

{
  "name": "lib",
  "version": "0.0.0",
  "main": "./dist/index.js",
  "scripts": {
    "build": "tsc"
  },
  "types": "./index.ts",
  "devDependencies": {
    "typescript": "^4.5.2"
  }
}

After those are done, I run the script pnpm run build in the root.

Then I run pnpm dev , and your apps should start working on http://localhost:3000/ with the text Test: Hello World!

The thing behind that is you should have a build script to compile your Typescript code into JS, then have the main to point exactly the compiled index. So that when NestJs use your lib, it should have all the functions and classes in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM