Skip to content

Commit 18a8e14

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 fde5ec5 commit 18a8e14

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

lib/index.js

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

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

0 commit comments

Comments
 (0)