88
99/* eslint-disable no-console */
1010
11- import { tags } from '@angular-devkit/core ' ;
11+ import { createRequire } from 'node:module ' ;
1212import { SemVer , satisfies } from 'semver' ;
1313
1414export function assertCompatibleAngularVersion ( projectRoot : string ) : void | never {
1515 let angularCliPkgJson ;
1616 let angularPkgJson ;
17- const resolveOptions = { paths : [ projectRoot ] } ;
17+
18+ // Create a custom require function for ESM compliance.
19+ // NOTE: The trailing slash is significant.
20+ const projectRequire = createRequire ( projectRoot + '/' ) ;
1821
1922 try {
20- const angularPackagePath = require . resolve ( '@angular/core/package.json' , resolveOptions ) ;
23+ const angularPackagePath = projectRequire . resolve ( '@angular/core/package.json' ) ;
2124
22- angularPkgJson = require ( angularPackagePath ) ;
25+ angularPkgJson = projectRequire ( angularPackagePath ) ;
2326 } catch {
24- console . error ( tags . stripIndents `
25- You seem to not be depending on "@angular/core". This is an error.
26- ` ) ;
27+ console . error ( 'You seem to not be depending on "@angular/core". This is an error.' ) ;
2728
2829 process . exit ( 2 ) ;
2930 }
3031
3132 if ( ! ( angularPkgJson && angularPkgJson [ 'version' ] ) ) {
32- console . error ( tags . stripIndents `
33- Cannot determine versions of "@angular/core".
34- This likely means your local installation is broken. Please reinstall your packages.
35- ` ) ;
33+ console . error (
34+ ' Cannot determine versions of "@angular/core".\n' +
35+ ' This likely means your local installation is broken. Please reinstall your packages.' ,
36+ ) ;
3637
3738 process . exit ( 2 ) ;
3839 }
3940
4041 try {
41- const angularCliPkgPath = require . resolve ( '@angular/cli/package.json' , resolveOptions ) ;
42- angularCliPkgJson = require ( angularCliPkgPath ) ;
42+ const angularCliPkgPath = projectRequire . resolve ( '@angular/cli/package.json' ) ;
43+ angularCliPkgJson = projectRequire ( angularCliPkgPath ) ;
4344 if ( ! ( angularCliPkgJson && angularCliPkgJson [ 'version' ] ) ) {
4445 return ;
4546 }
@@ -55,19 +56,16 @@ export function assertCompatibleAngularVersion(projectRoot: string): void | neve
5556 return ;
5657 }
5758
58- const supportedAngularSemver =
59- require ( '../../package.json' ) [ 'peerDependencies' ] [ '@angular/compiler-cli' ] ;
59+ const supportedAngularSemver = projectRequire ( '@angular-devkit/build-angular/package.json' ) [
60+ 'peerDependencies'
61+ ] [ '@angular/compiler-cli' ] ;
6062 const angularVersion = new SemVer ( angularPkgJson [ 'version' ] ) ;
6163
6264 if ( ! satisfies ( angularVersion , supportedAngularSemver , { includePrerelease : true } ) ) {
6365 console . error (
64- tags . stripIndents `
65- This version of CLI is only compatible with Angular versions ${ supportedAngularSemver } ,
66- but Angular version ${ angularVersion } was found instead.
67-
68- Please visit the link below to find instructions on how to update Angular.
69- https://update.angular.io/
70- ` + '\n' ,
66+ `This version of CLI is only compatible with Angular versions ${ supportedAngularSemver } ,\n` +
67+ `but Angular version ${ angularVersion } was found instead.\n` +
68+ 'Please visit the link below to find instructions on how to update Angular.\nhttps://update.angular.io/' ,
7169 ) ;
7270
7371 process . exit ( 3 ) ;
0 commit comments