Skip to content

Commit d9238af

Browse files
committed
fix: do not crash when removing nameless packages
Fix: npm/npm#17858 Fix: npm/npm#18042 Fix: https://npm.community/t/issue-npm-dedupe-crash-with-typeerror-cannot-read-property-0-of-undefined/644/3 Close: #201 This fixes a bug where a package folder might have a package.json which is missing or lacks a name property. It also properly detects the scoped-ness of a package folder even if the package name is not scoped, since one might install `express@npm:@scope/express` and end up in that state.
1 parent f5e8849 commit d9238af

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/unbuild.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ function rmStuff (pkg, folder, cb) {
5858
// if it's global, and folder is in {prefix}/node_modules,
5959
// then bins are in {prefix}/bin
6060
// otherwise, then bins are in folder/../.bin
61-
var parent = pkg.name[0] === '@' ? path.dirname(path.dirname(folder)) : path.dirname(folder)
61+
var dir = path.dirname(folder)
62+
var scope = path.basename(dir)
63+
var parent = scope.charAt(0) === '@' ? path.dirname(dir) : dir
6264
var gnm = npm.dir
6365
// gnm might be an absolute path, parent might be relative
6466
// this checks they're the same directory regardless

test/tap/uninstall-package.js

+23-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var json = {
1818
version: '0.0.0',
1919
dependencies: {
2020
underscore: '~1.3.1',
21-
request: '~0.9.0'
21+
request: '~0.9.0',
22+
'@isaacs/namespace-test': '1.x'
2223
}
2324
}
2425

@@ -69,6 +70,27 @@ test('returns a list of removed items', function (t) {
6970
})
7071
})
7172

73+
test('does not fail if installed package lacks a name somehow', function (t) {
74+
const scope = path.resolve(pkg, 'node_modules/@isaacs')
75+
const scopePkg = path.resolve(scope, 'namespace-test')
76+
const pj = path.resolve(scopePkg, 'package.json')
77+
fs.writeFileSync(pj, JSON.stringify({
78+
lol: 'yolo',
79+
name: 99
80+
}))
81+
common.npm(
82+
['uninstall', '@isaacs/namespace-test'],
83+
EXEC_OPTS,
84+
function (err, code, stdout, stderr) {
85+
if (err) throw err
86+
t.equal(code, 0, 'should exit successfully')
87+
t.has(stdout, /removed 1 package in/)
88+
t.notOk(fs.existsSync(scope), 'scoped package removed')
89+
t.end()
90+
}
91+
)
92+
})
93+
7294
test('cleanup', function (t) {
7395
cleanup()
7496
t.end()

0 commit comments

Comments
 (0)