gpt4 book ai didi

javascript - 未找到模块 : You attempted to import which falls outside of the project src/directory. 不支持 src/之外的相对导入

转载 作者:行者123 更新时间:2023-12-05 00:35:31 25 4
gpt4 key购买 nike

我刚开始 React JS,但是我有一个问题,尽管我的研究我无法得到结果,我尝试了很多方法,但我一直无法解决这个问题。你能帮忙吗?
这是我在终端遇到的错误

./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.
  • 节点版本:v12.18.3
  • Npm 版本:6.14.8

  • 我的文件结构
    - node_modules
    - public
    * images
    * img-2.jpg
    * videos

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

    我试图在我的 css 文件中导入的图片
    background-image: url('/public/images/img-2.jpg');
    应用.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;
    包.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"
    ]
    }
    }

    最佳答案

    您应该使用 /images/img-2.jpg/引用文件夹。原因是您使用的是 create-react-app创建这个 React 应用程序。捆绑后,应用程序将位于 public文件夹。阅读本文other question它提到了同样的问题。

    关于javascript - 未找到模块 : You attempted to import which falls outside of the project src/directory. 不支持 src/之外的相对导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65083234/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com