JavaScript library compatible with RequireJs, jQuery and Node.js

Last night, I was updating is_email JavaScript library so that it supports jQuery, RequireJS and Node.js. I found information for each library but on different blogs. I didn’t find complete set information at one place. As each of my blog says, I would be putting it here  in the hope that it would be accessible all at one place.

I would explain with demo example. First wrap whole library in a function, so that even though my library has thousand functions, only the one I needed would go in global scope. In below example I’m creating a library function which checks if variable is undefined.

Now I have wrote my function, now I would want it to be enabled or compatible with RequireJS. Check if define is a function and it is a AMD loader then define our function. That’s it. Checkout below example.

You want that your library can also be used as Node module. There are few more steps to create Node module. Checkout tutorial for how Node.js module is created. In below I would just show to expose your lib/function to Node as module.

Check if “module” is defined and “exports” is defined, if yes then we assume it is Node whose using/calling a JS file or JS funcion.

Last, how to enable support for jQuery. There are many ways to do expose/add your function to jQuery. I would demo two methods.

If you are looking for complete tutorial for jQuery plugin, then you are on wrong blog as I’m just demoing how to enable jQuery support. Please go to for writing your own jquery plugin.

We can combine all these above 3 examples into one and enable support /compatibility for RequireJS, jQuery and Node. One of the working example is is_email library which validates your email against relevant RFCs.

If I’m missing some information or you have any suggestions, doubts or questions do write your comments/thoughts on the blog..

Leave a Reply