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 ...

Get Node.js Web Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.