seenreq icon indicating copy to clipboard operation
seenreq copied to clipboard

Browserify cannot handle variable requires

Open MatissJanis opened this issue 9 years ago • 10 comments

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.

MatissJanis avatar Dec 03 '16 09:12 MatissJanis

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

mike442144 avatar Dec 08 '16 07:12 mike442144

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 avatar Dec 08 '16 07:12 MatissJanis

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

mike442144 avatar Dec 08 '16 07:12 mike442144

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 avatar Dec 10 '16 01:12 nathantbaker

@nathantbaker Are you using browserify ?

mike442144 avatar Dec 10 '16 05:12 mike442144

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

nathantbaker avatar Dec 10 '16 15:12 nathantbaker

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 avatar Jan 20 '17 09:01 andeersg

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

andeersg avatar Jan 20 '17 10:01 andeersg

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

mike442144 avatar Jan 21 '17 11:01 mike442144

Please try the 1.0.0 version to see if it works.

mike442144 avatar Dec 06 '17 08:12 mike442144