Skip to content

Commit

Permalink
fix: don't reify if package.json doesn't exist or is empty object
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongmao86 committed Jan 5, 2024
1 parent 86ac76c commit 56b9023
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ graph LR;
npm-->pacote;
npm-->parse-conflict-json;
npm-->proc-log;
npm-->read-package-json-fast;
npm-->read;
npm-->semver;
npm-->ssri;
Expand Down Expand Up @@ -553,6 +554,7 @@ graph LR;
npm-->parse-conflict-json;
npm-->proc-log;
npm-->qrcode-terminal;
npm-->read-package-json-fast;
npm-->read;
npm-->remark-gfm;
npm-->remark-github;
Expand Down
15 changes: 15 additions & 0 deletions lib/commands/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const { resolve, join } = require('path')
const runScript = require('@npmcli/run-script')
const pacote = require('pacote')
const checks = require('npm-install-checks')
const path = require('node:path')
const rpj = require('read-package-json-fast')

const ArboristWorkspaceCmd = require('../arborist-cmd.js')
class Install extends ArboristWorkspaceCmd {
Expand Down Expand Up @@ -140,6 +142,19 @@ class Install extends ArboristWorkspaceCmd {
throw this.usageError()
}

if (!isGlobalInstall) {
const pkgPath = path.join(this.npm.prefix, './package.json')
const pkg = await rpj(pkgPath)
const isPkgEmpty = Object.keys(pkg).length === 0 && pkg.constructor === Object

const emptyJson = new Error()
emptyJson.code = 'EEMPTYPKGJSON'
emptyJson.path = pkgPath
if (isPkgEmpty) {
throw emptyJson
}
}

const Arborist = require('@npmcli/arborist')
const opts = {
...this.npm.flatOptions,
Expand Down
8 changes: 8 additions & 0 deletions lib/utils/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,14 @@ const errorMessage = (er, npm) => {
])
break

case 'EEMPTYPKGJSON':
short.push(['eemptypkgjson', er.message])
detail.push([
'eemptypkgjson',
'The root of package.json is an empty object',
])
break

default:
short.push(['', er.message || er])
if (er.signal) {
Expand Down
1 change: 1 addition & 0 deletions package-lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@
"proc-log": "^3.0.0",
"qrcode-terminal": "^0.12.0",
"read": "^2.1.0",
"read-package-json-fast": "^3.0.2",
"semver": "^7.5.4",
"spdx-expression-parse": "^3.0.1",
"ssri": "^10.0.5",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
"proc-log": "^3.0.0",
"qrcode-terminal": "^0.12.0",
"read": "^2.1.0",
"read-package-json-fast": "^3.0.2",
"semver": "^7.5.4",
"spdx-expression-parse": "^3.0.1",
"ssri": "^10.0.5",
Expand Down Expand Up @@ -183,6 +184,7 @@
"proc-log",
"qrcode-terminal",
"read",
"read-package-json-fast",
"semver",
"spdx-expression-parse",
"ssri",
Expand Down

0 comments on commit 56b9023

Please sign in to comment.