Fix: Improve error message for missing package.json#44
Open
Conversation
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.
There was a problem hiding this comment.
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.tsfor 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
readPackagecalls but doesn’t invokevi.mock('read-pkg')to mock the module itself. Addvi.mock('read-pkg')at the top of this file so thereadPackageimport is properly stubbed.
import { readPackage } from 'read-pkg';
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When the
package.jsonfile 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-pkglibrary.A new test case has been added to
src/packages.test.tsto verify that this custom error message is correctly thrown.Additionally, formatting in
src/packages.test.tshas been updated to ensure correct newline placement around import statements as per project Prettier configuration.