Vite启动后提示Network: use `--host` to expose

当使用 Vite 构建项目后,发现只有localhost + 端口 服务,没有 IP + 端口服务。
运行npm run dev,终端提示Vite启动后提示Network: use '--host' to expose

Vite

package.json中脚本配置

"scripts": {
    
    
  "dev": "vite",
  "build": "vite build",
  "serve": "vite preview"
}

问题复现

当运行 npm run dev | serve 命令时,会显示一下内容。

> [email protected]
> vite | vite preview

vite v2.5.1 build preview server running at:

> Local: http://localhost:3000 | 5000/
> Network: use `--host` to expose

解决

  • 配置vite.config.js
export default defineConfig(({
     
      mode })=>({
    
    
  // ..
  server: {
    
    
    host: '0.0.0.0',
  },
  // ..
}))
  • 配置package.json
"scripts": {
    
    
  "dev": "vite --host",
  "build": "vite build",
  "serve": "vite preview --host"
}

执行npm run dev命令

vite v2.5.1 dev server running at:

> Local:    http://localhost:3000/
> Network:  http://xxx.xx.xxx.xx:3000/

欢迎访问:个人博客地址

猜你喜欢

转载自blog.csdn.net/tiven_/article/details/120769922