Aerodynamika
Aerodynamika

Reputation: 8413

How to launch the script that was Browserified from a browser

I browserified a module that takes a value and returns a new one.

The original .js file was:

 module.exports = function (term) {
      return term + ' blabla';
 }

If I want to call it from Node.Js I'd simply include it as in

var foo = require('./my-file.js');
foo('no'); // returns 'no blabla'

But how do I call this same function from a browser if I include the browserify-generated file in <script src="/javascripts/new-file.js"></script>?

Thank you!

Upvotes: 0

Views: 109

Answers (2)

Justin Howard
Justin Howard

Reputation: 5643

You want to use the --standalone flag for browserify. From the documentation:

Generate a UMD bundle for the supplied export name. This bundle works with other module systems and sets the name given as a window global if no module system is found.

So if you use the --standalone flag,

browserify --standalone my_global_name my-file.js > new-file.js

you will be able to use the window.my_global_name property to access your function.

Upvotes: 1

Scimonster
Scimonster

Reputation: 33399

You need to compile with the -r flag, to set it up as exposing for requiring.

browserify -r my-file.js > new-file.js

Then, in your script, you should be able to do:

var foo = require('./my-file.js');
foo('no'); // returns 'no blabla'

For more information, you can read the documentation.

Upvotes: 0

Related Questions