Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use require() and when to use define()?

I have being playing around with requirejs for the last few days. I am trying to understand the differences between define and require.

Define seems to allow for module separation and allow for dependency ordering to be adhere. But it downloads all the files it needs to begin with. Whilst require only loads what you need when you need it.

Can these two be used together and for what purposes should each of them be used?

like image 337
skinnybrit51 Avatar asked Feb 29 '12 22:02

skinnybrit51


People also ask

What is the use of require ()?

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.

Is RequireJS outdated?

Yes, it is still used a lot. Or is it slowly dying and needed only for maintaining legacy codes? [Comments as of early 2022] I wouldn't so much say that it is dying as there are very few projects on NPM that don't still support CommonJS.

Can you use require in a module?

“Require” is built-in with NodeJSrequire is typically used with NodeJS to read and execute CommonJS modules. These modules can be either built-in modules like http or custom-written modules. With require , you can include them in your JavaScript files and use their functions and variables.

What does require mean in JS?

The basic functionality of require is that it reads a JavaScript file, executes the file, and then proceeds to return the exports object.


2 Answers

With define you register a module in require.js that you can then depend on in other module definitions or require statements. With require you "just" load/use a module or javascript file that can be loaded by require.js. For examples have a look at the documentation

My rule of thumb:

  • Define: If you want to declare a module other parts of your application will depend on.

  • Require: If you just want to load and use stuff.

like image 113
wischan Avatar answered Sep 20 '22 10:09

wischan


From the require.js source code (line 1902):

/**  * The function that handles definitions of modules. Differs from  * require() in that a string for the module should be the first argument,  * and the function to execute after dependencies are loaded should  * return a value to define the module corresponding to the first argument's  * name.  */ 

The define() function accepts two optional parameters (a string that represent a module ID and an array of required modules) and one required parameter (a factory method).

The return of the factory method MUST return the implementation for your module (in the same way that the Module Pattern does).

The require() function doesn't have to return the implementation of a new module.

Using define() you are asking something like "run the function that I am passing as a parameter and assign whatever returns to the ID that I am passing but, before, check that these dependencies are loaded".

Using require() you are saying something like "the function that I pass has the following dependencies, check that these dependencies are loaded before running it".

The require() function is where you use your defined modules, in order to be sure that the modules are defined, but you are not defining new modules there.

like image 20
Robert Avatar answered Sep 19 '22 10:09

Robert