These options change how modules are resolved. webpack provides reasonable defaults, but it is possible to change the resolving in detail. Have a look at Module Resolution for more explanation of how the resolver works.



Configure how modules are resolved. For example, when calling import "lodash" in ES2015, the resolve options can change where webpack goes to look for "lodash" (see modules).



Create aliases to import or require certain modules more easily. For example, to alias a bunch of commonly used src/ folders:

alias: {
  Utilities: path.resolve(__dirname, 'src/utilities/'),
  Templates: path.resolve(__dirname, 'src/templates/')

Now, instead of using relative paths when importing like so:

import Utility from '../../utilities/utility';

you can use the alias:

import Utility from 'Utilities/utility';

A trailing $ can also be added to the given object's keys to signify an exact match:

alias: {
  xyz$: path.resolve(__dirname, 'path/to/file.js')

which would yield these results:

import Test1 from 'xyz'; // Exact match, so path/to/file.js is resolved and imported
import Test2 from 'xyz/file.js'; // Not an exact match, normal resolution takes place

The following table explains other cases:

alias: import "xyz" import "xyz/file.js"
{ xyz: "/abs/path/to/file.js" }
{ xyz$: "/abs/path/to/file.js" }
{ xyz: "./dir/file.js" }
{ xyz$: "./dir/file.js" }
{ xyz: "/some/dir" }
{ xyz$: "/some/dir" }
{ xyz: "./dir" }
{ xyz: "modu" }
{ xyz$: "modu" }
{ xyz: "modu/some/file.js" }
{ xyz: "modu/dir" }
{ xyz: "xyz/dir" }
{ xyz$: "xyz/dir" }

index.js may resolve to another file if defined in the package.json.

/abc/node_modules may resolve in /node_modules too.



Specify a field, such as browser, to be parsed according to this specification. Default:

aliasFields: ["browser"]


boolean (since webpack 3.1.0)

If unsafe cache is enabled, includes request.context in the cache key. This option is taken into account by the enhanced-resolve module. Since webpack 3.1.0 context in resolve caching is ignored when resolve or resolveLoader plugins are provided. This addresses a performance regression.



The JSON files to use for descriptions. Default:

descriptionFiles: ["package.json"]



If true, it will not allow extension-less files. So by default require('./foo') works if ./foo has a .js extension, but with this enabled only require('./foo.js') will work. Default:

enforceExtension: false



Whether to require to use an extension for modules (e.g. loaders). Default:

enforceModuleExtension: false



Automatically resolve certain extensions. This defaults to:

extensions: [".js", ".json"]

which is what enables users to leave off the extension when importing:

import File from '../path/to/file'
Using this will override the default array, meaning that webpack will no longer try to resolve modules using the default extensions. For modules that are imported with their extension, e.g. import SomeFile from "./somefile.ext", to be properly resolved, a string containing "*" must be included in the array.



When importing from an npm package, e.g. import * as D3 from "d3", this option will determine which fields in it's package.json are checked. The default values will vary based upon the target specified in your webpack configuration.

When the target property is set to webworker, web, or left unspecified:

mainFields: ["browser", "module", "main"]

For any other target (including node):

mainFields: ["module", "main"]

For example, the package.json of D3 contains these fields:

  main: 'build/d3.Node.js',
  browser: 'build/d3.js',
  module: 'index',

This means that when we import * as D3 from "d3" this will really resolve to the file in the browser property. The browser property takes precedence here because it's the first item in mainFields. Meanwhile, a Node.js application bundled by webpack will resolve by default to the file in the module field.



The filename to be used while resolving directories. Default:

mainFiles: ["index"]



Tell webpack what directories should be searched when resolving modules.

Absolute and relative paths can both be used, but be aware that they will behave a bit differently.

A relative path will be scanned similarly to how Node scans for node_modules, by looking through the current directory as well as it's ancestors (i.e. ./node_modules, ../node_modules, and on).

With an absolute path, it will only search in the given directory.

resolve.modules defaults to:

modules: ["node_modules"]

If you want to add a directory to search in that takes precedences over node_modules/:

modules: [path.resolve(__dirname, "src"), "node_modules"]


regex array boolean

Enable aggressive, but unsafe, caching of modules. Passing true will cache everything. Default:

unsafeCache: true

A regular expression, or an array of regular expressions, can be used to test file paths and only cache certain modules. For example, to only cache utilities:

unsafeCache: /src\/utilities/
Changes to cached paths may cause failure in rare cases.


A list of additional resolve plugins which should be applied. It allows plugins such as DirectoryNamedWebpackPlugin.

plugins: [new DirectoryNamedWebpackPlugin()]


Whether to resolve symlinks to their symlinked location. Default:

symlinks: true



A function which decides whether a request should be cached or not. An object is passed to the function with path and request properties. Default:

cachePredicate: function() { return true }



This set of options is identical to the resolve property set above, but is used only to resolve webpack's loader packages. Default:

    modules: ["node_modules"],
    extensions: [".js", ".json"],
    mainFields: ["loader", "main"]
Note that you can use alias here and other features familiar from resolve. For example { txt: 'raw-loader' } would shim txt!templates/demo.txt to use raw-loader.



The extensions which are tried when resolving a module (e.g. loaders). By default this is an empty array.

If you want to use loaders without the -loader suffix, you can use this:

moduleExtensions: ['-loader']

© JS Foundation and other contributors
Licensed under the Creative Commons Attribution License 4.0.