Some of the common modules are:
CommonJS, AMD, RequireJS, ES(ECMAScript)6 Modules. Refer to this medium article for an in-depth explanation of how these modules are different.
Table of Contents
- Syntax & Explanation
- How is require() different from import()
- Other Related Concepts
Syntax and explanation
Syntax:To include a module, the require() function is used with the name of the module:
var myVar = require('http'); //to use built-in modules Var myVar2 = require('./myLocaModule') to use local modules
2) import()import() & export() statements are used to refer to an ES module. Other modules with file types such as .json cannot be imported with these statements. They are permitted to be used only in ES modules and the specifier of this statement can either be a URL-style relative path or a package name. Also, the import statement cannot be used in embedded scripts unless such script has a type="module". A dynamic import can be used for scripts whose type is not “module”
var myVac = import("module-name");
How is require() different from import()One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() cannot be called conditionally, it always runs at the beginning of the file.
To use the require() statement, a module must be saved with .js extension as opposed to .mjs when the import() statement is used.
ES modules can be loaded dynamically via the import() function unlike require().