Let's get started.
By including modules, your primary files can contain less code and become easier to read, navigate and debug. Human readable code is a common goal that many programmers aim to achieve, which modules and libraries make much more manageable.
The code above is only the general syntax. Let's look at the syntax for including an external module from a web-based location.
The syntax for including a local file is very similar. You need to pass into the function a reference to the local address for the file you wish to include. In either case, the filename you pass in must have the .js extension.
In the above code, the dot notation used in the file address signifies the root location of the calls originating folder.
The import function performs similarly with one clear difference; it always runs the file to which it points. Furthermore, the import function can only be run at the beginning of the file, whereas the require function can include files conditionally.
The import function works the same. However, the file extension must be of the type .mjs and cannot be used to load JSON files. The syntax for the import function looks like the code in the example below.
This post covers everything you need to know to use the require function. You have learned about the syntax for both ways to use the require function to include either a local or non-local file. Moving forward, the best way to further understand the require function is to put it to use by writing some code and using require to include files in your software's file.