Create-React-App 构建 - “Uncaught SyntaxError: Unexpected token <"; [英] Create-React-App build - "Uncaught SyntaxError: Unexpected token <"

查看:60
本文介绍了Create-React-App 构建 - “Uncaught SyntaxError: Unexpected token <";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这个问题已被问过多次,但对我没有任何帮助...

I realize this question has been asked multiple times but nothing has worked for me...

我正在尝试创建 create-react-app 项目的静态构建,但出现以下错误:

I'm trying to create a static build of a create-react-app project but I'm getting the following errors:

Uncaught SyntaxError: Unexpected token <     1.ca81c833.chunk.js:1
Uncaught SyntaxError: Unexpected token <     main.7ced8661.chunk.js:1

由于这些文件被缩小了,我不知道从哪里开始调试它们.

Due to these files being minified, I'm not sure where to begin in debugging them.

根据其他 SO 回复,以下是我尝试过的一些方法:

Per other SO responses, here are some things I've tried:

//Original index.html file, which gets included in the built file:
<script type="text/babel" src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script>

//package.json
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",

//.babelrc file
{
  "presets": ["react", "es2015", "stage-1"]
}

不确定这是否相关,但我在我的快速服务器上有这个,我认为这是标准的:

Not sure if this is relevant, but I have this on my express server, which I believe is standard:

if (process.env.NODE_ENV === "production") {

    app.use(express.static('client/built'));
    app.get("*", (req, res) => {
        res.sendFile(require('path')
            .resolve(__dirname, 'client', 'build', 'index.html'));
    })
}

假设它实际上是一个 JSX 问题,整个事情就非常令人困惑——create-react-app 不应该自动处理 JSX 吗?

Assuming it's actually a JSX issue, the whole thing is very confusing - shouldn't create-react-app be handling JSX automatically?

更新:我刚刚发布了这个问题,但已经有了相关的更新.我可以通过运行 pm2 serve build 通过 pm2 静态提供页面,所以我认为问题可能出在我的服务器配置上.

UPDATE: I just posted this question but already have a relevant update. I'm able to serve the page statically through pm2 by running pm2 serve build so I think the issue may be with my server configuration.

推荐答案

我最终在这里找到了答案:https://github.com/facebook/create-react-app/issues/1812

I ended up finding an answer here: https://github.com/facebook/create-react-app/issues/1812

我从上面删减了完整的解决方案,但我改变了:

I trimmed down the full solution from above, but I changed:

app.use(express.static('client/build'));
app.get("*", (req, res) => {
    res.sendFile(require('path')
        .resolve(__dirname, 'client', 'build', 'index.html'));
})

到:

const root = require('path').join(__dirname, 'client', 'build')
app.use(express.static(root));
app.get("*", (req, res) => {
    res.sendFile('index.html', { root });
})

第一个块不起作用对我来说肯定有点奇怪.我认为它与我的 React 项目中的相对链接有关,因为我确实收到了一个 index.html 文件传递​​到浏览器,尽管收到了错误.也许一个完全静态的文件可以处理第一个块,但我很想知道这是否准确.

It's definitely a bit strange to me that the first block didn't work. I assume it has something to do with the relative links in my React project since I do get an index.html file delivered to browser, despite getting the error. Maybe a completely static file would work with the first block, but I'd be interested to know if that's accurate.

这篇关于Create-React-App 构建 - “Uncaught SyntaxError: Unexpected token &lt;";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆