Common module in node.js and browser javascript

I am developing a node.js app, and I want to use a module I am creating for the node.js server also on the client side.

An example module would be circle.js:

var PI = Math.PI;

exports.area = function (r) {
  return PI * r * r;

exports.circumference = function (r) {
  return 2 * PI * r;

and you do a simple

var circle = require('./circle')

to use it in node.

How could I use that same file in the web directory for my client side javascript?


This seems to be how to make a module something you can use on the client side.



    // your code goes here

   exports.test = function(){
        return 'hello world'

})(typeof exports === 'undefined'? this['mymodule']={}: exports);

And then to use the module on the client:

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

This code would work in node:

var mymodule = require('./mymodule');


It magically lets you do that.

Webmake was made to port CommonJS/NodeJS modules to browser. Give it a try

Need Your Help

Firebase how to send Topic Notification

php android firebase firebase-cloud-messaging

I am using below script to send notification to particular users:

Objective-C Class Reference: Symbols not found error

iphone objective-c reference missing-symbols

I've been working on this iPhone app for a while now, and I had it completely finished and working. The project I was expanding on was downloaded from a subversion repository online that my profess...