jsdf/browserify-incremental

Custom NODE_PATH breaks --require

joshfrench opened this issue · 8 comments

I'm getting an ENOENT: can't open... error when I try to use --require in conjunction with a custom NODE_PATH (Rails app, can't get around the path issue.) The cachefile gets written initially, but the next time it's read it throws an error trying to read whatever file is passed to --require.

Here's a repo: https://github.com/joshfrench/browserifyinc-demo

To reproduce:

  1. git clone && npm install
  2. npm run build three times.

I suspect #8 is making it so you have to run the build an extra time up front, but once the cache actually gets read the error will occur.

I've been trying to chase this down with no luck. Any clues as to where I should start digging?

jsdf commented

Oh, is it a maximum open files issue? What OS are you on? Have you tried increasing the operating system's ulimit?

Seems unlikely, I'm able to reproduce on a minimal repo. My first thought was that the cache file just needed to use absolute paths, but if that's the case I haven't been able to find the right combination. (I'm also pretty inexperienced with Node, so it's entirely possible I've overlooked something obvious.)

I'm having the same issue. @joshfrench - were you able to solve this?

I haven't been able to figure it out, unfortunately. We've just stopped using browserify-incremental, since the only workaround was to delete the cache file between changes and then recompile the bundle from scratch anyway :P

@joshfrench - I was able to find a workaround.

Because --require is only broken for libraries in the node_modules folder, I was able to create a file within my own project that referenced all of the files that I want to be globals.

So it looks like this:

// globals.js
module.exports = {
  jquery: require('jquery'),
  lodash: require('lodash'),
  react: require('react')
};

And I can --require this file without any issues.

> browserifyinc -r my-project/globals ...

So if I need to reference these libraries globally, I can just do this:

window.$ = require('globals').jquery;
window._ = require('globals').lodash;
window.React = require('globals').react;
jsdf commented

@joshfrench Just realised this was related to #7 as the specific version of browserify which is bundled with browserify-incremental has this issue, but a later version seems to have fixed it (I'm guessing it was ultimately caused by browserify/module-deps#30).

I've released a new version which removes the fixed browserify dependency version. Whatever version of browserify you install alongside browserify-incremental will be used. Using a recent version seems to fix your issue.

There's still something funky here, although at this point I don't know if it's browserify-incremental or module-deps or just the universe telling me to quit my job and open a nice cafe on the beach instead.

I can rebuild the cache without error now, but the resulting JS always throws RangeError: Maximum call stack size exceeded no matter what I'm trying to require. Here's a sample bundle with a single module (minus the require preamble.) The error is raised at apply(exports,arguments).

{"/Users/josh/src/browserifyinc-demo/in.js":[function(require,module,exports){
require('foo');
},{"foo":"foo"}],"foo":[function(require,module,exports){
module.exports = 'FOO';
},{}],"foo":[function(require,module,exports){
arguments[4]["foo"][0].apply(exports,arguments)
},{}]},{},["/Users/josh/src/browserifyinc-demo/in.js"]

I've updated my sample repo, to reproduce:

  1. git clone && npm install
  2. npm run clean
  3. npm run build && node out.js three times (still looks like the very first call doesn't actually write browserify-cache.json.)