VS Code中serverReadyAction为何打开127.0.0.1而非localhost?
解决WSL调试时VS Code打开127.0.0.1而非localhost的问题
针对你遇到的情况,可以通过以下两种方式解决:
方式一:修正日志匹配规则,确保准确提取端口
你的app.js输出的日志是Example app listening on port 3000!,但当前launch.json里的pattern规则无法准确匹配到端口号,导致VS Code自动检测端口后使用默认的127.0.0.1。
修改serverReadyAction的pattern为:
"pattern": "listening on port ([0-9]+)"
这个规则会精准匹配日志中port后的端口数字,配合uriFormat: "http://localhost:%s",就能生成正确的http://localhost:3000并打开。
方式二:禁用远程别名自动解析
在WSL这类远程环境中,VS Code默认会将localhost替换为127.0.0.1,你可以通过添加resolveRemoteAlias: false来禁用这个行为:
修改serverReadyAction配置:
"serverReadyAction": { "pattern": "listening on.* (https?://\\S+|[0-9]+)", "uriFormat": "http://localhost:%s", "action": "openExternally", "resolveRemoteAlias": false }
建议优先尝试方式一,因为更贴合你当前的日志输出格式,能确保端口提取的准确性。
内容的提问来源于stack exchange,提问作者Lin Du
相关产品推荐
相关产品推荐

