Skip to content

Commit

Permalink
fix: skip missing optional deps when bundling, closes npm/cli#5924 (#149
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nlf authored Dec 7, 2022
1 parent 7406f7f commit e5256de
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ class PackWalker extends IgnoreWalker {

// get a reference to the node we're bundling
const node = this.tree.edgesOut.get(dep).to
// if there's no node, this is most likely an optional dependency that hasn't been
// installed. just skip it.
if (!node) {
continue
}
// we use node.path for the path because we want the location the node was linked to,
// not where it actually lives on disk
const path = node.path
Expand Down
52 changes: 52 additions & 0 deletions test/bundled-missing-optional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use strict'

const Arborist = require('@npmcli/arborist')
const t = require('tap')
const packlist = require('../')

const elfJS = `
module.exports = elf =>
console.log("i'm a elf")
`

t.test('includes bundled dependency using bundleDependencies', async (t) => {
const pkg = t.testdir({
'package.json': JSON.stringify({
name: 'test-package',
version: '3.1.4',
main: 'elf.js',
dependencies: {
history: '1.0.0',
},
bundleDependencies: [
'history',
],
}),
'elf.js': elfJS,
'.npmrc': 'packaged=false',
node_modules: {
history: {
'package.json': JSON.stringify({
name: 'history',
version: '1.0.0',
main: 'index.js',
optionalDependencies: {
// defined here, but not installed
optionalDep: '^1.0.0',
},
}),
'index.js': elfJS,
},
},
})

const arborist = new Arborist({ path: pkg })
const tree = await arborist.loadActual()
const files = await packlist(tree)
t.same(files, [
'elf.js',
'node_modules/history/index.js',
'node_modules/history/package.json',
'package.json',
])
})

0 comments on commit e5256de

Please sign in to comment.