Skip to content

Commit 45a427e

Browse files
committed
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 brianc#1894 (comment)
1 parent 8eca181 commit 45a427e

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

packages/pg/lib/index.js

+20-13
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,27 @@ if (typeof process.env.NODE_PG_FORCE_NATIVE !== 'undefined') {
4444
module.exports = new PG(Client)
4545

4646
// lazy require native module...the native module may not have installed
47-
module.exports.__defineGetter__('native', function () {
48-
delete module.exports.native
49-
var native = null
50-
try {
51-
native = new PG(require('./native'))
52-
} catch (err) {
53-
if (err.code !== 'MODULE_NOT_FOUND') {
54-
throw err
47+
Object.defineProperty(module.exports, 'native', {
48+
configurable: true,
49+
get () {
50+
var native = null
51+
try {
52+
native = new PG(require('./native'))
53+
} catch (err) {
54+
if (err.code !== 'MODULE_NOT_FOUND') {
55+
throw err
56+
}
57+
/* eslint-disable no-console */
58+
console.error(err.message)
59+
/* eslint-enable no-console */
5560
}
56-
/* eslint-disable no-console */
57-
console.error(err.message)
58-
/* eslint-enable no-console */
61+
62+
// overwrite module.exports.native so that getter is never called again
63+
Object.defineProperty(module.exports, 'native', {
64+
value: native
65+
})
66+
67+
return native
5968
}
60-
module.exports.native = native
61-
return native
6269
})
6370
}

0 commit comments

Comments
 (0)