没有扩展名的模块解析

时间:2016-05-02 20:55:45

标签: reactjs ecmascript-6 webpack

在index.jsx中给出这一行:

import ExampleTable from 'table';

我得到了:

  

找不到模块:错误:无法解析模块'表'在global-path-to / application / public / scripts中    @ ./application/public/scripts/index.jsx 19:13-29

但是table.jsx存在于该文件夹中,我试图在webpack.config.js中定义文件扩展名:

{
entry:SOURCE_DIRECTORY + '/index.jsx',

output:
{
    path:SCRIPT_DIRECTORY,
    filename:'bundle.js'
},

resolve:
{
    extensions:['', '.js', '.jsx']
},    

module:
{
    loaders:
    [
        {
            // Regular expression to find both .js and .jsx files.
            test:/\.jsx?$/,
            include:SCRIPT_DIRECTORY,
            loader: 'babel'
        }
    ]
}
};
$ npm ls --depth 0
├── babel-core@6.7.7
├── babel-loader@6.2.4
├── babel-preset-es2015@6.6.0
├── babel-preset-react@6.5.0
├── jquery@2.2.3
├── jsx-loader@0.13.2
├── material-ui@0.15.0-beta.2
├── react@15.0.1
├── react-dom@15.0.1
├── react-tap-event-plugin@1.0.0
└── webpack@1.13.0

1 个答案:

答案 0 :(得分:2)

我认为你只需要使用相对路径./table,否则它会尝试在node_modules中查找table包。

如果您有更复杂的路径,您还可以在webpack配置中设置路径解析器:http://webpack.github.io/docs/configuration.html#resolve-root