无法解析webpack中的加载路径

3mpgtkmj  于 2022-12-13  发布在  Webpack
关注(0)|答案(3)|浏览(198)

我尝试将gulp+browserify迁移到webpack,但在解析路径时遇到问题。
下面是我的项目树:

/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

下面是我的配置文件:

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: []
    }
};

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

var $ = require('jquery'),
    Device = require('device'),
    Ga = require('ga');

require('jquery.raty');
require('jquery.autoNumeric');
require('jquery.unveil');

function initialize() {}

module.exports = {
    initialize: initialize,
};

当我启动webpack时,出现webpack --display-error-details错误:

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

我不明白为什么Web Pack只在node_modules文件夹中搜索,而不在“www/static/js/”文件夹中搜索。
对于信息,我将有多个条目,但现在我只想webpack工程。
谢谢你的帮助。

pdtvr36n

pdtvr36n1#

根据webpack文档modulesDirectories应该是一个目录名数组,而不是路径。
For example, if the value is ["mydir"], webpack will look in “./mydir”, “../mydir”, “../../mydir”, etc.

n53p2ov0

n53p2ov02#

我知道了。
我必须使用context选项:

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

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

替换为:

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

23c0lvtd3#

我认为context不是处理此问题的正确方法。
我只需要将入口文件的路径设置为绝对路径,而忽略resolve.modulesDirecories
但那只是我的想法。

相关问题