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

Documentation really sketchy on how to export a module #382

Closed
moll opened this issue Apr 17, 2013 · 4 comments
Closed

Documentation really sketchy on how to export a module #382

moll opened this issue Apr 17, 2013 · 4 comments

Comments

@moll
Copy link

moll commented Apr 17, 2013

I've been going back and forth the README and code for a while now trying to understand how the damn this is supposed to be used for exposing a module in the browser.

The beep-poop example and README mention of a --require flag but that even fails in the example's build.sh as robot.js is not a module in ./node_modules, but just a file.

Then an attempt to use --require with a relative path ends up meaning you'll have to use require("./foo") in the browser as well, as opposed to just using require("foo"). Oh, hey, Optionally use a colon separator to set the target., but that's not even implemented according to #319?!

Symlink the name of your library to your index.js and relative --require that? Well, that prints out peculiar useless intermediary requires named in the style of "l5QO1E".

So, how do you package an existing index.js file so you can do require("foo") in the browser?

@ghost
Copy link

ghost commented Apr 18, 2013

If you have a foo.js:

module.exports = 555

and want require('foo') to work in arbitrary script tags, do:

$ mkdir -p node_modules/foo
$ mv foo.js node_modules/foo/index.js
$ browserify -r foo > bundle.js

Now you can require('foo') in any script tag:

<script src="bundle.js"></script>
<script>
var foo = require('foo');
console.log(foo);
</script>

which is the same as:

$ cat bundle.js <(echo "console.log(require('foo'))") | node
555

However, it's more common to just put the part that does require('foo') in a main.js and browserify up the entire thing into a single bundle:

main.js:

var foo = require('foo')
console.log(foo)
$ browserify main.js > bundle.js

Then just put a single script tag into your page:

<script src="bundle.js"></script>

@ghost ghost closed this as completed Apr 18, 2013
@moll
Copy link
Author

moll commented Apr 18, 2013

Thank you for the workaround example!

I do have to ask, why is the that common case so complicated? You'll have to make a whole fake directory structure and copy your code there for just building a regular library that exports regular functions to be used both with Node and in the browser.

A related question, the --requireflag seems to assign the require functions to a global variable named require. That's expected. But it seems to do so in a way that overwrites its previous value, which I was under the impression from the README, is supposed to be re-used to work with other browserified code. Or am I reading the code wrong?

@ghost
Copy link

ghost commented Apr 18, 2013

To make require('foo') work it is exactly the same in node. Non-relative paths get resolved in node_modules/. If you want a relative path you can just do require('./foo.js') in a main.js entry file. If you have explicit entry points everything is very simple because that environment is completely self-contained so it's much easier to support.

The global require definition uses the previous value in order to cascade across multiple bundles.

@moll
Copy link
Author

moll commented Apr 18, 2013

Umm, I get your simplicity point, but that shouldn't make exporting the library in the current working directory that difficult and inelegant. What's with the target option to --require? That seems to be something that would make "make this library I'm building work in the browser" possible.

You're right, I was reading the code wrong and assumed the require inside the returned function ends up referring to itself, which it doesn't.

This issue was closed.
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

1 participant