简体   繁体   English

React SyntaxError:意外的标记“<”

[英]React SyntaxError: Unexpected token '<'

29.12.2022 UPDT: 29.12.2022 更新:

When I visit all routes of initial nesting(such as / or /company or /users and so) my bundle.js comes from /static/js/bundle.js , it contains js and all is fine.当我访问初始嵌套的所有路由(例如//company/users等)时,我的bundle.js来自/static/js/bundle.js ,它包含 js 并且一切正常。 But when I visit pages with more route nesting by entering it's url or refreshing page being on them it gives me error, cuz bundle.js now comes not from /static/js/bundle.js but for example from /company/static/js/bundle.js and it contains html. So error occurs但是,当我通过输入 url 或刷新页面来访问具有更多路由嵌套的页面时,它会给我错误,因为bundle.js现在不是来自/static/js/bundle.js ,而是来自/company/static/js/bundle.js它包含 html。因此发生错误

[Error] SyntaxError: Unexpected token '<'
    (anonymous function) (bundle.js:1)
[Error] SyntaxError: Unexpected token '<'
    (anonymous function) (vendors~main.chunk.js:1)
[Error] SyntaxError: Unexpected token '<'
    (anonymous function) (main.chunk.js:1)

So it seems like i have to change webpack configuration to give /static/js/bundle.js on every request no matter what nesting, but i can't get where i should change that所以看起来我必须更改 webpack 配置以在每个请求上提供/static/js/bundle.js ,无论嵌套是什么,但我无法找到应该更改的位置

This is my config-overrides.js file:这是我的config-overrides.js文件:

const path = require("path");
// const TerserPlugin = require("terser-webpack-plugin");
const { DefinePlugin } = require("webpack");
const rewireSvgSpriteLoader = require("react-app-rewired-svg-sprite-loader");
// const webpack = require("react-app-rewired");
// const { devServer } = require('react-app-rewired/config-overrides')

module.exports = {
   webpack: function override(config, env) {
      config.resolve = {
         ...config.resolve,
         alias: {
            ...config.alias,
            "@components": path.resolve(__dirname, "src/components"),
            "@pages": path.resolve(__dirname, "src/pages"),
            "@hooks": path.resolve(__dirname, "src/hooks"),
            "@mock": path.resolve(__dirname, "src/mock"),
            "@scss": path.resolve(__dirname, "src/scss"),
            "@services": path.resolve(__dirname, "src/services"),
            "@store": path.resolve(__dirname, "src/store"),
            "@interfaces": path.resolve(__dirname, "src/types"),
            "@utils": path.resolve(__dirname, "src/utils"),
            "@ui": path.resolve(__dirname, "src/ui"),
            "@modals": path.resolve(__dirname, "src/modals"),
            "@assets": path.resolve(__dirname, "src/assets"),
            "@http": path.resolve(__dirname, "src/http"),
         },
      };
      config.module.rules = [
         ...config.module.rules,
         {
            test: /\.module\.scss$/,
            include: path.resolve(__dirname, "./src/components"),
            use: [
               {
                  loader: "sass-resources-loader",
                  options: {
                     resources: require("./src/scss/scss-resourses"),
                  },
               },
            ],
         },
      ];
      config = rewireSvgSpriteLoader(config, env);

      config.resolve.modules = [path.resolve("src")].concat(config.resolve.modules);
      config.output = {
         ...config.output,
         publicPath: "",
      };
      config.plugins = [
         ...config.plugins,
         new DefinePlugin({
            "process.env.URL": JSON.stringify(process.env.URl),
            "process.env.API_URL": JSON.stringify(process.env.API_URL),
            "process.env.EDITOR_URL": JSON.stringify(process.env.EDITOR_URL),
         }),
      ];
      return config;
   },

   devServer: function(configFunction) {
      return function(proxy, allowedHost) {
         const config = configFunction(proxy, allowedHost)

         config.historyApiFallback.disableDotRule = true
         return config
      }
   }
}

I think the first screenshot is not your JS file, but an error message from the server.我认为第一个屏幕截图不是您的 JS 文件,而是来自服务器的错误消息。 It's included as a JS file (in place of your JS script into your main HTML) and this cause the error what you see in the console.它作为一个 JS 文件包含(代替您的 JS 脚本到您的主要 HTML 中),这会导致您在控制台中看到的错误。

It could be a response to a bad HTTP call with a bad URL in your script tag.它可能是对错误的 HTTP 调用的响应,脚本标记中有错误的 URL。 I see a strange comment after "manifest" with a possible error message "only files inside the public(...)", but since I can't see the full response, I can't tell what the problem is.我在“清单”之后看到一条奇怪的注释,其中可能包含错误消息“只有公共文件内部 (...)”,但由于我看不到完整的响应,所以我无法判断问题出在哪里。

If you can, please copy the entire server response to help me figure out what is the root cause.如果可以,请复制整个服务器响应以帮助我找出根本原因。 If I had to guess it's a react router misconfiguration where your URL points out of your public folder.如果我不得不猜测这是一个反应路由器配置错误,您的 URL 指向您的公共文件夹。

An SPA server should return the index.html for all paths that aren't real files, so it's understandable that you'd see a HTML page for an otherwise not-found path. SPA 服务器应为所有非真实文件的路径返回 index.html,因此您会看到 HTML 页面以获取其他未找到的路径是可以理解的。 This is so the SPA application can do routing itself.这样 SPA 应用程序就可以自己进行路由。

Right now it looks like you're probably on https://vgokna.ru/company/ , and that page attempts to load scripts from .现在看起来您可能在https://vgokna.ru/company/上,并且该页面尝试从. instead of / , so https://vgokna.ru/company/static/js/... gets requested (and since it would otherwise 404, the server returns the index.html page).而不是/ ,所以https://vgokna.ru/company/static/js/...被请求(并且因为它会否则为 404,服务器返回index.html页面)。 You'll need to make sure the HTML page attempts to load scripts (and other assets) from the server root instead of the page's directory.您需要确保 HTML 页面尝试从服务器根目录而不是页面目录加载脚本(和其他资产)。 IOW, if you'd have IOW,如果你有

<script src="static/js/bundle.js">

you'd need你需要

<script src="/static/js/bundle.js">

instead.反而。

The issue was in this lines in config-overrides.js问题出在config-overrides.js的这一行

config.output = {
     ...config.output,
     publicPath: "",
};

I had to change public path because path of static files of js( /static/bundle.js ) was conflicting with public path of react(/static/images/**.png etc), so i couldn't get js properly我不得不更改公共路径,因为 static 个 js 文件的路径( /static/bundle.js )与 react 的公共路径(/static/images/**.png 等)冲突,所以我无法正确获取 js

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM