From 99dbdc7e62b1306ab6a9b48ce8b46ec6fe909887 Mon Sep 17 00:00:00 2001 From: AgentEnder Date: Mon, 3 Apr 2023 13:45:55 -0400 Subject: [PATCH] fix(core): should not throw 'unable to resolve nx/package.json' --- packages/nx/bin/nx.ts | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/nx/bin/nx.ts b/packages/nx/bin/nx.ts index ea96c3ad8a872..66fcd97dc381e 100644 --- a/packages/nx/bin/nx.ts +++ b/packages/nx/bin/nx.ts @@ -11,8 +11,8 @@ import { getNxRequirePaths, } from '../src/utils/installation-directory'; import { major } from 'semver'; -import { readJsonFile } from '../src/utils/fileutils'; import { stripIndents } from '../src/utils/strip-indents'; +import { readModulePackageJson } from '../src/utils/package-json'; import { execSync } from 'child_process'; function main() { @@ -163,12 +163,17 @@ function warnIfUsingOutdatedGlobalInstall( } } -function getLocalNxVersion(workspace: WorkspaceTypeAndRoot): string { - return readJsonFile( - require.resolve('nx/package.json', { - paths: getNxRequirePaths(workspace.dir), - }) - ).version; +function getLocalNxVersion(workspace: WorkspaceTypeAndRoot): string | null { + const localNxPackages = ['nx', '@nrwl/tao', '@nrwl/cli']; + for (const pkg of localNxPackages) { + try { + const { packageJson } = readModulePackageJson( + 'nx', + getNxRequirePaths(workspace.dir) + ); + return packageJson.version; + } catch {} + } } function _getLatestVersionOfNx(): string {