ReactJS 无法编译语法错误意外的令牌;常见问题,但我无法解决

ReactJS Failed to Compile Syntax Error Unexpected token; common problem but I can't solve it

MacOSX Node:v15.10.0 NPM:7.5.3

我意识到这是一个常见问题,我已经尝试解决这个问题超过一天了,在尝试了多种不同的解决方案之后,我仍然无法解决这个问题。我正在学习 ReactJS 并遵循从 create-react-app 开始的指南。无论指南如何,我都会得到以下信息:

Failed to compile.

./src/requests.js
SyntaxError: /Users/.../requests.js: Unexpected token (4:22)

我试图通过 package.json、webpack.config.js 和 .babelrc 解决 Webpack 和 Babel 的差异,但似乎没有任何效果。我尝试删除 node-modules 和 package-lock.json 并重新安装应用程序中的所有内容。我试过没有 webpack.config 文件和六种预设、排除等变体。我也尝试完全重新安装节点和 npm,但仍然没有骰子。

App.js

import './App.css';
import Row from "./Row.js";
import requests from "./requests.js";

function App() {
  return (
    <div className="App">
      <div className="header">
          Starting NetFlavor
      </div>
      <Row title="Weekly Trending" fetchURL={requests.fetchWeekTrending} />
      <Row title="Newest" fetchURL={requests.fetchMovie}/>
    </div>
  );
}

export default App;

requests.js

const API_KEY = '';

const requests = {
    fetchWeekTrending = '/trending/movie/week?api_key=${API_KEY}',
    fetchMovie = 'https://api.themoviedb.org/3/movie/228203?api_key==${API_KEY}'
};

export default requests;

Row.js

import React from 'react'

function Row({ title }) {

    const [movies, setMovies] = useState([]);

    return (
        <div>
            <h2>{title}</h2>
        </div>
    )
}

export default Row

webpack.config.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'chamber.js'
    },
    
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader: 'babel-loader',
                options:{
                presets: [
                    '@babel/preset-env',
                    '@babel/preset-react'
                ],
            }
            }}
        ]},
    plugins: [
        new HtmlWebpackPlugin({
            template:'./src/index.html'
        })
    ],
}

package.json

{
  "name": "chamber",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/preset-env": "^7.13.9",
    "@babel/preset-react": "^7.12.13",
    "@material-ui/core": "^4.11.3",
    "@testing-library/jest-dom": "^5.11.9",
    "@testing-library/react": "^11.2.5",
    "@testing-library/user-event": "^12.8.0",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.3",
    "web-vitals": "^1.1.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"
    ]
  },
  "devDependencies": {
    "@babel/core": "^7.12.3",
    "babel-loader": "^8.1.0"
  },
  "babel": {
    "presets": [
        "es2015",
        "react"
    ]
}
}

.babelrc

{
    "presets": ["@babel/preset-env", "@babel/preset-react"],
    "plugins": ["transform-decorators-legacy", "transform-class-properties"]
}

非常感谢任何帮助。在这一点上,我什至无法学习,因为我无法克服这个错误。

如错误所述,您的 requests.js 中存在语法错误。 注意 const requests:使用 : 分配属性并使用反引号。

const API_KEY = '';

const requests = {
    fetchWeekTrending: `/trending/movie/week?api_key=${API_KEY}`,
    fetchMovie: `https://api.themoviedb.org/3/movie/228203?api_key==${API_KEY}`
};

export default requests;