Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 516
Dan Ramos Avatar asked Sep 24 '26 16:09

Dan Ramos


1 Answers

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.

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!