Site icon Tutorial

The module wrapper

Before a module’s code is executed, Node.js will wrap it with a function wrapper that looks like the following:

(function(exports, require, module, __filename, __dirname) {

// Module code actually lives in here

});

By doing this, Node.js achieves a few things:

__dirname

Added in: v0.1.27. It is <string>

The 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

Added in: v0.0.1 It is <string>

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

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:

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

Added in: v0.1.12 It is <Object>. A 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

Added in: v0.1.16 It is <module>. A 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)

Added in: v0.1.13 It is id <string> module name or path. It returns, <any> exported module content. 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.

// Importing a local module:

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(‘crypto’);

require.cache – Added in: v0.3.0 It is <Object>. Modules are cached in this object when they are required. By deleting a key value from this object, the next require will reload the module. Note that this does not apply to native addons, for which reloading will result in an error.

require.main – Added in: v0.1.17 It is <module>. The Module object representing the entry script loaded when the Node.js process launched. See “Accessing the main module”. In entry.js script:

console.log(require.main);

node entry.js

Module {

id: ‘.’,

exports: {},

parent: null,

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]) – Updated in v8.9.0. The  request is <string> for module path to resolve and options is <Object>, as

It returns <string>. Use the internal require() machinery to look up the location of a module, but rather than loading the module, just return the resolved filename.

 require.resolve.paths(request) – Added in: v8.9.0

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

Exit mobile version