Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use require.js on the serverside?

I use it on the client right now with Backbone.js, but I'd like to use it with node.js also.

like image 801
kevlar Avatar asked Nov 15 '11 05:11

kevlar


2 Answers

There are directions on the requirejs homepage that I found pretty useful: http://requirejs.org/docs/node.html

I also set up some examples here: https://github.com/rgarcia/node-requirejs-examples

The basic idea is that you use it just like on the client side, but for npm modules and built-in node modules, you don't use the relative path, but rather the module name. For all of your custom modules you use the relative path.

like image 135
Rafael Avatar answered Oct 19 '22 23:10

Rafael


Here is the doc for usage of requireJS on node.js

http://requirejs.org/docs/node.html

Install the node.js

npm install requirejs

Usage example

var requirejs = require('requirejs');

requirejs(['foo', 'bar'],
function   (foo,   bar) {
    //foo and bar are loaded according to requirejs
    //config, but if not found, then node's require
    //is used to load the module.
});
like image 35
steveyang Avatar answered Oct 20 '22 00:10

steveyang