How does webpack resolve imports from node_modules?

BetaRide picture BetaRide · Jun 17, 2016 · Viewed 18.6k times · Source

When I need a library from my node_modules folder I do something like this:

import angular from 'angular';
import $ from 'jquery;

How does webpack know what file(s) it really has to import? Guess there's some kind of strategy what files it's going to check?

Answer

Grgur picture Grgur · Jun 17, 2016

Webpack loops over resolvers to find the file you requested. It goes over resolve templates to figure out the exact path.

If you try to import a module that doesn't exist you'll see the error trace outlining all the paths it tried to use to find the file but failed.

Resolvers are a powerful configuration tool that could help you develop better code. I really appreciate that resolvers allow me to drop relative paths and use more developer-friendly ES6 imports.

I hope that helps.