Webpack 2:无法解析模块 [英] Webpack 2: cannot resolve module

查看:44
本文介绍了Webpack 2:无法解析模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的项目:

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

这是 webpack 配置文件:

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']
    }
    ...

在 app.js 中,我有:

in app.js, I have:

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

这在 webpack 1.x 中按预期工作,但是 webpack 2 无法解析 myModule 模块,我收到找不到模块:无法解析 ...\app 中的‘模块’".

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".

似乎 modulesDirectories 条目被忽略,基本 URL 对应于条目的文件夹.

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

如何使用 webpack 2 使模块正确解析?

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

推荐答案

正如其他人所指出的,对于 Webpack 2,如下所示:

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

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

对于 Webpack 1,我有一个包含此条目的设置:

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')]
};

这篇关于Webpack 2:无法解析模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆