React - 我的 App.js 文件中出现意外的标识符错误。我不明白为什么

React - Unexpected Identifier error in my App.js file. I don't understand why

我正在学习有关如何使用 React、GraphQL 和 Apolo 构建应用程序的教程。我在启动应用程序时 运行 遇到意外标识符错误 ("node App.js")。这个错误似乎与 "import React" 有关,我不明白为什么。

此外,我同时使用 运行 前端和后端 "npm run dev" 并得到一个不同的错误。

我试过重新安装软件包。我分别开始 server.js 和 App.js。服务器工作正常但不是 App.js(如上所述)。

这是错误:

_Tutorial_Follow-Alongs/GraphQL_React-Apollo_Tut_Follow/client/src/App.js:1
(function (exports, require, module, __filename, __dirname) { import React, { Component } from 'react';
                                                                     ^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:73:7)
    at createScript (vm.js:245:10)
    at Object.runInThisContext (vm.js:297:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)
EFs-MacBook-Pro:src ef$ 

这是我的 App.js 代码:

import React, { Component } from 'react'; 
import ApolloClient from 'apollo-boost';
import { ApolloProvider } from 'react-apollo';
import Launches from "./components/Launches"
import "./App.css"; 
import logo from "./assets/images/logo.png";

// With Apollo wrap the component in a provider and pass in a client
const client = new ApolloClient({
  uri: "http://localhose:5000/graphql" //path to the one endpoint created using graphql in server.js
});

class App extends Component {
  render() {
    return (

      <ApolloProvider client={client}>
      <div className="container">
        <img 
          src={logo}
          alt="SpaceX"
          style={{ width: 300, display: "block", margin: "auto" }}
        />
      </div>
      <Launches />
      </ApolloProvider>
    );
  }
}

export default App;

这是并发使用时的错误,"npm run dev"

Failed to compile.
[1] 
[1] ./node_modules/react-dev-utils/formatWebpackMessages.js
[1] Module not found: Can't resolve '/Users/ef/Documents/UCF CODING BOOTCAMP/UCF_Local/_PRACTICE/_Tutorial_Follow-Alongs/GraphQL_React-Apollo_Tut_Follow/client/node_modules/babel-preset-react-app/node_modules/@babel/runtime/helpers/slicedToArray' in '/Users/ef/Documents/UCF CODING BOOTCAMP/UCF_Local/_PRACTICE/_Tutorial_Follow-Alongs/GraphQL_React-Apollo_Tut_Follow/client/node_modules/react-dev-utils'

在这两种情况下,应用都不会 运行。我完全卡住了。我将不胜感激任何帮助。谢谢

欢迎来到 Whosebug。

出现此问题是因为您正在尝试执行适用于 node 浏览器的前端代码。

如您在本例中所见,这不起作用:

代码应由您的服务器提供,并在浏览器中执行

对于第二个错误,您似乎在 client 目录中使用 CRA,该目录有自己的 package.json。因此,您应该能够从根目录 npm install --prefix client 将依赖项安装到 client 文件夹中。