`var thing = require('./thing');` style requires throw Module not Found errors #227

Closed
Techwraith opened this Issue Oct 18, 2012 · 10 comments

Projects

None yet

3 participants

@Techwraith

index.js

in node_modules/test/

var test = {};
test.thing = require('./thing');

module.exports = test;

thing.js

in node_modules/test/

module.exports = function thing (foo) {
  console.log("thing", foo);
}

Command

When doing browserify -r test -o test.js and then loading test.js on the page, then doing:

var test = require('test');

Error

I get this error:

Error: Cannot find module './thing'

Any idea what might be going on here?

@PaulKinlan

I think I have a solution, but I need to test it a lot more. I believe the module resolution system incorrectly thinks that any ./* module in a module should be based off the root of the apps directory. I have a test that changes these requires into "module/thing" path syntax and it seems to work for my use-case.

@Techwraith
@PaulKinlan

Let me fork and add the fix, the problem is that it actually removes code so I think it might break other stuff.

@Techwraith
@PaulKinlan

that I need to test.

@PaulKinlan

Staged at PaulKinlan@0207df2 - all tests pass although it feels a bit hacky. For every module that is a submodule I append the target path so the data is accessed via require "/abc/thing" if the module is abc and the child module is "thing"

@Techwraith

@substack What do you think of this?

@Techwraith

Thanks @PaulKinlan This totally fixed it for me. @substack If this looks ok to you, I'd recommend merging this pull request. All tests pass and it made my use case work.

@PaulKinlan

@substack I have created a pull request for this. #231

@substack
Owner

Sorry for the delay on this and thanks for posting. v2 should fix this bug since the require rebasing with commondir doesn't happen for the module resolution since the require graph is completely resolved at bundle time instead of runtime now.

@substack substack closed this Feb 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment