nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.62k stars 2.36k forks source link

Build error: Could not find 'nx' module in this workspace #18174

Closed yumadilovtimur closed 1 year ago

yumadilovtimur commented 1 year ago

Current Behavior

After upgrading from angular/nx v15 to v16, the application build started throwing an error in CI. (half of builds with errors, half successful) Sometimes (sometimes from the third time) restarting the build in TeamCity helps. Prior to the update, all builds were consistently successful. The build steps and TeamCity settings have not changed. Tried to delete node_modules, yarn.lock, as well as cache in folders: .yarn/cache, .nx, .angular. It didn't help. Tell me, what could be the problem? Thank you very much in advance!

node.js v18.12.1

nx.json

{
  "$schema": "./node_modules/nx/schemas/nx-schema.json",
  "npmScope": "FrontEnd",
  "affected": {
    "defaultBase": "release"
  },
  "tasksRunnerOptions": {
    "default": {
      "runner": "nx/tasks-runners/default",
      "options": {
        "cacheableOperations": [
          "build",
          "lint",
          "test",
          "e2e",
          "build-storybook"
        ],
        "parallel": 5,
        "cacheDirectory": ".nx/cache"
      }
    }
  },
  "targetDefaults": {
    "build": {
      "dependsOn": [
        "^build"
      ],
      "inputs": [
        "production",
        "^production"
      ]
    },
    "test": {
      "inputs": [
        "default",
        "^production",
        "{workspaceRoot}/jest.preset.js"
      ]
    },
    "e2e": {
      "inputs": [
        "default",
        "^production"
      ]
    },
    "lint": {
      "inputs": [
        "default",
        "{workspaceRoot}/.eslintrc.json"
      ]
    },
    "build-storybook": {
      "inputs": [
        "default",
        "^production",
        "{workspaceRoot}/.storybook/**/*"
      ]
    }
  },
  "namedInputs": {
    "default": [
      "{projectRoot}/**/*",
      "sharedGlobals"
    ],
    "production": [
      "default",
      "!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)",
      "!{projectRoot}/tsconfig.spec.json",
      "!{projectRoot}/jest.config.[jt]s",
      "!{projectRoot}/.eslintrc.json",
      "!{projectRoot}/.storybook/**/*",
      "!{projectRoot}/**/*.stories.@(js|jsx|ts|tsx|mdx)"
    ],
    "sharedGlobals": []
  },
  "cli": {
    "packageManager": "yarn"
  },
  "generators": {
    "@nx/angular:application": {
      "style": "scss",
      "linter": "eslint",
      "unitTestRunner": "jest",
      "e2eTestRunner": "cypress"
    },
    "@nx/angular:library": {
      "linter": "eslint",
      "unitTestRunner": "jest",
      "buildable": true
    },
    "@nx/angular:component": {
      "style": "scss"
    }
  },
  "defaultProject": "client"
}

package.json

{
  "name": "front-end",
  "version": "1.0.0",
  "scripts": {
    //...
  },
  "lint-staged": {
    "*.{ts,html}": "eslint --fix",
    "*.scss": "stylelint --fix --customSyntax postcss-scss"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged --relative"
    }
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "16.0.6",
    "@angular/cdk": "16.0.4",
    "@angular/common": "16.0.6",
    "@angular/compiler": "16.0.6",
    "@angular/core": "16.0.6",
    "@angular/forms": "16.0.6",
    "@angular/platform-browser": "16.0.6",
    "@angular/platform-browser-dynamic": "16.0.6",
    "@angular/platform-server": "16.0.6",
    "@angular/router": "16.0.6",
    "@angular/service-worker": "16.0.6",
    "@fingerprintjs/fingerprintjs": "3.3.3",
    "@microsoft/signalr": "6.0.3",
    "@nx/angular": "16.3.2",
    "@sentry/angular": "7.12.1",
    "@sentry/tracing": "7.12.1",
    "angular-auth-oidc-client": "15.0.4",
    "angular-google-tag-manager": "1.4.4",
    "crypto-js": "4.1.1",
    "disable-scroll": "0.6.0",
    "guid-typescript": "1.0.9",
    "jwt-decode": "3.1.2",
    "lottie-web": "5.7.11",
    "ng2-pdf-viewer": "9.0.0",
    "ngx-cookie-service": "15.0.0",
    "ngx-device-detector": "4.0.1",
    "ngx-infinite-scroll": "14.0.0",
    "ngx-mask": "14.0.1",
    "ngx-quicklink": "0.3.0",
    "number-to-words-ru": "2.3.4",
    "plural-ru": "2.0.2",
    "primeflex": "3.2.1",
    "primeicons": "5.0.0",
    "primeng": "14.0.2",
    "qr-code-styling": "1.6.0-rc.1",
    "rxjs": "7.8.1",
    "simplebar-angular": "2.4.0",
    "svg4everybody": "2.1.9",
    "tslib": "2.3.0",
    "typograf": "6.14.1",
    "zone.js": "0.13.1"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "16.0.6",
    "@angular-devkit/core": "16.0.6",
    "@angular-devkit/schematics": "16.0.6",
    "@angular-eslint/eslint-plugin": "16.0.3",
    "@angular-eslint/eslint-plugin-template": "16.0.3",
    "@angular-eslint/template-parser": "16.0.3",
    "@angular/cli": "16.1.0",
    "@angular/compiler-cli": "16.0.6",
    "@angular/language-service": "16.0.6",
    "@babel/core": "7.14.2",
    "@compodoc/compodoc": "1.1.19",
    "@nx/cypress": "16.3.2",
    "@nx/eslint-plugin": "16.3.2",
    "@nx/jest": "16.3.2",
    "@nx/linter": "16.3.2",
    "@nx/storybook": "16.3.2",
    "@nx/workspace": "16.3.2",
    "@schematics/angular": "16.0.6",
    "@storybook/addon-a11y": "7.0.22",
    "@storybook/addon-docs": "7.0.22",
    "@storybook/addon-essentials": "7.0.22",
    "@storybook/addon-links": "7.0.22",
    "@storybook/addon-mdx-gfm": "7.0.22",
    "@storybook/addon-storysource": "7.0.22",
    "@storybook/angular": "7.0.22",
    "@storybook/core-server": "7.0.22",
    "@storybook/test-runner": "0.10.0",
    "@types/jest": "29.4.4",
    "@types/node": "16.11.7",
    "@typescript-eslint/eslint-plugin": "5.59.11",
    "@typescript-eslint/parser": "5.59.11",
    "babel-loader": "8.2.2",
    "brotli-cli": "1.0.4",
    "chromatic": "6.11.3",
    "codelyzer": "6.0.2",
    "compression": "1.7.4",
    "copy-webpack-plugin": "8.1.1",
    "cypress": "11.1.0",
    "directory-tree": "2.2.9",
    "eslint": "8.15.0",
    "eslint-plugin-cypress": "2.10.3",
    "eslint-plugin-storybook": "0.6.12",
    "glob": "9.3.1",
    "globby": "11.0.3",
    "gzip-cli": "1.2.0",
    "handbrake-js": "6.0.0",
    "husky": "4.3.0",
    "imagemin-keep-folder": "5.3.2",
    "imagemin-mozjpeg": "9.0.0",
    "imagemin-pngquant": "9.0.2",
    "imagemin-svgo": "8.0.0",
    "imagemin-webp": "6.0.0",
    "jest": "29.4.3",
    "jest-environment-jsdom": "29.4.3",
    "jest-preset-angular": "13.1.1",
    "json-to-scss": "1.6.2",
    "lint-staged": "13.0.3",
    "mkdirp": "1.0.4",
    "mozjpeg": "7.0.0",
    "ng-packagr": "16.0.1",
    "nx": "16.3.2",
    "open": "8.4.0",
    "postcss": "8.4.5",
    "postcss-import": "14.1.0",
    "postcss-preset-env": "7.5.0",
    "postcss-scss": "4.0.2",
    "postcss-url": "10.1.3",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "replace-in-file": "6.3.5",
    "resolve": "1.9.0",
    "sharp": "0.32.2",
    "slash": "3.0.0",
    "storybook": "7.0.22",
    "storybook-addon-themes": "6.1.0",
    "stylelint": "14.11.0",
    "stylelint-order": "5.0.0",
    "stylelint-scss": "4.3.0",
    "svg-sprite": "1.5.2",
    "svgo": "1.3.2",
    "ts-dedent": "2.2.0",
    "ts-jest": "29.1.0",
    "ts-node": "10.9.1",
    "typescript": "5.0.4",
    "vinyl": "2.2.1",
    "webpack": "5.64.0",
    "webpack-cli": "4.7.0"
  },
  "packageManager": "yarn@3.6.0"
}

build steps:

npm install -g @angular/cli@16.1.0 nx@16.3.2
corepack enable
yarn set version 3.6.0
yarn install --immutable
yarn nx run client:build-app --configurationId=staging

build error:

[05:49:15][Step 2/8] Could not find 'nx' module in this workspace. Error: [readCachedProjectGraph] ERROR: No cached ProjectGraph is available.
[05:49:15][Step 2/8] If you are leveraging \`readCachedProjectGraph()\` directly then you will need to refactor your usage to first ensure that
[05:49:15][Step 2/8] the ProjectGraph is created by calling \`await createProjectGraphAsync()\` somewhere before attempting to read the data.
[05:49:15][Step 2/8] If you encounter this error as part of running standard \`nx\` commands then please open an issue on https://github.com/nrwl/nx
[05:49:15][Step 2/8]     at readCachedProjectGraph (/opt/buildagent/work/ab1214113e2446bc/node_modules/nx/src/project-graph/project-graph.js:30:15)
[05:49:15][Step 2/8]     at run (/opt/buildagent/work/ab1214113e2446bc/node_modules/nx/src/command-line/run/run.js:179:69)
[05:49:15][Step 2/8]     at /opt/buildagent/work/ab1214113e2446bc/node_modules/nx/bin/run-executor.js:60:48
[05:49:15][Step 2/8]     at Generator.next (<anonymous>)
[05:49:15][Step 2/8]     at /opt/buildagent/work/ab1214113e2446bc/node_modules/tslib/tslib.js:117:75
[05:49:15][Step 2/8]     at new Promise (<anonymous>)
[05:49:15][Step 2/8]     at Object.__awaiter (/opt/buildagent/work/ab1214113e2446bc/node_modules/tslib/tslib.js:113:16)
[05:49:15][Step 2/8]     at process.<anonymous> (/opt/buildagent/work/ab1214113e2446bc/node_modules/nx/bin/run-executor.js:58:44)
[05:49:15][Step 2/8]     at process.emit (node:events:513:28)
[05:49:15][Step 2/8]     at emit (node:internal/child_process:937:14)

Expected Behavior

The application is being built successfully. There are no errors

GitHub Repo

No response

Steps to Reproduce

described above

Nx Report

NX  Falling back to ts-node for local typescript execution. This may be a little slower.
  - To fix this, ensure @swc-node/register and @swc/core have been installed

 >  NX   Report complete - copy this into the issue template

   Node   : 18.12.1
   OS     : darwin x64
   yarn   : 3.6.0
   Hasher : Native

   nx (global)        : 16.5.2
   nx                 : 16.3.2
   @nx/js             : 16.3.2
   @nx/jest           : 16.3.2
   @nx/linter         : 16.3.2
   @nx/workspace      : 16.3.2
   @nx/angular        : 16.3.2
   @nx/cypress        : 16.3.2
   @nx/devkit         : 16.3.2
   @nx/eslint-plugin  : 16.3.2
   @nx/storybook      : 16.3.2
   @nrwl/tao          : 16.3.2
   @nx/webpack        : 16.3.2
   typescript         : 5.0.4
   ---------------------------------------
   Community plugins:
   @compodoc/compodoc       : 1.1.19
   @storybook/angular       : 7.0.22
   angular-auth-oidc-client : 15.0.4
   ---------------------------------------
   Local workspace plugins:
         @FrontEnd/storybook-host
         shared
         atms

Failure Logs

No response

Operating System

Additional Information

No response

AgentEnder commented 1 year ago

Are you installing nx globally in CI? In some instances that can cause problems. If so, I'd generally recommend using npx nx, pnpm nx etc to call into your targets.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏

seeplusplus commented 1 year ago

fyi @yumadilovtimur, I fixed this by changing my cacheDirectory. I was using the same directory as you, .nx/cache. Switching to any dir other than one in .nx seems to work for me. Specifically, I am using build/cache now, but I tested with .test/cache as well.

CC @AgentEnder relevant discord thread https://discord.com/channels/1143497901675401286/1146559675656065075/1146559675656065075

domjtalbot commented 1 year ago

Thanks for the suggestion @seeplusplus! 🏆 My cacheDirectory was set to .nx/.cache and I faced the same bug. Renaming to a non-.nx/* directory solved it.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it hasn't had any recent activity. It will be closed in 14 days if no further activity occurs. If we missed this issue please reply to keep it active. Thanks for being a part of the Nx community! 🙏

github-actions[bot] commented 1 year ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.