最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Webpack 2: cannot resolve module - Stack Overflow

programmeradmin0浏览0评论

I have a project like this:

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

Here is the webpack config 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 with webpack 1.x, but the myModule module isn't resolved with webpack 2, I'm getting "Module not found: can't resolve 'module' in ... \app".

It seems the modulesDirectories entry is ignored and the base URL corresponds to the entry's folder.

What can I do to make modules resolve correctly with webpack 2 ?

I have a project like this:

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

Here is the webpack config 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 with webpack 1.x, but the myModule module isn't resolved with webpack 2, I'm getting "Module not found: can't resolve 'module' in ... \app".

It seems the modulesDirectories entry is ignored and the base URL corresponds to the entry's folder.

What can I do to make modules resolve correctly with webpack 2 ?

Share Improve this question edited Jul 3, 2016 at 14:10 warpdesign asked Jul 1, 2016 at 15:26 warpdesignwarpdesign 7472 gold badges7 silver badges17 bronze badges 1
  • Christopher Davies has the correct answer, you should select it. – Leon Gaban Commented Jul 17, 2017 at 19:06
Add a comment  | 

3 Answers 3

Reset to default 12

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

In Webpack 2, resolvers from root, modulesDirectories, and fallback settings will merge into a single property – modules. Here is how we can resolve file and module locations in Webpack 2:

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

You can specify a number of directories in modules, but make sure not to forget node_modules or npm package dependencies will fail to load.


So in your case what was before:

resolve: {
  modulesDirectories: ['core']
}

now needs to be

resolve: {
  modules: ['core'] // also 'node_modules' 
}

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

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

For Webpack 1, I have a setup 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')]
};

Thanks to Christopher Davies I fixed the problem by adding:

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

I had to add resolveLoader line otherwise I was getting an error about babel-loader that could not be loaded.

发布评论

评论列表(0)

  1. 暂无评论