I have a bit of code where I try to require a module, and if it is not present, fall back to a default.
var ourLayout = null;
try {
ourLayout = require('swishLayout');
} catch (e) {
ourLayout = require('drabLayout');
}
It works just fine, except for this message when starting the app :
Unable to resolve some modules:
"swishLayout" in /home/yourself/yourApp/client/configs/theme.jsx (web.browser)
If you notice problems related to these missing modules, consider running:
meteor npm install --save swishLayout
Can I suppress that message OR do this “object overloading” trick in a better way?
Will 1.5 provide a better way?
The only thing I can think of is to patch my package.json
just before running the app … but it’s kinda crude.
I’ve been struggling with this for quite a long time in unifroms. Try this:
let x;
try {
const r = require;
x = r('package/x');
} catch (error) {/* Do something else... */}
Source.
2 Likes
try to delete node_modules/ folder and again run command meteor npm install
. Hope it may help you.
Nice one! Exactly what I needed!
Thank you!
It seems I spoke to soon. 
Yes the error message went away, but require
did not like to be treated that way, and loaded nothing. It may be because the dependency was not really
there, it’s a symlink created with npm link
.
I fact, my original example was over simplified.
This does do what I need, but I am not happy with it :
function requireLayout(isModule) {
if ( isModule.toLowerCase() === 'true' ) {
return require('mmks_layout').LayoutDefault;
}
return require('../modules/layout/containers/Layout.jsx').default;
}
const LayoutDefault = requireLayout(Meteor.settings.public.IS_OVERRIDDEN);
export { LayoutDefault };
I’ve noticed that my solution won’t work if the module is not loaded anywhere in the application - it has to be statically analysed.
This trick used to work for me, but suddenly does not.
const r = require; // using r shortcut suppresses Raven dependency warnings
Raven = process.env.NODE_ENV === 'development'
? r('raven')
: require('raven');
Does anyone know why?
Yes. I found some references to that, but it would not work for me.
I suspect it’s rules tightening to conform to ES2016.