Search code examples
typescriptbuildmonoreponrwl-nx

Getting 'Cannot read properties of undefined (reading 'projects')' when trying to build TypeScript project


I'm getting the following error when trying to build TypeScript project:

Cannot read properties of undefined (reading 'projects').

This is the tsconfig.json of the project:

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "rootDir": ".",
    "baseUrl": ".",
    "outDir": "dist",
    "target": "ES2020",
    "module": "ES2020",
    "lib": [
      "ES2020"
    ],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "importHelpers": false,
    "moduleResolution": "node",
    "types": [
      "jest",
      "node"
    ],
    "resolveJsonModule": true,
    "allowJs": true,
    "checkJs": true,
    "declaration": false,
    "sourceMap": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": false,
    "noImplicitAny": false,
    "strictNullChecks": false,
    "strictFunctionTypes": false,
    "strictBindCallApply": true,
    "strictPropertyInitialization": false,
    "noImplicitThis": false,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": false,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": false,
    "skipLibCheck": true,
    "skipDefaultLibCheck": true
  },
  "include": [
    "./src/**/*.ts"
  ]
}

This is the tsconfig.base.json:

{
  "compileOnSave": true,
  "compilerOptions": {
    "paths": {}
  },
  "ts-node": {
    "require": [
      "tsconfig-paths/register"
    ]
  },
  "exclude": [
    "components",
    "dist",
    "node_modules"
  ]
}

I'm using NX monorepo to manage my projects, so this is the configuration of the project:

        "build": {
          "builder": "@nrwl/js:tsc",
          "outputs": [
            "{options.outputPath}"
          ],
          "options": {
            "outputPath": "packages/db-package/dist",
            "tsConfig": "packages/db-package/tsconfig.json",
            "main": "packages/db-package/src/index.ts",
            "assets": [
              "packages/db-package/*.md"
            ]
          }
        }

Any ideas?


Solution

  • This is the same problem described here: Problem with launching application in NX monorepo: (cannot read undefined (reading 'projects').

    You ultimately likely have mismatched versions of nrwl/nx packages. You can confirm by running:

    
        $ npx nx report
    
    

    In my case I had some packages at 15.3.0 and others at 15.6.0.

    To resolve the issue, perform a migration to the latest package versions:

    
        $ npx nx migrate latest
    
    

    This will output something along the lines of the following:

    
        Fetching meta data about packages.
        It may take a few minutes.
        Fetching [email protected]
        Fetching @nrwl/[email protected]
        Fetching @nrwl/[email protected]
        Fetching @nrwl/[email protected]
        Fetching @nrwl/[email protected]
        Fetching @nrwl/[email protected]
        Fetching @nrwl/[email protected]
        Fetching @nrwl/[email protected]
        Fetching @nrwl/[email protected]
        Fetching @angular/[email protected]
        
         >  NX   The migrate command has run successfully.
        
         - Package.json has been updated.
         - Migrations.json has been generated.
        
        
         >  NX   Next steps:
        
         - Make sure package.json changes make sense and then run 'npm install',
         - Run 'npx nx migrate --run-migrations'
         - To learn more go to https://nx.dev/core-features/automate-updating-dependencies
         - You may run 'npm run nx -- connect-to-nx-cloud' to get faster builds, GitHub integration, and more. Check out https://nx.app
    
    

    Note the next steps in the prompt. Additionally, you may, as I did, have conflicts in your package-lock.json that will cause npm install to fail, in which case you will need to remove the package lock file to proceed (rm package-lock.json).

    
        $ rm package-lock.json
        $ npm install
        $ npx nx migrate --run-migrations
    
    

    Note my usage of npx which can be omitted if nx is installed in your context.