diff --git a/src/compiler/moduleSpecifiers.ts b/src/compiler/moduleSpecifiers.ts index 0afff755d15b7..484dd069f4fed 100644 --- a/src/compiler/moduleSpecifiers.ts +++ b/src/compiler/moduleSpecifiers.ts @@ -90,6 +90,7 @@ import { pathIsBareSpecifier, pathIsRelative, PropertyAccessExpression, + readJson, removeExtension, removeFileExtension, removeSuffix, @@ -907,11 +908,11 @@ function tryGetModuleNameFromPackageJsonImports(moduleFileName: string, sourceDi } const packageJsonPath = combinePaths(ancestorDirectoryWithPackageJson, "package.json"); const cachedPackageJson = host.getPackageJsonInfoCache?.()?.getPackageJsonInfo(packageJsonPath); - if (typeof cachedPackageJson !== "object" && cachedPackageJson !== undefined || !host.fileExists(packageJsonPath)) { + if (typeof cachedPackageJson !== "object" && cachedPackageJson !== undefined) { return undefined; } - const packageJsonContent = cachedPackageJson?.contents.packageJsonContent || JSON.parse(host.readFile(packageJsonPath)!); - const imports = packageJsonContent?.imports; + const packageJsonContent = cachedPackageJson?.contents.packageJsonContent || readJson(packageJsonPath, host as { readFile(path: string): string | undefined; }); + const imports = (packageJsonContent as any).imports; if (!imports) { return undefined; }