简体   繁体   English

index.js:1 Uncaught SyntaxError: 意外的标记 <

[英]index.js:1 Uncaught SyntaxError: Unexpected token <

I'm trying to run our production version of our web app which uses the minified (production) version of React on our IIS server.我正在尝试运行我们的 web 应用程序的生产版本,它在我们的 IIS 服务器上使用 React 的缩小(生产)版本。 We can currently run the developer version, but when attempting to run the build version, we keep getting the following error我们目前可以运行开发者版本,但是在尝试运行构建版本时,我们不断收到以下错误

`index.js:1 Uncaught SyntaxError: Unexpected token <` 

I thought it may of been to the minified code in the build folder, so I made a basic index.js with just a alert inside, and it's still failing.我认为它可能是构建文件夹中的缩小代码,所以我制作了一个基本的 index.js,里面只有一个警报,但它仍然失败。 I checked the file path, and it seems to be write, and the index.html file seems to be in order as well.我检查了文件路径,似乎是写入的,index.html文件似乎也是有序的。 We are using the boiler from reactboilerplate.com and running the npm run build command我们正在使用来自 reactboilerplate.com 的锅炉并运行npm run build命令

Dev Console printout:开发控制台打印输出: 在此处输入图像描述

index.html:索引.html:

    <!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width,initial-scale=1">
 <link rel="manifest" href="manifest.json">
 <meta name="mobile-web-app-capable" content="yes">
 <title>Timeclock</title>
</head>

<body>
 <noscript>If you're seeing this message, that means
  <strong>JavaScript has been disabled on your browser</strong>, please <strong>enable JS</strong> to make this app work.</noscript>
 <div id="app"></div>
 <script type="text/javascript" src="index.js"></script>
</body>

</html>

Inspect your page and verify that when you fetch the index.js file that you're actually getting it. 检查您的页面并验证当您获取实际获得它的index.js文件时。 My guess is that when you fetch index.js you'll actually get the index.html, and therefore when your browser attempts to execute a copy of your index.html file as a script, it immediately errors as < is not a valid initial character for a piece of javascript. 我的猜测是,当你获取index.js时,你实际上会获得index.html,因此当你的浏览器尝试将index.html文件的副本作为脚本执行时,它会立即出错,因为<不是有效的初始值一段javascript的字符。 This is a common misconfiguration people bump into when configuring html push state, so your webserver may not be properly configured to serve index.js properly; 这是人们在配置html推送状态时遇到的常见错误配置,因此您的网络服务器可能无法正确配置为正确地提供index.js; make sure you have the following set in your iisconfig.xml file: 确保在iisconfig.xml文件中设置了以下内容:

<configuration>
    <system.webServer>
        <rewrite>
          <rules>
            <rule name="Main Rule" stopProcessing="true">
                    <match url=".*" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                    </conditions>
                    <action type="Rewrite" url="/" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

After you set up an index.js and linked with the HTML file, then you should delete the in index.js and run your code directly.设置好 index.js 并与 HTML 文件链接后,您应该删除 index.js 中的内容并直接运行您的代码。 the error will disappear, It is that simple.错误会消失,就这么简单。 but tricky at the start但一开始很棘手

While linking to index.js file you just need to add the type as "text/JSX" And the error disappears.在链接到 index.js 文件时,您只需将类型添加为“text/JSX”,错误就会消失。

<script src="../src/index.js" type="text/JSX"></script>

I was getting the same error which was being caused because i was using express.static('public') but that was not the main file there.我遇到了同样的错误,这是因为我使用的是 express.static('public') 但那不是那里的主文件。 My 'Public' folder was inside another folder called 'Develop" which was messing the paths up. Pulled everything outside Develop and the problem got solved. Hope this helps我的“公共”文件夹位于另一个名为“开发”的文件夹中,该文件夹弄乱了路径。将所有内容拉到开发之外,问题得到解决。希望这有帮助

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

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