require()¶
Description¶
The require()
function is used to load a module. Unlike the include()
function, which loads a file and returns it as a string, the require()
function returns an object that contains the exported data from the loaded file.
Syntax¶
Parameters¶
Parameter | Type | Description |
---|---|---|
path |
String |
The path to the script file that needs to be loaded. |
Path Types¶
Type of Path | Example | Description |
---|---|---|
Absolute | "C:/scripts/library.js" |
The full path to a file on disk. |
Relative | "../config/settings.js" |
A path relative to the current executing file. |
File Extension Support
You can omit the .js
file extension when specifying the path. For example, require("utils")
automatically finds the file utils.js
.
Return value¶
Type | Description |
---|---|
Object |
If the file does not have explicit exports, an empty object ({} ) is returned. |
Example¶
JS | |
---|---|
See also