How do I use a NodeJS package with RequireJS?

I would like to use the the node-bencoding package with my current RequireJS project setup, but I have been unable to get it configured.

I have followed these instructions and have ran:

npm install requirejs

npm install node-bencoding

Then in my app.js file I had changed it:

var requirejs = require('requirejs');

// Place third party dependencies in the lib folder
//
// Configure loading modules from the lib directory,
// except 'app' ones,
requirejs.config({
    nodeRequire: require,
    "baseUrl": "assets/js/lib",
    "paths": {
        "app": "../app",
        "jquery": "https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min",
        "angularjs": "https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.min"
    },
});

However when I load the page I get the error:

Error: Module name "requirejs" has not been loaded yet for context: _. Use require([]) http://requirejs.org/docs/errors.html#notloaded

I'm not exactly sure where I should have my node_modules directory. My directory structure is as follows: all my JS files are contained within src/assets/js - there is assets/js/app and assets/js/lib as is the RequireJS convention. Currently I have put my node_modules directory in src/.

Looks like you are trying to use it in a browser. And your application is not server side JavaScript, so RequireJS usage sample in a Node does not apply. In this case you would like to use node only to optimize your scripts.

I recently blogged about Understanding AMD & RequireJS, it might be useful.