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

Can't expose require created with Readable stream #908

Closed
lukekarrys opened this issue Sep 17, 2014 · 7 comments
Closed

Can't expose require created with Readable stream #908

lukekarrys opened this issue Sep 17, 2014 · 7 comments

Comments

@lukekarrys
Copy link

I'm trying to add a readable stream to a bundle and then require it by the exposed name in main.js.

main.js

var bar = require('m1');

The following code produces an error Error: Cannot find module 'm1'.

var Browserify = require('browserify');
var Readable = require('stream').Readable;

function stringStream(content) {
    var s = new Readable();
    s.push(content);
    s.push(null);
    return s;
}

var b = new Browserify();
b.require(stringStream('module.exports = 5'), {basedir: '.', expose: 'm1'});
b.add('./main.js');
b.bundle().pipe(process.stdout);

Should this work?

@lukekarrys
Copy link
Author

Also while testing this, I came across some other strange behavior. If I create a bundle using require(file, {expose: 'm1') before I create a bundle with a stream, then it works.

I captured the behavior in a gist. It seems like Browserify is storing some state for subsequent calls to browserify().

@mikalai-s
Copy link

Experiencing the same issue.

@lukekarrys Did you find a workaround?

@lukekarrys
Copy link
Author

@mikalai-silivonik Unfortunately, I didn't find a workaround yet that allows a stream to be required.

@tad-lispy
Copy link

I'm running into this as well. Just spent three days trying to inject configuration prepared at build time. Everything seems to work (no errors), but code from the stream doesn't appear in bundled javascript. b.add(stream) doesn't work as well.

I'd be glad to hear about any solution - maybe another way to inject code generated in gulp script into a bundle? Obvious one is to dump it into temp file, but it doesn't seem very elegant.

@terinjokes
Copy link
Contributor

@lukekarrys You've correctly exposed the module, and Browserify will use it when generating the bundle.

The error you're getting "Error: Cannot find module 'm1'" comes from module-deps which implements the node module resolution algorithm, and searches for "m1" in the node_modules directories, as well as by looking at the "browser" field in package.json. It doesn't know about any other modules you're dynamically generating and handing to Browserify.

What I recommend is to inform module-deps that it shouldn't attempt to resolve the module by using b.exclude('m1');. I've written up an example, with a few extra notes, in a Gist.

@lukekarrys
Copy link
Author

Thank you @terinjokes! That is soooo helpful 🎉

I'm gonna go ahead and close this since I now get that the issue isn't in browserify.

@terinjokes
Copy link
Contributor

That is soooo helpful 🎉

I try, and often fail, to provide helpful answers. I regret when I don't, but glad to have made you happy here. 😄

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

No branches or pull requests

4 participants