From a42d681529d271a82df319923a015b565f70a36b Mon Sep 17 00:00:00 2001 From: adriangroch <30421974+adriangroch@users.noreply.github.com> Date: Fri, 10 Jun 2022 23:46:12 +1000 Subject: [PATCH] fix(js): check that project's package.json exists before attempting to read its contents (#10650) --- packages/js/src/utils/update-package-json.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/js/src/utils/update-package-json.ts b/packages/js/src/utils/update-package-json.ts index ec81e24cf464c..e3b3be8493845 100644 --- a/packages/js/src/utils/update-package-json.ts +++ b/packages/js/src/utils/update-package-json.ts @@ -11,6 +11,7 @@ import { } from '@nrwl/workspace/src/utilities/buildable-libs-utils'; import { basename, dirname, join, relative } from 'path'; import { NormalizedExecutorOptions } from './schema'; +import { fileExists } from 'nx/src/utils/fileutils'; function getMainFileDirRelativeToProjectRoot( main: string, @@ -28,7 +29,15 @@ export function updatePackageJson( dependencies: DependentBuildableProjectNode[], withTypings = true ): void { - const packageJson = readJsonFile(join(options.projectRoot, 'package.json')); + const pathToPackageJson = join( + context.root, + options.projectRoot, + 'package.json' + ); + + const packageJson = fileExists(pathToPackageJson) + ? readJsonFile(pathToPackageJson) + : { name: context.projectName }; const mainFile = basename(options.main).replace(/\.[tj]s$/, ''); const relativeMainFileDir = getMainFileDirRelativeToProjectRoot(