From 07f44a7d7b23caa8edbed1bfa0912ea24ee3b6ed Mon Sep 17 00:00:00 2001 From: Gabe Gorelick Date: Thu, 17 Oct 2019 14:29:50 -0400 Subject: [PATCH] Make `native` non-enumerable 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 https://github.com/brianc/node-postgres/issues/1894#issuecomment-543300178 --- lib/index.js | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/lib/index.js b/lib/index.js index 8e000a378..a29ff2ff1 100644 --- a/lib/index.js +++ b/lib/index.js @@ -40,20 +40,25 @@ 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 + var native = null + Object.defineProperty(module.exports, 'native', { + configurable: true, + get () { + if (native) { + return native } - /* eslint-disable no-console */ - console.error(err.message) - /* eslint-enable no-console */ + + 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 */ + } + return native } - module.exports.native = native - return native }) }