2017-06-18 110 views
0

的WebPack 2模块未找到:错误:当我想用我的创造bundle.js无法解析模块

$ ./node_modules/.bin/webpack --config webpack.config.js 

我收到以下错误

ERROR in ./dev/js/source/scripts/components/TextInput.js Module not found: Error: Cannot resolve module 'source/scripts/components/IconButton' in /Users/sebastien/Documents/Javascript/Tests/MyApp/dev/js/source/scripts/components @ ./dev/js/source/scripts/components/TextInput.js 4:18-65

ERROR in ./dev/js/source/scripts/components/TextInput.js Module not found: Error: Cannot resolve module 'source/scripts/SDK/classNames' in /Users/sebastien/Documents/Javascript/Tests/MyApp/dev/js/source/scripts/components @ ./dev/js/source/scripts/components/TextInput.js 15:17-57

项目树如下

/Users/sebastien/Documents/Javascript/Tests/MyApp 
- webpack.config.js 
- dev 
- js 
    - index.js 
    - source 
    - scripts 
    - components 
    - TextInput.js 
    - IconButton.js 
    - SDK 
    - classNames.js 

在TextInput.js有下面的语句

import IconButton from "source/scripts/components/IconButton"; 

和我webpack.config.js包含以下

var path = require('path'); 
var webpack = require('webpack'); 

module.exports = { 
    devServer: { 
     inline: true, 
     contentBase: './src', 
     port: 8080 
    }, 
    devtool: 'cheap-module-eval-source-map', 
    entry: './dev/js/index.js', 
    module: { 
     loaders: [ 
      { 
       test: /\.js$/, 
       loaders: ['babel'], 
       exclude: /node_modules/ 
      }, 
      { 
       test: /\.scss/, 
       loader: 'style-loader!css-loader!sass-loader' 
      } 
     ] 
    }, 
    output: { 
     path: 'src', 
     filename: 'js/bundle.min.js' 
    }, 
    plugins: [ 
     new webpack.optimize.OccurrenceOrderPlugin() 
    ] 
}; 

如何配置的WebPack添加路径./dev/js搜索模块?我曾尝试过:

- modules: [path.resolve(__dirname, "./dev/js"), "node_modules"] 

没有任何成功。你能帮我吗?

问候,

回答

0

尝试

import IconButton from "./source/scripts/components/IconButton"; 

,看看有没有什么帮助。

import IconButton from "../source/scripts/components/IconButton"; 

,但第一个应该很可能工作。试一试。

+0

谢谢,它有帮助,但我不想更改位于目录脚本下的.js文件的源代码。 我不是这些* .js文件的所有者(他们由另一个团队提供)。我只想用webpack配置解决这个问题(如果可能的话)。 – sebastien

+0

呃,对不起,帮不了你。我认为我提供的答案可能有效,因为在React中工作时我遇到了完全相同的问题。我从来没有真正使用过webpack,但是也许如果你在谷歌搜索错误,比如“webpack模块找不到修复路径”或类似的东西,或者只是等待某人给出适当的答案。 –

+1

不要对不起。当有人回答并尝试提供帮助时,我总是很感激。 – sebastien

相关问题