Node.js's algorithm for require (module)
In Node.js, modules are either stored in a single file, as discussed previously, or as a directory with particular characteristics. There are several ways to specify module names and several ways to organize module deployment in the filesystem. It's quite flexible, especially when used with the npm
package management system for Node.js.
Module identifiers and path names
Generally speaking, the module name is a pathname but with the file extension removed. Earlier, when we wrote require('./simple')
, Node.js knew to add .js
to the filename and load in simple.js
.
Modules whose filenames end in .js
are of course expected to be written in JavaScript. Node.js also supports binary code native libraries as Node.js modules, whose filename extension is .node
. It's outside the scope of this book to discuss implementation of native code Node.js modules. This gives you enough knowledge to recognize them when you come across one.
Some Node...