JavaScript NodeJS and Browser Modules

What do you think of this post?
  • Awesome (57.1%)
  • Interesting (14.3%)
  • Useful (28.6%)
  • Boring (0.0%)
  • Sucks (0.0%)

I want galojs to work on the browser and nodejs.

So I started to rewrite the galojs modules to work on nodejs. Apparently there is two types/specifications of modules in JS the CommonJS modules and AMD (Asynchronous Module Definition)

I think nodejs uses a improved version of CommonJS modules and a module can be something like this:

function Variable (varname) {
	this.varname = varname;
};


function Abstraction (x, M) {
	this.x = x;
	this.M = M;
};

function Application (M, N) {
	this.M = M;
	this.N = N;
};

...

One of the nice features of modules is that you can export only the important parts and
keep other functions/variables as “private”.

module.exports = {
	Variable: Variable,
	Abstraction: Abstraction,
	Application: Variable,
	parse: parse,
	bind: bind,
	std_lib: std_lib,
	std_rename: std_rename
};

After the module is defined you can import it like this:

var Lambda = require ("lambda.js");

And you simple use it like this:

var myvar = new Lambda.Variable("myvar");

How to use modules in the browser?

There is probably more than one way to use nodejs modules in the browser, my favorite is to “compile” the modules to the browser, most of this “compilers” put all the code in only one file witch is great because you only need to include it once.

There is a few compilers out there the most popular is browserify and webmake, I prefer the last one (webmake) because the generated code is much more clear to me.

After js code is compiled you include it as a normal js script:


The other cool thing is that using webmake you can use require(…) and modules to code to the browser. In my case I made a few modules to node and main.js file to run with node, and then I made other js file to just work in the browser, it looks something like this:

var Lambda = require("../node/lib/lambda.js");
var AST = require("../node/lib/ast.js");

function print (msg) {
	if (msg !== undefined) { 
	  msg = msg.toString(); 
	}
	else {
		msg = '#undef'
	}
	$("#output").append('
'+msg+'
'); } $(document).ready(function() { print("start"); function updateCode() { var code = $("#code").val(); try { $("#output").html(""); eval(code); } catch (e) { print(e); throw e; } }; $("#code").bind('input propertychange', function () { updateCode(); }); updateCode(); });

I am using jquery that I included in my html page, and using require to import my node modules. When compiling this with webmake all dependencies are solved and put in only one file to be included in my html page like this.

Conclusions

Using modules on nodejs and the browser is very simple and it makes the code much more clean.
I am really enjoying the webmake, its great to be able to import js files in js, I had tried other ways, jslibs/functions, to include files with js but I always found some issues when debugging in the browser, I didn’t debug anything (yet) with webmake generated files but since they are really clean I think that wouldn’t be a problem…

Anyway even if you are not thinking about writing modules to nodejs webmake is still a great tool if you are thinking in writing js as modules in the browser.

What do you think of this post?
  • Awesome (57.1%)
  • Interesting (14.3%)
  • Useful (28.6%)
  • Boring (0.0%)
  • Sucks (0.0%)

4 thoughts on “JavaScript NodeJS and Browser Modules

  1. It’s the best time to make a few plans for the long run and it is time to be happy.
    I have read this put up and if I may I wish to recommend you some fascinating issues or advice.
    Perhaps you can write next articles referring to this article.

    I want to read even more things about it!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>