Using require.js module in node AND browser

I would like to create a (require.js style) AMD module that can be used in the browser and in node. What is the best way to do this? I keep seeing references to r.js, but still not 100% sure on how to use it, or if it is necessary for my situation.

Also, when including this module in node, do I still run require('module-name'), or will this change as well?

First things first: AMD basics, What all you can do with them, How to optimize them

In extremely simple terms

  • AMD modules are reusable JS code. Think of them as functions kept in separate files.
  • AMD loaders are central functions which call all other functions (modules). Think of them as "main" method in C or Java.
  • RequireJS is a framework which pulls all this scattered code and stitches it in a usable form.
  • RequireJS works in a browser. As a result, all your code is "stitched" together in a web browser.
  • r.js works offline (in a web server or on your development machine) to "stitch" all the code offline so that when it reaches a web browser, it's already "stitched".
  • Use of RequireJS lib is a must no matter you want to "stitch" your code in browser or you want to serve your code "pre-stitched".
  • Use of r.js is optional. It's needed only if you want to increase performance and decrease HTTP calls.