A quick summary of all methods and variables available in code compiled with webpack.

Basic

require CommonJs

  1. require(dependency: String)

Returns the exports from a dependency. The call is sync. No request to the server is fired. The compiler ensures that the dependency is available.

Style: CommonJs

Example:

  1. var $ = require("jquery");
  2. var myModule = require("my-module");

define (with factory)

  1. define([name: String], [dependencies: String[]], factoryMethod: function(...))

The name argument is ignored. If the dependencies array is provided, the factoryMethod will be called with the exports of each dependency (in the same order). If dependencies is not provided the factoryMethod is called with require, exports and module (for compatibility!). If the factoryMethod returns a value, this value is exported by the module. The call is sync. No request to the server is fired. The compiler ensures that each dependency is available.

Style: AMD

Example:

  1. define(["jquery", "my-module"], function($, myModule) {
  2. // Do something with $ and myModule.
  3. // Export a function
  4. return function doSomething() {
  5. // Do something
  6. };
  7. });

Note: Can NOT be used in an async function.


module.exports

This value is returned, when that module is required. It’s default value is a new object.

Style: CommonJs

Example:

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

Note: Can NOT be used in an async function.


exports

The exported object. It’s the default value of module.exports. If module.exports gets overwritten, exports will no longer be exported.

Style: CommonJs

  1. exports.someValue = 42;
  2. exports.anObject = {
  3. x: 123
  4. };
  5. exports.aFunction = function doSomething() {
  6. // Do something
  7. };

Note: Using it in an async function may not have the expected effect.


define (with value)

  1. define(value: !Function)

Just exports the provided value. The value cannot be a function.

Style: AMD (for compatibility!)

Example:

  1. define({
  2. answer: 42
  3. });

Note: Can NOT be used in an async function.


export (label)

  1. export: value

Export the defined value. The label can occur before a function declaration or a variable declaration. The function name or variable name is the identifier under which the value is exported.

Style: Labeled modules dependencies.LabeledModulesPlugin

Example:

  1. export: var answer = 42;
  2. export: function method(value) {
  3. // Do something
  4. };

Note: Using it in an async function may not have the expected effect.


require label

  1. require: "dependency"

Make all exports from the dependency available in the current scope. The require label can occur before a string. The dependency must export values with the export label. CommonJs or AMD modules cannot be consumed.

Style: Labeled modules dependencies.LabeledModulesPlugin

Example:

  1. // in dependency
  2. export: var answer = 42;
  3. export: function method(value) {
  4. // Do something
  5. };
  1. require: "dependency";
  2. method(answer);

require.resolve

  1. require.resolve(dependency: String)

Returns the module id of a dependency. The call is sync. No request to the server is fired. The compiler ensures that the dependency is available.

The module id is a number in webpack (in contrast to node.js where it is a string, the filename).

Style: CommonJs

Example:

  1. var id = require.resolve("dependency");
  2. typeof id === "number";
  3. id === 0 // if dependency is the entry point
  4. id > 0 // elsewise

module.id

The module id of the current module.

Style: CommonJs

Example:

  1. // in file.js
  2. module.id === require.resolve("./file.js")

Advanced

require.cache

Multiple requires to the same module result in only one module execution and only one export. Therefore a cache in the runtime exists. Removing values from this cache cause new module execution and a new export. This is only needed in rare cases (for compatibility!).

Style: CommonJs

  1. var d1 = require("dependency");
  2. require("dependency") === d1
  3. delete require.cache[require.resolve("dependency")];
  4. require("dependency") !== d1
  1. // in file.js
  2. require.cache[module.id] === module
  3. require("./file.js") === module.exports
  4. delete require.cache[module.id];
  5. require.cache[module.id] === undefined
  6. require("./file.js") !== module.exports // in theory; in praxis this causes a stack overflow
  7. require.cache[module.id] !== module

require.context

  1. require.context(directory:String, includeSubdirs:Boolean /* optional, default true */, filter:RegExp /* optional */)

Example:

  1. var context = require.context('components', true, /\.html$/);
  2. var componentA = context.resolve('componentA');

Style: webpack


require.ensure

  1. require.ensure(dependencies: String[], callback: function([require]), [chunkName: String])

Download additional dependencies on demand. The dependencies array lists modules that should be available. When they are, callback is called. If the callback is a function expression, dependencies in that source part are extracted and also loaded on demand. A single request is fired to the server, except if all modules are already available.

This creates a chunk. The chunk can be named. If a chunk with this name already exists, the dependencies are merged into that chunk and that chunk is used.

Style: CommonJs

Example:

  1. // in file.js
  2. var a = require("a");
  3. require.ensure(["b"], function(require) {
  4. var c = require("c");
  5. });
  6. require.ensure(["d"], function() {
  7. var e = require("e");
  8. }, "my chunk");
  9. require.ensure([], function() {
  10. var f = require("f");
  11. }, "my chunk");
  12. /* This results in:
  13. * entry chunk
  14. - file.js
  15. - a
  16. * anonymous chunk
  17. - b
  18. - c
  19. * "my chunk"
  20. - d
  21. - e
  22. - f
  23. */

require AMD

  1. require(dependencies: String[], [callback: function(...)])

Behaves similar to require.ensure, but the callback is called with the exports of each dependency in the dependencies array. There is no option to provide a chunk name.

Style: AMD

Example:

  1. // in file.js
  2. var a = require("a");
  3. require(["b"], function(b) {
  4. var c = require("c");
  5. });
  6. /* This results in:
  7. * entry chunk
  8. - file.js
  9. - a
  10. * anonymous chunk
  11. - b
  12. - c
  13. */

require.include

  1. require.include(dependency: String)

Ensures that the dependency is available, but don’t execute it. This can be use for optimizing the position of a module in the chunks.

Style: webpack

Example:

  1. // in file.js
  2. require.include("a");
  3. require.ensure(["a", "b"], function(require) {
  4. // Do something
  5. });
  6. require.ensure(["a", "c"], function(require) {
  7. // Do something
  8. });
  9. /* This results in:
  10. * entry chunk
  11. - file.js
  12. - a
  13. * anonymous chunk
  14. - b
  15. * anonymous chunk
  16. - c
  17. Without require.include "a" would be in both anonymous chunks.
  18. The runtime behavior isn't changed.
  19. */

module.loaded

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

Style: node.js (for compatibility!)


module.hot

See [[Hot Module Replacement]].

Style: webpack


global

See node.js global

Style: node.js


process

See node.js process

Style: node.js


__dirname

Depending on the config option node.__dirname:

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

Style: node.js (for compatibility!)


__filename

Depending on the config option node.__filename:

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

Style: node.js (for compatibility!)


__resourceQuery

The resource query of the current module.

Style: webpack

Example:

  1. // Inside "file.js?test":
  2. __resourceQuery === "?test"

__webpack_public_path__

Equals the config options output.publicPath.

Style: webpack


__webpack_require__

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

Style: webpack


__webpack_chunk_load__

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.

Style: webpack


__webpack_modules__

Access to the internal object of all modules.

Style: webpack


require.resolveWeak

Like require.resolve, but doesn’t include the module into the bundle. It’s a weak dependency.

Style: webpack

Example:

  1. if(__webpack_modules__[require.resolveWeak("module")]) {
  2. // do something when module is available
  3. }
  4. if(require.cache[require.resolveWeak("module")]) {
  5. // do something when module was loaded before
  6. }

__webpack_hash__

Access to the hash of the compilation.

Only available with the HotModuleReplacementPlugin or the ExtendedAPIPlugin

Style: webpack


__non_webpack_require__

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

Style: webpack


DEBUG

Equals the config option debug

Style: webpack