Skip to content

Comments

Fix: Improve error message for missing package.json#44

Open
stdavis wants to merge 1 commit intomainfrom
better-missing-packagejson-error
Open

Fix: Improve error message for missing package.json#44
stdavis wants to merge 1 commit intomainfrom
better-missing-packagejson-error

Conversation

@stdavis
Copy link
Owner

@stdavis stdavis commented Jun 9, 2025

When the package.json file is not found in the current directory, the tool now throws a more user-friendly error message:
"No package.json file found in the current directory. Please ensure you are in the correct directory or create a package.json file."

This replaces the generic error that was previously thrown by the read-pkg library.

A new test case has been added to src/packages.test.ts to verify that this custom error message is correctly thrown.

Additionally, formatting in src/packages.test.ts has been updated to ensure correct newline placement around import statements as per project Prettier configuration.

When the `package.json` file is not found in the current directory, the tool
now throws a more user-friendly error message:
"No package.json file found in the current directory. Please ensure you are in the correct directory or create a package.json file."

This replaces the generic error that was previously thrown by the `read-pkg`
library.

A new test case has been added to `src/packages.test.ts` to verify that
this custom error message is correctly thrown.

Additionally, formatting in `src/packages.test.ts` has been updated
to ensure correct newline placement around import statements as per
project Prettier configuration.
@stdavis stdavis requested a review from Copilot June 9, 2025 22:27
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a user-friendly error when no package.json is found and verifies it via tests, plus minor formatting tweaks.

  • Wraps readPackage() in a try/catch to throw a clear "missing package.json" message on ENOENT.
  • Adds a test case in src/packages.test.ts for the custom error and adjusts import/newline formatting.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/packages.ts Catch ENOENT from read-pkg and throw a more descriptive error message.
src/packages.test.ts Added test for missing package.json error and reorganized import spacing.
Comments suppressed due to low confidence (2)

src/packages.test.ts:1

  • The import 'packageInfo' from 'package-json' is not used in this test file. Consider removing it to avoid unused imports and keep the test focused.
import packageInfo from 'package-json';

src/packages.test.ts:2

  • The test mocks readPackage calls but doesn’t invoke vi.mock('read-pkg') to mock the module itself. Add vi.mock('read-pkg') at the top of this file so the readPackage import is properly stubbed.
import { readPackage } from 'read-pkg';

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant