DEV Community

Josué Makuta for KADEA ACADEMY

Posted on

How to expose Vite local instance to the network, Eg. Mobile Device

Vite logo

What is Vite?

Vite (the French word for “fast”) is a new build tool for frontend web development.

Vite promotes a faster development experience by setting up a development environment for frameworks like Vue, TezJS, and React and even for Vanilla JavaScript apps with a dev server.

Learn more about Vite here …

Vite local instance exposition

When you configured your web application development using Vite, your package.json file scripts look like this :



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


Enter fullscreen mode Exit fullscreen mode

Now when you run the command :



npm run dev


Enter fullscreen mode Exit fullscreen mode

Here’s what the output might look like :

Image without local instance

The local instance is now running on http://localhost:5173/,

Note that it shows also the Network: Use — host

What’s that? Vite gives way to expose the local instance to the local network in cases where you would like to test or view your web application on another device, let’s say, on your mobile device.

There are 3 ways to do it:

  1. By adding --host option to the scripts section in package.json as shown below :


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


Enter fullscreen mode Exit fullscreen mode
  1. By adding the following configuration in the vite.config.js file like so:


server: {
    host: true
}


Enter fullscreen mode Exit fullscreen mode
  1. Lastly, you can use the --host option with the npm run dev command like so.


npm run dev --host


Enter fullscreen mode Exit fullscreen mode

Now the output should look like this :
Image with vite local instance exposed

Now the Vite local instance is available on the network address provided.

Hope you found this article helpful, 👏👏👏.

Thanks for reading!

Top comments (4)

Collapse
 
j_pirela_rivas profile image
Jorge Pirela Rivas

Simply yarn start to expose the host without changing any configuration

Collapse
 
ghanshyamkdobariya profile image
Ghanshyam

Thank you. This is what I was exactly looking for.

Collapse
 
stepanenko profile image
Dmytro

Thank you, this works.

Collapse
 
akajr profile image
Juma Kibula

This is so helpful