Skip to content

Commit 55916b1

Browse files
kaiyomadarcyclarke
authored andcommitted
fix: check npm.config before accessing its members
Sometimes, `npm.config` can be missing entirely, but there are several places where `npm.config.foo` is accessed blindly, resulting in these kinds of errors and stack traces: TypeError: Cannot read property 'get' of undefined at errorMessage (.../lib/utils/error-message.js:38:39) ... TypeError: Cannot read property 'loaded' of undefined at exit (.../lib/utils/error-handler.js:97:27) ... LBYL by checking `npm.config` first. Addresses a small part of #502. PR-URL: #508 Credit: @ Close: #508 Reviewed-by: @darcy Clarke
1 parent f533d61 commit 55916b1

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

lib/utils/error-handler.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ process.on('timing', function (name, value) {
3636
process.on('exit', function (code) {
3737
process.emit('timeEnd', 'npm')
3838
log.disableProgress()
39-
if (npm.config.loaded && npm.config.get('timing')) {
39+
if (npm.config && npm.config.loaded && npm.config.get('timing')) {
4040
try {
4141
timings.logfile = getLogFile()
4242
cacheFile.append('_timing.json', JSON.stringify(timings) + '\n')
@@ -64,7 +64,7 @@ process.on('exit', function (code) {
6464
log.verbose('code', code)
6565
}
6666
}
67-
if (npm.config.loaded && npm.config.get('timing') && !wroteLogFile) writeLogFile()
67+
if (npm.config && npm.config.loaded && npm.config.get('timing') && !wroteLogFile) writeLogFile()
6868
if (wroteLogFile) {
6969
// just a line break
7070
if (log.levels[log.level] <= log.levels.error) console.error('')
@@ -79,7 +79,7 @@ process.on('exit', function (code) {
7979
wroteLogFile = false
8080
}
8181

82-
var doExit = npm.config.loaded && npm.config.get('_exit')
82+
var doExit = npm.config && npm.config.loaded && npm.config.get('_exit')
8383
if (doExit) {
8484
// actually exit.
8585
if (exitCode === 0 && !itWorked) {
@@ -94,7 +94,7 @@ process.on('exit', function (code) {
9494
function exit (code, noLog) {
9595
exitCode = exitCode || process.exitCode || code
9696

97-
var doExit = npm.config.loaded ? npm.config.get('_exit') : true
97+
var doExit = npm.config && npm.config.loaded ? npm.config.get('_exit') : true
9898
log.verbose('exit', [code, doExit])
9999
if (log.level === 'silent') noLog = true
100100

lib/utils/error-message.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ function errorMessage (er) {
3535
case 'EACCES':
3636
case 'EPERM':
3737
const isCachePath = typeof er.path === 'string' &&
38-
er.path.startsWith(npm.config.get('cache'))
38+
npm.config && er.path.startsWith(npm.config.get('cache'))
3939
const isCacheDest = typeof er.dest === 'string' &&
40-
er.dest.startsWith(npm.config.get('cache'))
40+
npm.config && er.dest.startsWith(npm.config.get('cache'))
4141

4242
const isWindows = process.platform === 'win32'
4343

0 commit comments

Comments
 (0)