Skip to content

Commit

Permalink
fix: check npm.config before accessing its members
Browse files Browse the repository at this point in the history
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
  • Loading branch information
kaiyoma authored and darcyclarke committed Feb 25, 2020
1 parent f533d61 commit 55916b1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/utils/error-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ process.on('timing', function (name, value) {
process.on('exit', function (code) {
process.emit('timeEnd', 'npm')
log.disableProgress()
if (npm.config.loaded && npm.config.get('timing')) {
if (npm.config && npm.config.loaded && npm.config.get('timing')) {
try {
timings.logfile = getLogFile()
cacheFile.append('_timing.json', JSON.stringify(timings) + '\n')
Expand Down Expand Up @@ -64,7 +64,7 @@ process.on('exit', function (code) {
log.verbose('code', code)
}
}
if (npm.config.loaded && npm.config.get('timing') && !wroteLogFile) writeLogFile()
if (npm.config && npm.config.loaded && npm.config.get('timing') && !wroteLogFile) writeLogFile()
if (wroteLogFile) {
// just a line break
if (log.levels[log.level] <= log.levels.error) console.error('')
Expand All @@ -79,7 +79,7 @@ process.on('exit', function (code) {
wroteLogFile = false
}

var doExit = npm.config.loaded && npm.config.get('_exit')
var doExit = npm.config && npm.config.loaded && npm.config.get('_exit')
if (doExit) {
// actually exit.
if (exitCode === 0 && !itWorked) {
Expand All @@ -94,7 +94,7 @@ process.on('exit', function (code) {
function exit (code, noLog) {
exitCode = exitCode || process.exitCode || code

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

Expand Down
4 changes: 2 additions & 2 deletions lib/utils/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ function errorMessage (er) {
case 'EACCES':
case 'EPERM':
const isCachePath = typeof er.path === 'string' &&
er.path.startsWith(npm.config.get('cache'))
npm.config && er.path.startsWith(npm.config.get('cache'))
const isCacheDest = typeof er.dest === 'string' &&
er.dest.startsWith(npm.config.get('cache'))
npm.config && er.dest.startsWith(npm.config.get('cache'))

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

Expand Down

0 comments on commit 55916b1

Please sign in to comment.