I'm using clim and I replace the console object with it. But I only want to replace it if the module exists.
try {
var console = require('clim')();
} catch(err) {}
console.log('checking..');
If the module doesn't exists it makes the console undefined.
Strangely, saving the console object and replacing doesn't work either
var console_backup = console;
try {
var console = require('clim')();
} catch(err) {
if (err) var console = console_backup;
}
console.log('checking..');
Still throws error (console goes undefined) when clim doesn't exist.
http://runnable.com/U8vlFEpIYtkiV2N9/24850946-console-for-node-js
How to make work replacing the console with clim only when it exists?
Your second attempt is close, but you need to explicitly identity that you want to reference the global
consolewhen settingconsole_backupor it will reference the hoisted, localconsolevariable, even though you haven't declared it yet:or simplify it to: