What’s the practical difference between these options? If I have a project structure like this:
|-- webpack.config.js
|-- src
|-- node_modules
|-- web_modules
I was expecting to be able to set a root to /absolute-path/etc/src
and have webpack use that as the base to search for web_modules
and node_modules
. But instead I had to do either:
resolve.root = [ 'src/node_modules', 'src/web_moodules' ]
or
resolve.moduleDirectories = [ 'src/node_modules', 'src/web_moodules' ]
What’s the benefit of root
? Is it possible to set some kind of base path in the way I was looking for?
resolve.root
is usually the root of your project. All paths in the sourcemap for example will be relative to thisroot
.modulesDirectories
describes the names of directories for modules which are resolved via node’s resolving algorithm. SomodulesDirectories
shouldn’t contain whole paths, but only dir names.For example if you specifiy
["node_modules", "bower_components"]
and require a modulea
infoo/bar
webpack will look them up in this orderfoo/bar/node_modules/a
foo/bar/bower_components/a
foo/node_modules/a
foo/bower_components/a
node_modules/a
bower_components/a
You described the
context
option.resolve.root
is an absolute path to a directory containing modules.resolve.moduleDirectories
is an relative path to a tree of directories containing modules.Use
resolve.moduleDirectories
only for package managers with a depth dependency structure.In every other case use
resolve.root
.Sorry for the lack of details. This is what ended up working for me:
Now I am able to import files from the root of my src folder rather then needing “../../..”
Mhmmm this config could have an impact on your bundling performance. It causes webpack to prepend
./src
to every folder. So when you writerequire("a")
it will try to look up:resolve.root
should work as expected. I think there is another problem in your config.How do we do this in webpack 2.1.0-beta.25? It doesnt allow
resolveLoader.modulesDirectories
@IAMtheIAM use
resolve.modules
Using webpack 2.1.0-beta.22, I found that
webpack
appeared to be ignoring my settings inresolve.modulesDirectories
and, as far as I could understand, alsoresolve.root
.As @alex88 suggested, adding a
resolve.modules
configuration instead ofresolve.modulesDirectories
andresolve.root
allowed me to use paths relative to a root rather than relative to the module (exactly as this StackOverflow poster desired):