@@ -5,6 +5,7 @@ import { GeneratorCallback } from 'nx/src/config/misc-interfaces';
55import { clean , coerce , gt , satisfies } from 'semver' ;
66import { getPackageManagerCommand } from 'nx/src/utils/package-manager' ;
77import { execSync } from 'child_process' ;
8+ import { readModulePackageJson } from 'nx/src/utils/package-json' ;
89
910const NON_SEMVER_TAGS = {
1011 '*' : 2 ,
@@ -346,21 +347,22 @@ export async function ensurePackage(
346347 let version : string ;
347348
348349 // Read package and version from root package.json file.
349- const packageJson = readJson ( tree , 'package.json' ) ;
350350 const dev = options . dev ?? true ;
351351 const throwOnMissing = options . throwOnMissing ?? ! ! process . env . NX_DRY_RUN ; // NX_DRY_RUN is set in `packages/nx/src/command-line/generate.ts`
352352 const pmc = getPackageManagerCommand ( ) ;
353- const field = dev ? 'devDependencies' : 'dependencies' ;
354353
355- version = packageJson [ field ] ?. [ pkg ] ;
354+ // Try to resolve the actual version from resolved module.
355+ try {
356+ version = readModulePackageJson ( pkg ) . packageJson . version ;
357+ } catch {
358+ // ignore
359+ }
356360
357- // If package not found, try to resolve it using Node and get its version .
361+ // Otherwise try to read in from package.json. This is needed for E2E tests to pass .
358362 if ( ! version ) {
359- try {
360- version = require ( `${ pkg } /package.json` ) . version ;
361- } catch {
362- // ignore
363- }
363+ const packageJson = readJson ( tree , 'package.json' ) ;
364+ const field = dev ? 'devDependencies' : 'dependencies' ;
365+ version = packageJson [ field ] ?. [ pkg ] ;
364366 }
365367
366368 if ( ! satisfies ( version , requiredVersion ) ) {
0 commit comments