Skip to content

Commit 2aecec5

Browse files
committed
fix: npm ls --long missing deps
Running `npm ls --json --long --all` was broken for any project containing a missing dependency from the node_modules folder. This fixes it by avoiding trying to read the extra data required by the --long option in case a dependency is missing. Fixes: #2724 PR-URL: #3119 Credit: @ruyadorno Close: #3119 Reviewed-by: @wraithgar
1 parent c4ff4bc commit 2aecec5

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/ls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ const getJsonOutputItem = (node, { global, long }) => {
305305
if (node.isRoot && hasPackageJson)
306306
item.name = node.package.name || node.name
307307

308-
if (long) {
308+
if (long && !node[_missing]) {
309309
item.name = item[_name]
310310
const { dependencies, ...packageInfo } = node.package
311311
Object.assign(item, packageInfo)

test/lib/ls.js

+42
Original file line numberDiff line numberDiff line change
@@ -2489,6 +2489,48 @@ t.test('ls --json', (t) => {
24892489
})
24902490
})
24912491

2492+
t.test('missing deps --long', (t) => {
2493+
config.long = true
2494+
npm.prefix = t.testdir({
2495+
'package.json': JSON.stringify({
2496+
name: 'test-npm-ls',
2497+
version: '1.0.0',
2498+
dependencies: {
2499+
foo: '^1.0.0',
2500+
bar: '^1.0.0',
2501+
lorem: '^1.0.0',
2502+
ipsum: '^1.0.0',
2503+
},
2504+
}),
2505+
...simpleNmFixture,
2506+
})
2507+
ls.exec([], (err) => {
2508+
t.equal(
2509+
redactCwd(err.message),
2510+
'missing: ipsum@^1.0.0, required by [email protected]',
2511+
'should log missing dep as error'
2512+
)
2513+
t.equal(
2514+
err.code,
2515+
'ELSPROBLEMS',
2516+
'should have ELSPROBLEMS error code'
2517+
)
2518+
t.match(
2519+
jsonParse(result),
2520+
{
2521+
name: 'test-npm-ls',
2522+
version: '1.0.0',
2523+
problems: [
2524+
'missing: ipsum@^1.0.0, required by [email protected]',
2525+
],
2526+
},
2527+
'should output json containing problems info'
2528+
)
2529+
config.long = false
2530+
t.end()
2531+
})
2532+
})
2533+
24922534
t.test('with filter arg', (t) => {
24932535
npm.prefix = t.testdir({
24942536
'package.json': JSON.stringify({

0 commit comments

Comments
 (0)