无法解析webpack中的加载路径

时间:2016-01-31 13:45:08

标签: webpack

我尝试将gulp + browserify迁移到webpack,我在解决路径方面遇到了问题。

这是我的项目树:

max_answer = answers[0]
for answer in answers:
     if answer.question_id > max_answer.question_id:
         max_answer = answer
last_question = max_answer.question_id

这是我的配置文件:

/path/to/project
├── webpack.config.js
└── www/static
        ├── build
        │   ├── js
        │   │   ├── main.js <-------│ Output wanted
        │   │   └── main.js.map <---│
        ├── js
        │   ├── app.js
        │   ├── contact.js
        │   ├── device.js
        │   └── ...
        └── sass
            ├── main.scss
            └── partials
                ├── _helpers.scss
                ├── _mixins.scss
                └── _variables.scss

用于测试目的的app.js内容:

var path = require('path');

module.exports = {
    entry: { 'app': 'app.js' },

    resolve: {
        modulesDirectories: ['www/static/js/', './']
    },

    output: {
        filename: './www/static/build/js/main.js'
    },
    module: {
        loaders: []
    }
};

当我启动webpack时,我收到var $ = require('jquery'), Device = require('device'), Ga = require('ga'); require('jquery.raty'); require('jquery.autoNumeric'); require('jquery.unveil'); function initialize() {} module.exports = { initialize: initialize, }; 的错误:

ERROR in Entry module not found: Error: Can't resolve 'app' in '/path/to/project'
resolve 'app' in '/path/to/project'
  Parsed request is a module
  No description file found
  resolve as module
    /Users/jordid/MAWork/MA_vagrant/apps/node_modules doesn't exist or is not a directory
    /Users/jordid/MAWork/MA_vagrant/node_modules doesn't exist or is not a directory
    /Users/jordid/MAWork/node_modules doesn't exist or is not a directory
    /Users/jordid/node_modules doesn't exist or is not a directory
    /Users/node_modules doesn't exist or is not a directory
    /node_modules doesn't exist or is not a directory
    looking for modules in /path/to/project/node_modules
      No description file found
      No description file found
      as directory
        /path/to/project/node_modules/app doesn't exist
      no extension
        /path/to/project/node_modules/app doesn't exist
      .js
        /path/to/project/node_modules/app.js doesn't exist
      .json
        /path/to/project/node_modules/app.json doesn't exist

我不明白为什么网络包只在node_modules文件夹中搜索,而不是在www / static / js /&#39;文件夹中。

有关信息,我将有多个条目,但是现在我只想要webpack工作。

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

根据webpack docs modulesDirectories应该是一个目录名称数组。不是路径。

For example, if the value is ["mydir"], webpack will look in “./mydir”, “../mydir”, “../../mydir”, etc.

答案 1 :(得分:0)

知道了。

我必须使用context选项:

entry: { 'app': 'app.js' },

resolve: {
    modulesDirectories: ['www/static/js/', './']
},

替换为:

entry: { 'app' : './app.js' },
context: path.resolve(__dirname, './www/static/js/'),

答案 2 :(得分:0)

我认为context是处理此问题的正确方法。

我只是将我的输入文件的路径设为绝对,忘记resolve.modulesDirecories

但那只是我。