3

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?

Dan Ramos
  • 1,092
  • 2
  • 19
  • 35
  • 1
    If you mean that you want to create a module that can be used with both amd and commonjs, it has been answered here: http://stackoverflow.com/questions/13673346/supporting-both-commonjs-and-amd – Andreas Hultgren May 21 '13 at 13:50

1 Answers1

2

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.