Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Browserify cannot handle variable requires #2

Open
MatissJanis opened this issue Dec 3, 2016 · 10 comments
Open

Browserify cannot handle variable requires #2

MatissJanis opened this issue Dec 3, 2016 · 10 comments

Comments

@MatissJanis
Copy link

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.

@mike442144
Copy link
Owner

Hi, I'm not familiar with brwoserify, what's your advice?

@MatissJanis
Copy link
Author

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];
}

@mike442144
Copy link
Owner

@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?

@nathantbaker
Copy link

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

@mike442144
Copy link
Owner

@nathantbaker Are you using browserify ?

@nathantbaker
Copy link

No but Webpack modularizes code similar to browserfy and can uses commonJS require/export functionality: https://webpack.github.io/docs/commonjs.html

@andeersg
Copy link

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);
}

@andeersg
Copy link

Or maybe it requires some try-catch, I'm not that familiar with Webpack.

@mike442144
Copy link
Owner

mike442144 commented Jan 21, 2017

@andeersg I see, let me do it . Or can you open a pull request?

@mike442144
Copy link
Owner

Please try the 1.0.0 version to see if it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants