#Turborepo nestjs importing type library without building

12 messages · Page 1 of 1 (latest)

pulsar spear
#

Turborepo Root tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@seren/types": ["packages/types/src/index.ts"]
    }
  } 
}
#

Nestjs tsconfig.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "rootDir": "src",
    "outDir": "./dist",
    "incremental": true,
    "skipLibCheck": true,
    "strictNullChecks": true, // required by zod
    "noImplicitAny": false,
    "strictBindCallApply": false,
    "forceConsistentCasingInFileNames": false,
    "noFallthroughCasesInSwitch": false,
  },
  "include": [
    "src/**/*",
  ]
}
#

Nestjs tsconfig.build.json

{
  "extends": "./tsconfig.json",
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
#

Types library package.json

{
    "name": "@seren/types",
    "version": "1.0.0",
    "type": "module",
    "main": "./src/index.ts",
    "exports": {
        ".": {
            "types": "./src/index.ts",
            "default": "./src/index.ts"
        }
    },
    "devDependencies": {
        "typescript": "^5.7.3"
    },
    "dependencies": {
        "uuid": "^11.1.0",
        "zod": "^4.1.12"
    }
}
#

Types library tsconfig.json

{
  "compilerOptions": {
    "target": "es2016",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "outDir": "dist",
    "rootDir": "src"
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "dist"]
}
#

A portion of the error that I'm getting in nestjs, when it tries to build

src/modules/bankAccountBalance/bankAccountBalance.repository.ts:2:53 - error TS6059: File 'monorepo/packages/types/src/index.ts' is not under 'rootDir' 'monorepo/apps/server/src'. 'rootDir' is expected to contain all source files.
  The file is in the program because:
    Imported via "@seren/types" from file 'monorepo/apps/server/src/modules/bankAccountBalance/bankAccountBalance.repository.ts'
    Imported via "@seren/types" from file 'monorepo/apps/server/src/modules/budget/budget.schema.ts'
    Imported via "@seren/types" from file 'monorepo/apps/server/src/modules/bankAccount/bankAccount.schema.ts'
    Imported via "@seren/types" from file 'monorepo/apps/server/src/modules/bankAccountBalance/bankaccountbalance.service.ts'
solid stirrup
#

Won't work.

  1. NestJS says rootDir: "src
  2. TSC requires all files to be in rootDir

It works in vite because vite doesn't use use tsc but esbuild and doesn't envorfce strict structure.

But you can use SWC with nestjs (nestjs is actually meant to be transpiled with swc and not tsc, has first class support for swc) which doesn't enforce rootDir.

pulsar spear
#

I'll see if I can get that to work and reach back

#

Seems to have gotten further than before, but I'm not sure where it's getting the src source from?

nest start -b swc
>  SWC  Running...
Successfully compiled: 71 files with swc (642.82ms)
node:internal/modules/cjs/loader:1252
  throw err;
  ^

Error: Cannot find module '../../../../../packages/types/src'
Require stack:
- /monorepo/apps/server/dist/modules/bankAccount/bankAccount.schema.js
- /monorepo/apps/server/dist/modules/bankAccountBalance/bankAccountBalance.schema.js
- /monorepo/apps/server/dist/modules/bankAccountBalance/bankAccountBalance.repository.js
- /monorepo/apps/server/dist/modules/bankAccountBalance/bankAccountBalance.module.js
- /monorepo/apps/server/dist/app.module.js
- /monorepo/apps/server/dist/main.js
    at Function._resolveFilename (node:internal/modules/cjs/loader:1249:15)
    at Function._load (node:internal/modules/cjs/loader:1075:27)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:219:24)
    at Module.require (node:internal/modules/cjs/loader:1340:12)
    at require (node:internal/modules/helpers:138:16)
    at Object.<anonymous> (/monorepo/apps/server/src/modules/bankAccount/bankAccount.schema.ts:2:29)
    at Module._compile (node:internal/modules/cjs/loader:1565:14)
    at Object..js (node:internal/modules/cjs/loader:1708:10)
    at Module.load (node:internal/modules/cjs/loader:1318:32) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/monorepo/apps/server/dist/modules/bankAccount/bankAccount.schema.js',
    '/monorepo/apps/server/dist/modules/bankAccountBalance/bankAccountBalance.schema.js',
    '/monorepo/apps/server/dist/modules/bankAccountBalance/bankAccountBalance.repository.js',
    '/monorepo/apps/server/dist/modules/bankAccountBalance/bankAccountBalance.module.js',
    '/monorepo/apps/server/dist/app.module.js',
    '/monorepo/apps/server/dist/main.js'
  ]
}

Node.js v22.12.0
#

Is it because I'm extending the monorepo root tsconfig?

#

@solid stirrup , sorry I forgot to reply to your message before sending