【问题标题】:ReactJS: Getting Failed to Compile Error While Starting the ServerReactJS:启动服务器时无法编译错误
【发布时间】:2020-02-19 05:25:59
【问题描述】:

我是 ReactJS 的初学者。在完成 React 和 Redux 基础课程之后。我开始了一个新的 Udemy 课程,题为“反应数据可视化 - 构建加密货币仪表板”。在尝试使用yarn start 命令启动服务器时,我在第 4 课中遇到了一个问题:

Failed to compile
./src/index.js
Cannot find file: 'index.js' does not match the corresponding name on disk: '.\src\App\app'.

这是我在app 目录下的index.js 文件上的代码:

import React, { Component } from 'react';
import './App.css';

class App extends Component {
  render(){
    return (
      <div>
        Hello World
      </div>
    );
  }

}

export default App;

这是我在src 目录下的index.js 文件上的代码:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';

ReactDOM.render(<App />, document.getElementById('root'));

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.

我该如何解决这个问题?

您可以在此处找到项目文件。

https://drive.google.com/open?id=1u-8mCYSWZtXPbIqELKsF33FOiXb3yrkH

编辑 1:我在 package.json 文件中添加代码,正如 @Wesgur 所建议的:

{
  "name": "cryptodash",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-scripts": "3.4.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

编辑 2:我正在添加图像以显示文件夹结构,正如 @Madhavan.V 所建议的那样:

【问题讨论】:

  • 你也可以添加你的package.json吗?
  • @Wesgur 我已将package.json 文件的代码添加到问题中。
  • @Shawn 检查导入import App from './App';
  • @Shawn 你可以尝试将src/index中的导入更改为import App from './app';
  • 我认为您是从 ./App 导入而不是 ./app

标签: reactjs


【解决方案1】:

@Shawan 将 src/index 中的 App import 语句从 import App from './App'; 更改为 import App from './app'; 只是一个错字。

【讨论】:

    【解决方案2】:

    从 './app' 导入应用程序; 这将导入您的 app 文件夹的 index.js。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-01
      • 2023-03-28
      • 2016-08-28
      • 1970-01-01
      • 2016-01-09
      • 2014-03-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多