Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKyle Getz <kgetz@arista.com>2019-11-20 20:30:31 +0300
committerDarcy Clarke <darcy@darcyclarke.me>2020-02-25 18:43:15 +0300
commit55916b130ef52984584678f2cc17c15c1f031cb5 (patch)
treee8e929e0f2ff906983dd0b0a05d498c155caee44
parentf533d61eb27c484fae84571fbea8bc1d83acb42b (diff)
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: https://github.com/npm/cli/pull/508 Credit: @ Close: #508 Reviewed-by: @Darcy Clarke
-rw-r--r--lib/utils/error-handler.js8
-rw-r--r--lib/utils/error-message.js4
2 files changed, 6 insertions, 6 deletions
diff --git a/lib/utils/error-handler.js b/lib/utils/error-handler.js
index 39e0035c2..3e6f176ef 100644
--- a/lib/utils/error-handler.js
+++ b/lib/utils/error-handler.js
@@ -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')
@@ -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('')
@@ -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) {
@@ -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
diff --git a/lib/utils/error-message.js b/lib/utils/error-message.js
index 12f304d1e..3faa78f30 100644
--- a/lib/utils/error-message.js
+++ b/lib/utils/error-message.js
@@ -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'