Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Javascript 'require'?

Tags:

javascript

I was analyzing javascript codes in a chrome extension and noticed this in it.

require("name of required class/file/module") //Not sure whether it is class,file,module

May be its because I'm newbie to programming or something,but I cant find the 'require' keyword in javascript reference or documentation.There is 'import' statement for importing modules. I googled for require for javascript but all I can find is the 'require' for node.js, the server side scripting using javascript. I am talking about chrome extension/client side script.Does anyone have any idea about this ?

like image 674
Aravind Krishna Avatar asked Feb 23 '16 04:02

Aravind Krishna


People also ask

What is require () in JavaScript?

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.

What is require () in NodeJS?

require() is used to consume modules. It allows you to include modules in your app. You can add built-in core Node. js modules, community-based modules (node_modules), and local modules too.

Can we use require in JavaScript?

“Require” is built-in with NodeJS With require , you can include them in your JavaScript files and use their functions and variables. However, if you are using require to get local modules, first you need to export them using module. exports . For example, let's assume that you have a file called blogDetails.


2 Answers

require method is part of the commonjs file and module loader. You can check more details in their Sample page.

like image 155
TeaCoder Avatar answered Oct 05 '22 02:10

TeaCoder


You may be looking for requirejs.

RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code.

like image 38
Ankur Rana Avatar answered Oct 05 '22 02:10

Ankur Rana