I am using requireJS 2.x. I found out that some tutorials (and the official docs) sometimes use
requirejs.config({ [...] }); requirejs(["module"]) ...
and sometimes
require.config({ [...] }); require(["module"]) ...
Is there any difference between those two functions (require and requirejs)? I could not find any word about that in the docs. :(
The short answer would be: CommonJS and AMD are specifications (or formats) on how modules and their dependencies should be declared in javascript applications. RequireJS is a script loader library that is AMD compliant, curljs being another example.
require() and define() both used to load dependencies. There is a major difference between these two method. Require(): Method is used to run immediate functionalities. define(): Method is used to define modules for use in multiple locations(reuse).
2 Answers. Show activity on this post. require(["jquery"], function ($) { function doStuff(a, b) { //does some stuff } $('#the-button'). click(function() { doStuff(4, 2); }); });
1) require() In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object.
They are exactly the same.
The reason is some environments might already have a require
, in which case RequireJS doesn't overwrite it and allows usage of the library through requirejs
See this commit - https://github.com/jrburke/requirejs/commit/be45948433b053921dc6a6a57bf06d04e13b3b39
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With