Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using browserify output bundles directly in node.js

Ok, so modules written for node.js can be combined into bundles with browserify.

But just in case I only have a bunch of bundles created by browserify and not the source, would it still be possible to 'require' or otherwise use these bundles and their contents in a node.js environment besides the browser? (granted that the code does not do anything browser specific)

like image 384
Flion Avatar asked Nov 26 '14 18:11

Flion


People also ask

How do I use Browserify bundles?

Bundle up your first module With Browserify you can write code that uses require in the same way that you would use it in Node. Browserify parses the AST for require() calls to traverse the entire dependency graph of your project. Drop a single <script> tag into your html and you're done!

Is Browserify a module bundler?

Browserify allows us to use node. js style modules in the browser. We define dependencies and then Browserify bundles it all up into a single neat and tidy JavaScript file.


1 Answers

Ok, so modules written for node.js can be combined into bundles with browserify.

Firstly I am not sure what you mean by this, as browserify was created to do the opposite. Browserify was made to allow the use of node's require() statements in the browser.

But just in case I only have a bunch of bundles created by browserify and not the source, would it still be possible to 'require' or otherwise use these bundles and their contents in a node.js environment besides the browser? (granted that the code does not do anything browser specific)

Yes in short, as long as the modules do not use the global window scope because window is undefined in node.js. Common helper packages like lodash, axios, moment, bluebird, and q promises all work in node.js.

Generally though, packages are often modified to work in both the browser and node.js. There is a browser attribute option in package.json files that allows you to specifically target the browser when publishing npm modules. Often files designed for the browser are minified down to one file because of how files could potentially be imported into the browser. This is not necessary with node and there may be many files in a node project.

like image 101
thomasmeadows Avatar answered Nov 15 '22 06:11

thomasmeadows