File modules are modules loaded from the file system.
We can use absolute paths, relative paths, or the
to reference file modules.
Node supports Windows-style file paths.
On Windows, Node allows the slash and backslash characters (/ and \) to be used interchangeably.
Module names that begin with a slash (/) are treated as absolute paths.
require("/some/path/foo"); loads file module
using an absolute path.
Example Module Paths Valid on Windows
require("/some/path/foo"); require("C:/some/path/foo"); require("C:\\some\\path\\foo"); require("\\some/path\\foo");
Module paths that begin with one or two dots (. or ..) are interpreted as relative paths. They are considered relative to the file that called require().
The following code shows three examples of relative module paths.
foofrom the same directory as the calling script.
foofrom parent directory.
require("./sub/foo");loads foo from a subdirectory, sub, of the calling script's directory.
If a module path does not correspond to a core module, an absolute path,
or a relative path, then Node begins searching in
Node begins with the calling script's parent directory and appends
If the module is not found, Node moves one level up the directory tree,
/node_modules, and searches again.
This pattern is repeated until the module is located or the root of the directory structure is reached. If Node is unable to find a match, an error is thrown.
If require() does not find an exact match, it attempts to add
.node file extensions.