找不到模块:无法在React App中解析'/root/project/node_modules/eslint-loader/index.js

时间:2020-03-27 01:00:01

标签: reactjs material-ui eslint

我正在尝试使用Creative Tim的material-ui-kit使用React构建我的第一个项目,但是在尝试导入组件时出现错误。我的文件结构如下:

project
|
+--src
   |
   +--components
   |  |
   |  +--CustomButtons
   |     |
   |     +--Button.js
   +--Views
      |
      +--AdminView
         |
         +--Sections
         |  |
         |  +--Section1.js     
         |  +--Section2.js
         +--AdminView.js

从Section1.js内,我这样导入Button:

import Button from "components/CustomButtons/Button.js";

,这很好。但是,当我从section2.js执行相同操作时,出现以下错误:

Failed to compile.

./src/views/AdminView/Sections/MaterialTable.js
Module not found: Can't resolve '/root/project/node_modules/eslint-loader/index.js' in '/root/project'

我已经尽力想了一切,花了几个小时来查找,但是我不知道。这两个文件位于同一目录中,导入在一个目录中起作用,而在另一个目录中则无效。我已经使两个文件中的代码完全相同,但是无法正常工作。

任何帮助将不胜感激。

编辑

项目中的jsconfig.json文件具有以下内容:

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "*": ["src/*"]
    }
  }
}

如果我没记错的话,这就是"components/CustomButtons/Button.js"可以工作而不需要"../../components/CustomButtons/Button.js"的原因。

2 个答案:

答案 0 :(得分:1)

我不确定为什么会这样,但是我通过在项目上运行 npm update 解决了该问题。

值得注意的是,这是在我安装了许多npm依赖项之后发生的。

答案 1 :(得分:0)

尝试使用以下命令安装 eslint-loader。这对我有用

npm install eslint-loader --save-dev