Webpack 2: cannot resolve module

I have a project like this:

root/ webpack-config.js app/ app.js js/ dep.js core/ module.js 

Here is the webpack configuration file:

 module.exports = { entry: './app/app.js', output: { path: __dirname, filename: "[name]-bundle.js" }, module: { loaders: [ { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ } ] }, resolve: { modulesDirectories: ['core'] } ... 

in app.js, I have:

 import local_dep from './js/dep'; import myModule from 'module'; 

This works, as expected, using webpack 1.x, but the myModule module is not resolved using webpack 2, I get "Module not found: it is not possible to resolve the" module "in ... \ application".

It seems that the modulesDirectories entry is being ignored and the base URL matches the input folder.

What can I do to get modules resolved correctly with webpack 2?

+7
javascript webpack webpack-2
source share
3 answers

Edit: as others have noted, something like this works for Webpack 2:

 { resolve: { extensions: ['.js', '.jsx'], modules: ['node_modules', path.resolve(__dirname, 'core')] }, } 

For Webpack 1, I have a setting that has this entry:

 config.resolve = { // Allows us to include js and jsx files without specifying the extension extensions: ['', '.jsx', '.js'], root: [path.resolve('./core')] }; 
+3
source share

from: http://moduscreate.com/webpack-2-tree-shaking-configuration/

In Webpack 2, the converters from the root , modulesDirectories and fallback settings merge into one property - modules . Here's how we can resolve the location of files and modules in Webpack 2:

  resolve: { modules: [ path.resolve('./client'), 'node_modules' ] }, 

You can specify the number of directories in modules , but remember that node_modules or npm package dependencies will not load.


So, in your case, what happened before:

 resolve: { modulesDirectories: ['core'] } 

should now be

 resolve: { modules: ['core'] // also 'node_modules' } 
+12
source share

Thanks to Christopher Davis, I fixed the problem by adding:

 resolveLoader: { root: path.join(__dirname, 'node_modules') }, resolve: { root: ['./core'] } 

I had to add the resolveLoader line, otherwise I was getting an error about the babel bootloader, which could not be loaded.

-4
source share

All Articles