Skip to content

Commit e5256de

Browse files
authored
fix: skip missing optional deps when bundling, closes npm/cli#5924 (#149)
1 parent 7406f7f commit e5256de

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

lib/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,11 @@ class PackWalker extends IgnoreWalker {
384384

385385
// get a reference to the node we're bundling
386386
const node = this.tree.edgesOut.get(dep).to
387+
// if there's no node, this is most likely an optional dependency that hasn't been
388+
// installed. just skip it.
389+
if (!node) {
390+
continue
391+
}
387392
// we use node.path for the path because we want the location the node was linked to,
388393
// not where it actually lives on disk
389394
const path = node.path

test/bundled-missing-optional.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict'
2+
3+
const Arborist = require('@npmcli/arborist')
4+
const t = require('tap')
5+
const packlist = require('../')
6+
7+
const elfJS = `
8+
module.exports = elf =>
9+
console.log("i'm a elf")
10+
`
11+
12+
t.test('includes bundled dependency using bundleDependencies', async (t) => {
13+
const pkg = t.testdir({
14+
'package.json': JSON.stringify({
15+
name: 'test-package',
16+
version: '3.1.4',
17+
main: 'elf.js',
18+
dependencies: {
19+
history: '1.0.0',
20+
},
21+
bundleDependencies: [
22+
'history',
23+
],
24+
}),
25+
'elf.js': elfJS,
26+
'.npmrc': 'packaged=false',
27+
node_modules: {
28+
history: {
29+
'package.json': JSON.stringify({
30+
name: 'history',
31+
version: '1.0.0',
32+
main: 'index.js',
33+
optionalDependencies: {
34+
// defined here, but not installed
35+
optionalDep: '^1.0.0',
36+
},
37+
}),
38+
'index.js': elfJS,
39+
},
40+
},
41+
})
42+
43+
const arborist = new Arborist({ path: pkg })
44+
const tree = await arborist.loadActual()
45+
const files = await packlist(tree)
46+
t.same(files, [
47+
'elf.js',
48+
'node_modules/history/index.js',
49+
'node_modules/history/package.json',
50+
'package.json',
51+
])
52+
})

0 commit comments

Comments
 (0)