简体   繁体   English

未找到模块:您尝试导入项目 src/ 目录之外的模块。 不支持 src/ 之外的相对导入

[英]Module not found: You attempted to import which falls outside of the project src/ directory. Relative imports outside of src/ are not supported

I have just started React JS, but I have a problem and despite my research I can not get results, I have tried many ways, but I have not been able to solve this problem.Can you help?我刚刚开始 React JS,但是我有一个问题,尽管我的研究我无法得到结果,我尝试了很多方法,但我一直无法解决这个问题。你能帮忙吗?

this is the error I got at the terminal这是我在终端遇到的错误

./src/App.css (./node_modules/react-scripts/node_modules/css-loader/dist/cjs.js??ref--5-oneOf-4-1!./node_modules/postcss-loader/src??postcss!./src/App.css)Module not found: You attempted to import ../public/images/img-8.jpg which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
  • Node Version: v12.18.3节点版本:v12.18.3
  • Npm Version: 6.14.8 Npm 版本:6.14.8

My File Structure我的文件结构

- node_modules
 - public
     * images
          * img-2.jpg
     * videos

 - src
    * App.js
    * App.css
    * index.js
    * setupTest.js

the picture i am trying to import in my css file我试图在我的 css 文件中导入的图片

background-image: url('/public/images/img-2.jpg');

App.js应用程序.js

import React from "react";
import "./App.css";
import Navbar from "./components/Navbar";
import { BrowserRouter as Router,Switch, Route } from "react-router-dom";

function App() {

  return (
   <Router>
     <Navbar></Navbar>
     <Switch>
       <Route path="/" exact />
     </Switch>
   </Router>
  );
}

export default App;

package.json package.json

 {
      "name": "react-website",
      "version": "0.1.0",
      "private": true,
      "dependencies": {
        "@testing-library/jest-dom": "^5.11.6",
        "@testing-library/react": "^11.2.2",
        "@testing-library/user-event": "^12.2.2",
        "css-loader": "^5.0.1",
        "react": "^17.0.1",
        "react-dom": "^17.0.1",
        "react-router-dom": "^5.2.0",
        "react-scripts": "4.0.1",
        "style-loader": "^2.0.0"
      },
      "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
      },
      "eslintConfig": {
        "extends": [
          "react-app",
          "react-app/jest"
        ]
      },
      "browserslist": {
        "production": [
          ">0.2%",
          "not dead",
          "not op_mini all"
        ],
        "development": [
          "last 1 chrome version",
          "last 1 firefox version",
          "last 1 safari version"
        ]
      }
    }

You should use /images/img-2.jpg/ to refer to the folder.您应该使用/images/img-2.jpg/来引用该文件夹。 The reason is that you are using create-react-app to create this React app.原因是您正在使用create-react-app来创建这个 React 应用程序。 When being bundled the app will be located in the public folder.捆绑后,应用程序将位于public文件夹中。 Read this other question where it mentions the same issue.阅读这个另一个问题,它提到了同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未找到模块错误:您尝试导入项目 src/ 目录之外的内容。 不支持 src/ 之外的相对导入 - Module not found Error: You attempted to import which falls outside of the project src/ directory. Relative imports outside of src/ are not supported React JS导入错误-找不到模块:您试图导入不在项目src /目录下的../actions - React JS import error - Module not found: You attempted to import ../actions which falls outside of the project src/ directory 未找到模块:错误:您尝试导入位于项目 src/ 目录之外的 babel-preset - Module not found: Error: You attempted to import babel-preset which falls outside of the project src/ directory 未找到模块:错误:您尝试导入项目 src/ 目录之外的 /firebase/app - Module not found: Error: You attempted to import /firebase/app which falls outside of the project src/ directory ReactJS 在 src/ 目录外导入组件 - ReactJS import component outside src/ directory src 目录之外的 create-react-app 导入限制 - The create-react-app imports restriction outside of src directory 根目录外的 webpack /src - webpack /src outside root directory npm:从 src 文件夹外导入 - npm: import from outside the src folder React-从外部src目录导入文件 - React - importing files from outside src directory 如何在 React App 中从外部 /src 导入函数 - How can I import functions from outside /src in React App
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM