Node.js 未找到模块:错误:无法解析';文件';或';目录';
我正在尝试使用导入我的Node.js 未找到模块:错误:无法解析';文件';或';目录';,node.js,reactjs,express,webpack,Node.js,Reactjs,Express,Webpack,我正在尝试使用导入我的SignupLoginForm.jsx文件 import SignupLoginForm from '../container/SignupLoginForm'; 到我的主页.jsx文件 SignupLoginForm.jsx位于client/container/SignupLoginForm.jsx中Homepage.jsx位于client/components/Homepage/Homepage.jsx中 我得到这个错误: ERROR in ./client/comp
SignupLoginForm.jsx
文件
import SignupLoginForm from '../container/SignupLoginForm';
到我的主页.jsx
文件
SignupLoginForm.jsx
位于client/container/SignupLoginForm.jsx
中Homepage.jsx
位于client/components/Homepage/Homepage.jsx
中
我得到这个错误:
ERROR in ./client/components/homepage/HomePage.jsx
Module not found: Error: Cannot resolve 'file' or 'directory' ../container/SignupLoginForm in /Users/Proj_V/client/components/homepage
@ ./client/components/homepage/HomePage.jsx 23:23-62
使用以下命令:
import SignupLoginForm from '../../container/SignupLoginForm';
由于您的容器
文件夹比Homepage.jsx
文件高出两级,因此您需要使用。/
两次
/
,。/
-
/
表示当前目录,即留在当前文件夹中并在此文件夹中搜索文件
。/
表示当前目录/文件的父级,表示转到上一级并搜索该文件
例如,假设您在这些路径上有两个文件file1.js
和file2.js
,您想从file1导入file2,将您的文件夹结构想象为树数据结构:
案例1.
file1
->a/b/c/file1.js
file2
->a/b/c/file2.js
由于存在于同一文件夹中的文件,u可以像这样直接导入:/file2.js
案例2.
file1
->a/b/c/file1.js
file2
->a/b/c/d/file2.js
在本例中,文件2位于d
中,它与file1
处于同一级别,您可以这样导入它:/d/file2.js
案例3.
file1
->a/b/c/file1.js
file2
->a/b/file2.js
在本例中,file2
出现在file1
的父级,因此您需要使用。/
,这样做:。/file2.js
案例4.
file1
->a/b/c/file1.js
file2
->a/file2.js
在这种情况下,file2
出现在file1
的父级的父级,因此您需要通过使用。/
两次来进行升级:。/../file2.js
简单规则:
/
意味着在同一个文件夹中搜索,。/
直接向上搜索一步。我们也可以使用类似的组合:。/./
除了上述Mayank的答案之外,我建议您使用“babel根导入”插件,它将帮助您导入并要求使用基于根的路径
例如:您可以从主页访问SignupLoginForm,如下所示:
import SignupLoginForm from '~/container/SignupLoginForm';
有关更多详细信息,请参阅非常感谢您。你知道关于目录的任何资源吗?你的解决方案是正确的,但我不明白你说我的容器文件夹是两级更新的,请检查我的答案,让我知道你是否需要任何帮助:)