Skip to content

Commit

Permalink
Make native non-enumerable
Browse files Browse the repository at this point in the history
Making it non-enumerable means less spurious "Cannot find module"
errors in your logs when iterating over `pg` objects.

`Object.defineProperty` has been available since Node 0.12.

See brianc#1894 (comment)
  • Loading branch information
gabegorelick committed Oct 17, 2019
1 parent fde5ec5 commit 0b9b334
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,23 @@ if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
module.exports = new PG(Client)

// lazy require native module...the native module may not have installed
module.exports.__defineGetter__('native', function () {
delete module.exports.native
var native = null
try {
native = new PG(require('./native'))
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err
Object.defineProperty(module.exports, 'native', {
enumerable: false,
get () {
delete module.exports.native
var native = null
try {
native = new PG(require('./native'))
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err
}
/* eslint-disable no-console */
console.error(err.message)
/* eslint-enable no-console */
}
/* eslint-disable no-console */
console.error(err.message)
/* eslint-enable no-console */
module.exports.native = native
return native
}
module.exports.native = native
return native
})
}

0 comments on commit 0b9b334

Please sign in to comment.