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