Monorepo-Yarn workspace "Cannot find module or its corresponding type declarations." after build a package

46 Views Asked by At

I have a monorepo project using Yarn Workspace & Typescript

Folder Structure

packages
--- common
------ src
------ package.json
------ tsconfig.json
--- app
------ src
------ package.json
------ tsconfig.json
package.json
tsconfig.json

When I run script build on common:
yarn build
or
yarn workspace @project/common build

It rebuilds the common package but app doesn't recognize common anymore and typescript throws the following error on all the imports:

Cannot find module '@project/common' or its corresponding type declarations.

The only way to remove the syntax error is to reload the window in vscode or reload Typescript project.

What did I do wrong in my implementation? Have I missed something?

Specs

Typescript: 5.4.2
Yarn: 4.1.0
Node: 18.x

package.json

{
  "name": "@project/project",
  "version": "1.0.0",
  "private": true,
  "engines": {
    "node": "18.x"
  },
  "workspaces": {
    "packages": [
      "packages/*"
    ]
  },
  "installConfig": {
    "hoistingLimits": "dependencies"
  },
  "scripts": {
      // ...scripts
  },
  "devDependencies": {
      // ...dev dependencies
  },
  "packageManager": "[email protected]"
}

tsconfig.json

{
  "compilerOptions": {
    "allowJs": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "sourceMap": true,
    "declaration": true,
    "strict": true,
    "allowSyntheticDefaultImports": true,
    "skipLibCheck": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "strictPropertyInitialization": false,
    "ignoreDeprecations": "5.0",
    "removeComments": true,
    "allowUnreachableCode": false
  }
}

packages/common/package.json

{
  "name": "@project/common",
  "private": true,
  "version": "1.0.0",
  "main": "build/index.js",
  "types": "build/index.d.ts",
  "engines": {
    "node": "18.x"
  },
  "scripts": {
    "build": "rm -rf ./build && tsc -p ."
  },
  "dependencies": {
    // ...dependencies
  },
  "devDependencies": {
    // ...dev dependencies
  }
}

packages/app/package.json

{
  "name": "@project/app",
  "private": true,
  "version": "1.0.0",
  "engines": {
    "node": "18.x"
  },
  "scripts": {
    // ...scripts
  },
  "dependencies": {
    "@project/common": "*",
    // ...dependencies
  },
  "devDependencies": {
    // ...dev dependencies
  }
}

0

There are 0 best solutions below