简体   繁体   English

未找到模块错误:您尝试导入项目 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

I am currently making a music player app on React, and am new to React js.我目前正在 React 上制作音乐播放器应用程序,并且是 React js 的新手。 Not sure why i'm getting this error, having been following the exact steps on the tutorial I'm watching.不知道为什么我会收到这个错误,一直在按照我正在观看的教程中的确切步骤进行操作。

The full Error Code is:完整的错误代码是:

Module not found: Error: You attempted to import ../components/Player 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/.

For better clarity, This is the code for the Player component:为了更清楚起见,这是 Player 组件的代码:

import React from 'react';
import PlayerDetails from './PlayerDetails';

function Player(props) {
    return (  
        <div className="c-player">
            <audio></audio>
            <h4>Now playing</h4>
            <PlayerDetails song={props.song}/>
            {/*CONTROLS */}
            <p><strong> Next Up:</strong> {props.nextSong.title} {props.nextSong.artist}</p>

        </div>
    );
}

export default Player;

And this is the Code on App.js:这是 App.js 上的代码:

 import { useState } from "react";
 import Player from "../components/Player";

function App() {
const [songs, setSongs]= useState([
{
  title: "1",
  artist: "1",
  img_src: "./images/song-1.jpg/",
  src: "./music/song-1.mp3"
},
{
  title: "2",
  artist: "2",
  img_src: "./images/song-2.jpg/",
  src: "./music/song-2.mp3"
},
{
  title: "3",
  artist: "3",
  img_src: "./images/song-3.jpg/",
  src: "./music/song-3.mp3"
},
{
  title: "4",
  artist: "4",
  img_src: "./images/song-4.jpg/",
  src: "./music/song-4.mp3"
}

]);
const [currentSongIndex, setCurrentSongIndex]= useState(0);
const [nextSongIndex, setnextSongIndex]= useState (currentSongIndex + 1);


  return (
    <div className="App">
     <Player 
     songs= {songs[currentSongIndex]}
     nextSong={songs[nextSongIndex]}
     />
    </div>
  );
}

export default App;

Here is the folder structure:这是文件夹结构:

在此处输入图像描述 Appreciate your time!感谢您的时间!

I think path for your Player component is incorrect我认为您的 Player 组件的路径不正确

import Player from "../components/Player";

Try this:尝试这个:

import Player from "./components/Player";

Your components folder should be under the src folder, then set the path carefully.您的 components 文件夹应该在 src 文件夹下,然后仔细设置路径。

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

相关问题 未找到模块:您尝试导入项目 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 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 尝试导入错误:“formatDate”未从“src/utils”导出 - Attempted import error: 'formatDate' is not exported from 'src/utils'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM