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

如何将Vite默认运行地址从127.0.0.1改为localhost?

如何让Vite默认运行在localhost而非127.0.0.1

你给出的两种配置方式都是有效的,这里整理下配置细节和注意事项:

配置方案说明

方案1:通过package.json的scripts配置

直接在dev命令中指定--host参数,写法更简洁:

"scripts": {
  "dev": "vite --host localhost",
  "build": "vite build",
  "preview": "vite preview"
}

注:--host=localhost和--host localhost两种写法都生效,后者更符合命令行参数规范

方案2:通过vite.config.js全局配置

在配置文件的server字段中设置host值,无需每次在命令行加参数:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'

export default defineConfig({
  plugins: [react()],
  server: {
    host: 'localhost',
    port: 3000
  }
})

配置优先级说明

如果同时在命令行和配置文件中设置了host,命令行参数的优先级更高,会覆盖配置文件中的对应设置。

常见细节提示

有时候即使配置正确,控制台可能仍显示127.0.0.1——这是因为系统默认hosts文件中localhost就是解析到127.0.0.1的,二者网络层面完全等价。如果希望控制台明确显示localhost,只需确保配置无冲突即可,比如不要同时设置host: true(该设置会让Vite监听所有地址,控制台可能显示0.0.0.0或本机IP)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 16:22:03