Skip to content

Commit 0b60ea7

Browse files
authored
fix: handle missing package.json gracefully (#13691)
1 parent a4acd47 commit 0b60ea7

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

.changeset/red-pugs-smell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/package': patch
3+
---
4+
5+
fix: handle missing package.json gracefully

packages/package/src/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function load_config({ cwd = process.cwd() } = {}) {
2929

3030
/**
3131
* @param {string} cwd
32-
* @returns Record<string, any>
32+
* @returns {Record<string, any>}
3333
*/
3434
export function load_pkg_json(cwd = process.cwd()) {
3535
const pkg_json_file = path.join(cwd, 'package.json');

packages/package/src/validate.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { readFileSync } from 'node:fs';
2-
import { join } from 'node:path';
31
import colors from 'kleur';
2+
import { load_pkg_json } from './config.js';
43

54
/**
65
* @param {import("./types.js").Options} options
@@ -19,8 +18,14 @@ export function create_validator(options) {
1918
},
2019
validate() {
2120
/** @type {Record<string, any>} */
22-
const pkg = JSON.parse(readFileSync(join(options.cwd, 'package.json'), 'utf-8'));
21+
const pkg = load_pkg_json(options.cwd);
2322
const warnings = validate(pkg);
23+
if (Object.keys(pkg).length === 0) {
24+
warnings.push(
25+
'No package.json found in the current directory. Please create one or run this command in a directory containing one.'
26+
);
27+
}
28+
2429
// Just warnings, not errors, because
2530
// - would be annoying in watch mode (would have to restart the server)
2631
// - maybe there's a custom post-build script that fixes some of these

0 commit comments

Comments
 (0)