Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check npm.config before accessing its members #508

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/utils/error-handler.js
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')) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could dry this up by adding something like this near the top, no?

const config = npm.config || { loaded: false }

And then replacing npm.config with config in the lines below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, this approach seems to introduce more stack traces and more errors. Is this what we want?

$ ./bin/npx-cli.js foo
Command failed: node.exe npm-cli.js config get cache --parseable
Error: Exit prior to config file resolving.
    at errorHandler (...\lib\utils\error-handler.js:149:16)
    at ...\bin\npm-cli.js:150:20
    at get (...\lib\config.js:171:3)
    at EventEmitter.config (...\lib\config.js:68:14)
    at Object.commandCache.(anonymous function) (...\lib\npm.js:156:13)
    at EventEmitter.<anonymous> (...\bin\npm-cli.js:131:30)
    at process._tickCallback (internal/process/next_tick.js:61:11)
npm ERR! Exit prior to config file resolving.
Error: npm.load() required
    at Object.get (...\lib\npm.js:59:13)
    at errorHandler (...\lib\utils\error-handler.js:207:14)
    at ...\bin\npm-cli.js:150:20
    at get (...\lib\config.js:171:3)
    at EventEmitter.config (...\lib\config.js:68:14)
    at Object.commandCache.(anonymous function) (...\lib\npm.js:156:13)
    at EventEmitter.<anonymous> (...\bin\npm-cli.js:131:30)
    at process._tickCallback (internal/process/next_tick.js:61:11)
npm ERR! npm.load() required
...\lib\npm.js:59
      throw new Error('npm.load() required')
      ^

Error: npm.load() required
    at Object.get (...\lib\npm.js:59:13)
    at process.errorHandler (...\lib\utils\error-handler.js:207:14)
    at process.emit (events.js:198:13)
    at process._fatalException (internal/bootstrap/node.js:496:27)

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
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