Skip to content

resolve.root vs moduleDirectories #472

Closed

Description

@necolas

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?

Activity

  1. jhnns commented on Sep 15, 2014

    @jhnns
    Contributor

    resolve.root is usually the root of your project. All paths in the sourcemap for example will be relative to this root. modulesDirectories describes the names of directories for modules which are resolved via node's resolving algorithm. So modulesDirectories shouldn't contain whole paths, but only dir names.

    For example if you specifiy ["node_modules", "bower_components"] and require a module a in foo/bar webpack will look them up in this order

    • foo/bar/node_modules/a
    • foo/bar/bower_components/a
    • foo/node_modules/a
    • foo/bower_components/a
    • node_modules/a
    • bower_components/a
  2. sokra commented on Sep 16, 2014

    @sokra
    Member

    resolve.root is usually the root of your project. All paths in the sourcemap for example will be relative to this root.

    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.

  3. necolas commented on Sep 16, 2014

    @necolas
    Author

    Ah thanks! Not sure how I missed that…long days :(

  4. jhnns commented on Sep 17, 2014

    @jhnns
    Contributor

    @sokra Thanks for clearification.

    So bower_components should rather be part of resolve.root instead of modulesDirectories?

  5. sokra commented on Sep 17, 2014

    @sokra
    Member

    ehm... yes

  6. patricktargun commented on Dec 23, 2015

    @patricktargun

    This wasn't working for me:

    resolve: {
        root: path.resolve('src'),
        modulesDirectories: ['node_modules'],
        extensions: ['', '.js', '.jsx']
    },

    How should the resolve look to work with both node_modules and everything inside of the public folder?

  7. jhnns commented on Dec 26, 2015

    @jhnns
    Contributor

    @targun sorry, that's not enough information to solve your issue. You need to provide us with examples, your folder structure, your specific error.

  8. patricktargun commented on Dec 28, 2015

    @patricktargun

    Sorry for the lack of details. This is what ended up working for me:

    resolve: {
        modulesDirectories: ['node_modules', './src'],
        extensions: ['', '.js', '.jsx']
    },

    Now I am able to import files from the root of my src folder rather then needing "../../.."

  9. jhnns commented on Jan 1, 2016

    @jhnns
    Contributor

    Mhmmm this config could have an impact on your bundling performance. It causes webpack to prepend ./src to every folder. So when you write require("a") it will try to look up:

    /some/folder/structure/node_modules/a
    /some/folder/structure/src/a
    /some/folder/node_modules/a
    /some/folder/src/a
    /some/node_modules/a
    /some/src/a
    /node_modules/a
    /src/a
    

    resolve.root should work as expected. I think there is another problem in your config.

  10. patricktargun commented on Jan 2, 2016

    @patricktargun

    Thanks for your help!

  11. soda-x commented on Sep 22, 2016

    @soda-x

    @jhnns

    resolveLoader: {
      modulesDirectories: [ path.resolve(__dirname, '../node_modules'), 'node_modules' ],
      extensions: [".loader.js", ".js"]
    },
    

    So when I write css!postcss!less how to look up postcss-loader

    Thanks

  12. IAMtheIAM commented on Sep 26, 2016

    @IAMtheIAM

    How do we do this in webpack 2.1.0-beta.25? It doesnt allow resolveLoader.modulesDirectories

  13. alex88 commented on Oct 14, 2016

    @alex88

    @IAMtheIAM use resolve.modules

  14. dragosrusu commented on Dec 14, 2016

    @dragosrusu

    @alex88 : doesn't seem to work - can you provide a sample?
    Tried with: "resolve: { modules: "../devtools/node_modules" } and it did not work out.

  15. evenfrost commented on Dec 20, 2016

    @evenfrost

    @dragosrusu
    resolve.modules should be an array, so try resolve: { modules: ["../devtools/node_modules"] } instead.

  16. shirakaba commented on Jan 16, 2017

    @shirakaba

    Using webpack 2.1.0-beta.22, I found that webpack appeared to be ignoring my settings in resolve.modulesDirectories and, as far as I could understand, also resolve.root.

    As @alex88 suggested, adding a resolve.modules configuration instead of resolve.modulesDirectories and resolve.root allowed me to use paths relative to a root rather than relative to the module (exactly as this StackOverflow poster desired):

    resolve: {
       modules: ["."]
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions