【问题标题】:Module Not Found React Js找不到模块 React Js
【发布时间】:2023-01-09 01:18:54
【问题描述】:
Failed to compile.

Module not found: Error: You attempted to import ../components/App which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/.
ERROR in ./src/index.js 6:0-36
Module not found: Error: You attempted to import ../components/App which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/.

webpack compiled with 1 error

当我尝试朗读我的 React 应用程序时出现上述错误。我正在学习并且是 React js 的初学者。我正在学习 reactjs 中的映射,当我运行我的应用程序时出现此错误任何人都请解决这个问题。

文件系统分配如下。

【问题讨论】:

    标签: javascript reactjs npm mapping jsx


    【解决方案1】:

    使用import App from "./components/App"而不是import App from "../components/App";

    【讨论】:

      【解决方案2】:

      ../ 表示您想从文件所在的文件夹中退出。这意味着 ../ 将您带到 mapping 下,因此您可以这样导入

      import App from "../src/components/App"
      

      但最好不要出去使用 ./ 留在 src 下并像这样导入:

      import App from "./components/App"
      

      两者都是正确的。

      【讨论】:

        猜你喜欢
        • 2021-10-07
        • 2019-12-19
        • 1970-01-01
        • 2021-12-25
        • 2015-09-20
        • 2017-06-17
        • 2019-09-06
        • 2018-09-06
        相关资源
        最近更新 更多