Vue.js 在浏览器中出现意外令牌,但 npm run build 没有错误 [英] Vue.js unexpected token in browser but npm run build with no errors

查看:15
本文介绍了Vue.js 在浏览器中出现意外令牌,但 npm run build 没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理其他人启动的 vue.js 项目,并将其放在生产服务器上.npm run build 编译没有错误.但是当我尝试在浏览器中运行该项目时,我得到一个

未捕获的语法错误:意外标记<

这是index.html

我得到了所有 3 个 .js 资产文件.

请求的服务器控制台输出.看起来文件正在返回:

GET/static/css/app.d79e04c40fa9145fe427147ef17b4576.css 200 1.580 毫秒 - 820获取/static/js/manifest.2ae2e69a05c33dfc65f8.js 200 3.095 毫秒 - 820获取/static/js/app.19024fb1db555dd8417b.js 200 3.157 毫秒 - 820获取/static/js/vendor.bfe581b46b720086662f.js 200 3.429 毫秒 - 820

知道如何解决这个问题吗?

解决方案

在不同的选项卡上打开每个脚本的 URL(或检查开发人员工具的网络选项卡),这些 URL 可能返回 404(或某些其他错误代码)和一个 HTML 错误页面.

因此您的代码尝试将这些 HTML 错误页面解析为 JavaScript 代码,从而产生该错误.

它得到 Uncaught SyntaxError: Unexpected token < 因为它试图将 HTML 内容(例如 ...)解析为 JavaScript 代码.>

运行下面的代码片段并在控制台查看错误.

<script type=text/javascript src=https://stackoverflow.com></script>检查控制台:Uncaught SyntaxError: Unexpected token <"

I'm working with a vue.js project someone else started and I'm putting it on a production server. The npm run build compiles with no errors. But when i try to run the project in the browser i get a

Uncaught SyntaxError: unexpected token <

Here's the index.html

<!DOCTYPE html><html><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><title>tst-apps-suite</title><base href=/ ><link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,700,800" rel=stylesheet><link href="https://fonts.googleapis.com/css?family=Dosis:200,400,600,700,800" rel=stylesheet><link href=https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css rel=stylesheet><link href=/static/css/app.d79e04c40fa9145fe427147ef17b4576.css rel=stylesheet></head><body><div id=app></div><script type=text/javascript src=/static/js/manifest.2ae2e69a05c33dfc65f8.js></script><script type=text/javascript src=/static/js/vendor.bfe581b46b720086662f.js></script><script type=text/javascript src=/static/js/app.19024fb1db555dd8417b.js></script></body></html>

I get that for all 3 .js asset files.

Server console output on the request. Looks like the files are getting returned:

GET /static/css/app.d79e04c40fa9145fe427147ef17b4576.css 200 1.580 ms - 820
GET /static/js/manifest.2ae2e69a05c33dfc65f8.js 200 3.095 ms - 820
GET /static/js/app.19024fb1db555dd8417b.js 200 3.157 ms - 820
GET /static/js/vendor.bfe581b46b720086662f.js 200 3.429 ms - 820

Any idea how to fix this?

解决方案

Open the URLs of the scripts each on a different tab (or check the network tab of the developer tools), those URLs are probably returning a 404 (or some other error code) and a HTML error page.

So your code tries to parse those HTML error pages as JavaScript code, thus yielding that error.

It gets Uncaught SyntaxError: Unexpected token < because it tries to parse the HTML content (e.g. <html> ...) as JavaScript code.

Run the snippet below and see the error at the console.

<script type=text/javascript src=https://stackoverflow.com></script>
Check the console: "Uncaught SyntaxError: Unexpected token <"

这篇关于Vue.js 在浏览器中出现意外令牌,但 npm run build 没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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