W3cubDocs

/webpack 4

Module Variables

This section covers all variables available in code compiled with webpack. Modules will have access to certain data from the compilation process through module and other variables.

module.loaded (NodeJS)

This is false if the module is currently executing, and true if the sync execution has finished.

module.hot (webpack-specific)

Indicates whether or not Hot Module Replacement is enabled and provides an interface to the process. See the HMR API page for details.

module.id (CommonJS)

The ID of the current module.

module.id === require.resolve('./file.js');

module.exports (CommonJS)

Defines the value that will be returned when a consumer makes a require call to the module (defaults to a new object).

module.exports = function doSomething() {
  // Do something...
};

This CANNOT be used in an asynchronous function.

exports (CommonJS)

This variable is equal to the default value of module.exports (i.e. an object). If module.exports gets overwritten, exports will no longer be exported.

exports.someValue = 42;
exports.anObject = {
  x: 123
};
exports.aFunction = function doSomething() {
  // Do something
};

global (NodeJS)

See node.js global.

process (NodeJS)

See node.js process.

__dirname (NodeJS)

Depending on the config option node.__dirname:

If used inside an expression that is parsed by the Parser, the config option is treated as true.

__filename (NodeJS)

Depending on the config option node.__filename:

If used inside an expression that is parsed by the Parser, the config option is treated as true.

__resourceQuery (webpack-specific)

The resource query of the current module. If the following require call was made, then the query string would be available in file.js.

require('file.js?test');

file.js

__resourceQuery === '?test';

__webpack_public_path__ (webpack-specific)

Equals the config option's output.publicPath.

__webpack_require__ (webpack-specific)

The raw require function. This expression isn't parsed by the Parser for dependencies.

__webpack_chunk_load__ (webpack-specific)

The internal chunk loading function. Takes two arguments:

  • chunkId The id for the chunk to load.
  • callback(require) A callback function called once the chunk is loaded.

__webpack_modules__ (webpack-specific)

Access to the internal object of all modules.

__webpack_hash__ (webpack-specific)

This variable is only available with the HotModuleReplacementPlugin or the ExtendedAPIPlugin. It provides access to the hash of the compilation.

__non_webpack_require__ (webpack-specific)

Generates a require function that is not parsed by webpack. Can be used to do cool stuff with a global require function if available.

DEBUG (webpack-specific)

Equals the config option debug.

© JS Foundation and other contributors
Licensed under the Creative Commons Attribution License 4.0.
https://v4.webpack.js.org/api/module-variables