I’m using sentry in several node modules. I initialize it in admin.js
:
// admin.js
const Sentry = require('@sentry/node');
Sentry.init({
dsn: functions.config().sentry.dsn,
environment: functions.config().env.mode,
});
module.exports = { Sentry };
I understand that node is ‘require once’ (caches after the first time) and returns the same object after that, so that multiple requires behave as a single require.
My question relates to the order these require
statements are run, and whether in other modules I should use option #1 or #2 (or if it matters at all):
// utils.js
// option #1
const Sentry = require('@sentry/node');
testFn () {
try {
throw new Error();
} catch (err) {
Sentry.captureException(err);
}
}
// utils.js
// option #2
const { Sentry } = require('./admin');
testFn () {
try {
throw new Error();
} catch (err) {
Sentry.captureException(err);
}
}
If I use option #1, is it a possibility that Sentry won’t be properly initialized in utils.js
when captureException
is called?
If both options work, is one preferred over the other in terms of a best practice?