You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

localhost:8080无法加载及JS脚本不运行问题求助

解决localhost:8080无法加载JS脚本的问题

问题背景

  • 原本localhost:8080可正常运行,多次运行JS脚本后无法加载
  • 已确认Chrome开发者工具网络设置为无节流(非“离线”)
  • 临时方案:修改launch.json用file路径打开页面可正常显示,但启动localhost时JS脚本无法运行(JS仅含console.log('Hello World'),与index.html同目录)

当前使用的launch.json配置

"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"file": "//my personal path//project_file//index.html"
}

补充更新

通过Anaconda切换内核后,每次添加运行.js脚本的配置,localhost就无法加载,寻求解决办法

解决方法

方法1:修正launch.json的localhost配置

把file字段替换为url,指向本地服务地址,同时确保本地服务(如http-server、live-server)在8080端口运行:

"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:8080/index.html",
"webRoot": "${workspaceFolder}/project_file"
}
  • 核心逻辑:用url访问本地服务而非直接打开本地文件,避免文件协议限制JS运行
  • 前置操作:在项目目录下启动本地服务,比如运行npx http-server -p 8080(需安装Node.js)

方法2:排查Anaconda内核与本地服务的冲突

  • 关闭Anaconda相关内核服务,重启本地8080服务,验证是否恢复正常
  • 若必须使用Anaconda内核,更换本地服务端口(如8081),同步修改launch.json的url为http://localhost:8081/index.html,启动服务时指定对应端口

方法3:清理浏览器缓存与进程

  • 打开Chrome按Ctrl+Shift+Delete,清理缓存、Cookie和本地存储
  • 关闭所有Chrome进程(含后台进程),重启浏览器后再访问localhost

方法4:检查JS脚本引入与VS Code配置

  • 删除自动添加的单独运行.js脚本的配置,确保launch.json仅保留Chrome访问localhost的配置
  • 确认index.html中正确引入JS脚本,示例代码:
<script src="./your-script.js"></script>

内容的提问来源于stack exchange,提问作者curiouscoder0619

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 12:05:02