how to use node js module system on the clientside

how to use node js module system on the clientside  using -'javascript,node.js'

I would like to use the CommonJS module system in a clientside javascript application. I chose nodejs as implementation but can't find any tutorial or docs on how to use nodejs clientside, ie without using node application.js

I included node.js like this in my html page:

<script type="text/javascript" src="node.js"></script>

Note that I didn't make nodejs on my local machine, I'm on Windows anyway (I'm aware of the Cygwin option).
When I want to use the require function in my own javascript it says it's undefined.

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

My question is, is it possible to use nodejs like this?

asked Sep 18, 2015 by jekbishnoi
0 votes

3 Answers

0 votes

Node.js is a serverside application where you run javascript on the server. What you want to do is use the require function on the client.

Your best bet is to just write the require method yourself or use any of the other implementations that use a different syntax like requireJS.

Having done a bit of extra research it seems that no-one has written a require module using the commonJS syntax for the client. I will end up writing my own in the near future, I recommend you do the same.


One important side effect is that the require function is synchronous and thus loading large blocks of javascript will block the browser completely. This is almost always an unwanted side-effect. You need to know what you're doing if you're going to do this. The requireJS syntax is set up so that it can be done asynchronously.

answered Sep 18, 2015 by shikhar jain
0 votes

SubStack on github has a module called node-browserify.

It will compress and bundle your modules and deliver it as a single js file, but you use it just like Node.js (example from module readme):


    foo = 

From the module description:


Browser-side require() for your node modules and npm packages**

Browserify bundles everything ahead-of-time at the mount point you specify. None of this ajaxy module loading business.

More features:

  • recursively bundle dependencies of npm modules
  • uses es5-shim for browsers that suck
  • filters for {min,ugl}ification
  • coffee script works too!
answered Sep 18, 2015 by mannar kande
0 votes

If you'd like to write code for browser with same style modules as you do for Node.js, try Webmake. Take a look also at simple prototype of application build that way: SoundCloud Playlist Manager

answered Sep 18, 2015 by devkumargupta