javascript Webpack 错误 - 无法解析文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34159119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 17:20:07  来源:igfitidea点击:

Webpack Error - Cannot Resolve File or Directory

javascriptnode.jswebpackwebpack-dev-servernpm-liveserver

提问by Kayote

I am getting this error when I npm start my webpack-dev-server:

当我 npm 启动 webpack-dev-server 时出现此错误:

ERROR in multi main
Module not found: Error: Cannot resolve 'file' or 'directory' /var/www/html/151208-DressingAphrodite/app in /var/www/html/151208-DressingAphrodite
 @ multi main

Here is my webpack.config.js:

这是我的 webpack.config.js:

var path = require('path');
var webpack = require('webpack');
var HtmlWebpackPlugin = require ('html-webpack-plugin');

const PATHS = {
  app: path.join (__dirname, 'app'),
  build : path.join (__dirname, 'build')
};

module.exports = {
  entry : [
    'babel-polyfill',
    'webpack-dev-server/client?http://localhost:8080',
    PATHS.app
  ],
  output : {
      publicPath : '/',
      filename : 'dressingaphrodite.js',
      hash : true
  },
  debug : true,
  devServer : {
    contentBase : './app'
  },
  devtool : 'source-map',
  module : {
    loaders : [
      {
        test : /\.jsx?$/,
        include : PATHS.app,
        loader : 'babel-loader',
        query : {
          presets : ["es2015"]
        }
      },
      {
          test: /\.css$/,
          include: PATHS.app,
          loader: 'style!css'
      }
    ]
  },
  plugins : [
    new HtmlWebpackPlugin ({
      title : 'Dressing Aphrodite',
      filename : 'da.html'
    })
  ]
};

回答by Fareed Alnamrouti

ok if any one face this problem, you can solve it in 2 methods:

好的,如果有人遇到这个问题,您可以通过两种方法解决它:

Method 1:

方法一:

1- add a file called package.json inside your entry folder (in your case put it inside the folder "{project_dir}/app/package.json")

1- 在您的入口文件夹中添加一个名为 package.json 的文件(在您的情况下将其放在文件夹中"{project_dir}/app/package.json"

2- inside this file write any json object for example:

2- 在此文件中写入任何 json 对象,例如:

{
  "name": "webpack why you do diss :("
}

Method 2:

方法二:

Change your entry files to be at least 2 level away from your project home directory, for example: "{project_dir}/src/app"

将您的入口文件更改为距离您的项目主目录至少 2 级,例如: "{project_dir}/src/app"

Explanation:for each entry file webpack will try to find a file called package.json to use it for webpack configuration incase this entry file is a module, when you put your entry file only 1 level away from your project home dir webpack will use your project packge.json as configuration file for your entry file and it will fail because of miss configuration.

说明:对于每个入口文件,webpack 将尝试找到一个名为 package.json 的文件,以将其用于 webpack 配置,以防此入口文件是一个模块,当您将入口文件放在距项目主目录仅 1 级的位置时,webpack 将使用您的项目 packge.json 作为您的入口文件的配置文件,它会因为错过配置而失败。

回答by Matthias M

You should check the browser console. It provides more detailed informations.

您应该检查浏览器控制台。它提供了更详细的信息。

In my case:

就我而言:

Uncaught Error: Cannot find module "./src/index"
at webpackMissingModule (webpack:///multi_main?:4)
at eval (webpack:///multi_main?:4)
at Object.<anonymous> (bundle.js:586)
at __webpack_require__ (bundle.js:556)
at bundle.js:579
at bundle.js:582

So here is the error a missing (or misspelled) java script file.

所以这是一个丢失(或拼写错误)的java脚本文件的错误。

回答by Cin

The same problem I met. I found an answer in github Error : ERROR in Entry module not found: Error: Cannot resolve 'file' or 'directory'#981. But sadly, in webpack 1.15.12, the --allow-incompatible-updatehas been remove. Also, I found when set the entry type is array, such as entry: [entry.js], webpack will run, but throw another error:(

我遇到了同样的问题。我在 github 中找到了一个答案Error : ERROR in Entry module not found: Error: Cannot resolve 'file' or 'directory'#981。但遗憾的是,在 webpack 1.15.12 中,--allow-incompatible-update已删除。另外,我发现当设置入口类型为数组时,例如entry: [entry.js],webpack 会运行,但又抛出另一个错误:(

I wish it can help you, hopefully.

我希望它可以帮助你,希望。