我正试图用React构建我的第一个项目,使用Creative Tim的材料ui工具包,但在尝试导入组件时出错。我的文件结构如下:
project
|
+--src
|
+--components
| |
| +--CustomButtons
| |
| +--Button.js
+--Views
|
+--AdminView
|
+--Sections
| |
| +--Section1.js
| +--Section2.js
+--AdminView.js
从Section1.js中导入按钮,如下所示:
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"
。
我不确定为什么会发生这种情况,但我通过在项目上运行npm update解决了这个问题。
可能值得注意的是,这是在我安装了很多npm依赖项之后发生的。
尝试使用以下命令安装eslint加载器。这对我有效
npm install eslint-loader --save-dev