Browserify cannot handle variable requires
Error: Cannot find module './repo/repo_default.js'
Essentially browserify cannot handle & analyze structure like this. Generally it is considered a bad approach to use variable requires.
Hi, I'm not familiar with brwoserify, what's your advice?
Something like this should work, but I've never really encountered a problem like this. A bit of researching should be done to get to the best solution.
var repos = {
default: require('./repo/repo_default.js'),
redis: require('./repo/repo_redis.js')
};
function someFunction(repo) {
return repos[repo];
}
@MatissJanis I've tried your solution, but if somebody only need a simple seenreq in memory, the redis dependency shouldn't be installed, in this case I require the module dynamically. do you have any idea to avoid that?
Think this is related? I get this error when compiling Webpack:
ERROR in ./~/seenreq/lib/repo/repo_mongo.js Module not found: Error: Cannot resolve module 'mongodb' in /Users/nathantbaker/workspace/projects/BGGstats.com/www/node_modules/seenreq/lib/repo @ ./~/seenreq/lib/repo/repo_mongo.js 3:18-36
@nathantbaker Are you using browserify ?
No but Webpack modularizes code similar to browserfy and can uses commonJS require/export functionality: https://webpack.github.io/docs/commonjs.html
You could probably change this:
function seenreq(options){
var Repo = require("./repo/repo_"+((options && options.repo)||"default")+".js");
this.repo = new Repo(options);
var Normalizer = require("./normalizer/normalizer_"+((options&&options.normalizer)||"default")+".js");
this.normalizer = new Normalizer(options);
}
to something like this:
function seenreq(options){
var Repo;
if (typeof options.repo !== 'undefined' && options.repo == 'redis') {
Repo = require('path-to-redis');
}
else if (same check for mongo) {
Repo = require('path-to-mongo');
}
else {
Repo = require('default');
}
this.repo = new Repo(options);
var Normalizer = require("./normalizer/normalizer_"+((options&&options.normalizer)||"default")+".js");
this.normalizer = new Normalizer(options);
}
Or maybe it requires some try-catch, I'm not that familiar with Webpack.
@andeersg I see, let me do it . Or can you open a pull request?
Please try the 1.0.0 version to see if it works.