turborepo typescript build node app with workspace dependencies

244 views Asked by At

Trying to build an express api inside a pnpm turborepo workspace.

api has @my/shared as a dependency which is a local workspace package.

I have been unable to get the build correct. I think what I need to be able to do is build the shared library and include it in the api project, but to do this I would need to be able to change all the import statements to allow node to find the module because is is imported as @my/shared.

structure:

apps/
  /api <- express (depends on @my/shared)
packages/
  /shared <- @my/shared

base tsconfig

{
  "compilerOptions": {
    "target": "ES2017",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "checkJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "module": "CommonJS",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "noUncheckedIndexedAccess": true,
    "composite": false,
    "declaration": true,
    "declarationMap": true,
    "inlineSources": false,
    "outDir": "./dist"
  },
  "exclude": ["node_modules", "build", "dist", ".next", ".expo"]
}

express & shared tsconfigs

"extends": "../../tsconfig.json",
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "~/*": ["*"]
    },
    "module": "commonjs",
    "outdir": "./dist"
  },
 "include": [
    "src",
  ]
}

I simply want to convert this ts app into js to run in production mode with pm2.

I have found lots of resources but have been unable to find anything to help me achieve this.

I have tried running tsc from the root, running the output in node gives the error:

`"Error: Cannot find module 'express"`

I have tried running tsc at the api level which gives the following error when running pnpm in /dist to install dependencies.

ERR_PNPM_WORKSPACE_PKG_NOT_FOUND: @my/shared

1

There are 1 answers

1
bon On

I had a similar problem and found the reference and successfully build Express.

You can use tsup to transcompile the source.