The module scope
History
__dirname
History
stringThe directory name of the current module. This is the same as the
path.dirname() of the __filename.
Example: running node example.js from /Users/mjr
console.log(__dirname); // Prints: /Users/mjr console.log(path.dirname(__filename)); // Prints: /Users/mjr
__filename
History
stringThe file name of the current module. This is the current module file's absolute path with symlinks resolved.
For a main program this is not necessarily the same as the file name used in the command line.
See __dirname for the directory name of the current module.
Examples:
Running node example.js from /Users/mjr
console.log(__filename); // Prints: /Users/mjr/example.js console.log(__dirname); // Prints: /Users/mjr
Given two modules: a and b, where b is a dependency of
a and there is a directory structure of:
/Users/mjr/app/a.js/Users/mjr/app/node_modules/b/b.js
References to __filename within b.js will return
/Users/mjr/app/node_modules/b/b.js while references to __filename within
a.js will return /Users/mjr/app/a.js.
exports
History
ObjectA reference to the module.exports that is shorter to type.
See the section about the exports shortcut for details on when to use
exports and when to use module.exports.
module
History
moduleA reference to the current module, see the section about the
module object. In particular, module.exports is used for defining what
a module exports and makes available through require().
require(id): any
Used to import modules, JSON, and local files. Modules can be imported
from node_modules. Local modules and JSON files can be imported using
a relative path (e.g. ./, ./foo, ./bar/baz, ../foo) that will be
resolved against the directory named by __dirname (if defined) or
the current working directory. The relative paths of POSIX style are resolved
in an OS independent fashion, meaning that the examples above will work on
Windows in the same way they would on Unix systems.
// Importing a local module with a path relative to the `__dirname` or current // working directory. (On Windows, this would resolve to .\path\myLocalModule.) const myLocalModule = require('./path/myLocalModule'); // Importing a JSON file: const jsonData = require('./path/filename.json'); // Importing a module from node_modules or Node.js built-in module: const crypto = require('node:crypto');
ObjectModules are cached in this object when they are required. By deleting a key
value from this object, the next require will reload the module.
This does not apply to native addons, for which reloading will result in an
error.
Adding or replacing entries is also possible. This cache is checked before
built-in modules and if a name matching a built-in module is added to the cache,
only node:-prefixed require calls are going to receive the built-in module.
Use with care!
const assert = require('node:assert'); const realFs = require('node:fs'); const fakeFs = {}; require.cache.fs = { exports: fakeFs }; assert.strictEqual(require('fs'), fakeFs); assert.strictEqual(require('node:fs'), realFs);
ObjectInstruct require on how to handle certain file extensions.
Process files with the extension .sjs as .js:
require.extensions['.sjs'] = require.extensions['.js'];
Deprecated. In the past, this list has been used to load non-JavaScript modules into Node.js by compiling them on-demand. However, in practice, there are much better ways to do this, such as loading modules via some other Node.js program, or compiling them to JavaScript ahead of time.
Avoid using require.extensions. Use could cause subtle bugs and resolving the
extensions gets slower with each registered extension.
The Module object representing the entry script loaded when the Node.js
process launched, or undefined if the entry point of the program is not a
CommonJS module.
See "Accessing the main module".
In entry.js script:
console.log(require.main);
node entry.js
Module { id: '.', path: '/absolute/path/to', exports: {}, filename: '/absolute/path/to/entry.js', loaded: false, children: [], paths: [ '/absolute/path/to/node_modules', '/absolute/path/node_modules', '/absolute/node_modules', '/node_modules' ] }
require.resolve(request, options?): string
stringObjectstring[]$HOME/.node_modules, which are
always included. Each of these paths is used as a starting point for
the module resolution algorithm, meaning that the node_modules hierarchy
is checked from this location.stringUse the internal require() machinery to look up the location of a module,
but rather than loading the module, just return the resolved filename.
If the module can not be found, a MODULE_NOT_FOUND error is thrown.
require.resolve.paths(request): string[] | null
Returns an array containing the paths searched during resolution of request or
null if the request string references a core module, for example http or
fs.