Ever tried running a Docker container or a local development server, only to find you can’t access it from other devices on your network? You’re not alone—many developers hit this roadblock and wonder, “How do I actually expose my service using –host?”
Knowing how to properly use the –host option can save you hours of frustration, especially when testing across different devices or collaborating with a team.
In this article, we’ll break down what –host does, how to use it effectively, and share practical tips to ensure seamless connectivity for your applications.
Related Video
How to Use --host
to Expose a Local Network in Vite
When developing modern web applications with Vite, you may encounter situations where you want your local development server to be accessible not just from your own machine, but also from other devices on the same network. For example, you might want to test your app on a mobile device, share the work-in-progress with a teammate, or run automated UI tests from another machine. This is where the --host
option comes into play.
If you see a message like “Network: use --host
to expose” after starting your Vite server, it means your server is currently restricted to localhost (your machine only). Let’s break down what this means, why it happens, and how you can solve it.
What Does the “use --host
to expose” Message Mean?
By default, Vite’s development server binds only to the localhost
address (127.0.0.1). This means:
- Only your computer can access the development server (e.g.,
http://localhost:5173
). - Other devices—even those connected to the same Wi-Fi network—can’t see your app.
When you see this message, Vite is telling you that your server is hidden from your network. If you want other devices to access your development server, you need to explicitly instruct Vite to listen to network connections. That’s where --host
comes in.
How Do You Expose Vite to the Network Using --host
?
The process is straightforward. You can use the --host
flag when starting your Vite server to allow network access.
Step-by-Step Guide
- Find your Computer’s Local IP Address
- On Windows: Run
ipconfig
in the Command Prompt; look forIPv4 Address
. - On macOS/Linux: Use
ifconfig
orip a
in Terminal; look forinet
under your network device. -
You will get something like
192.168.1.100
. -
Start Vite with the
--host
Option - Open your project in the terminal.
-
Use one of these commands (the default port is 5173):
vite --host
npm run dev -- --host
yarn dev --host
-
Access from Another Device
- On another device (phone, tablet, or computer) connected to the same network, open the browser.
- Type
http://:5173
- For example:
http://192.168.1.100:5173
- You should now see your Vite app!
Things to Note
- If you want to specify which IP address Vite should listen on, you can pass it:
npm run dev -- --host=192.168.1.100
--host 0.0.0.0
will listen on all available network interfaces. Most commonly, just--host
is enough.
Why Is the Server Not Exposed by Default?
There are good reasons behind this design:
- Security: Exposing your development server to the entire network (or worse, the internet) can be risky. Localhost-only access ensures that only someone with access to your machine can see your in-progress work or any sensitive environment variables/configuration.
- Best Practice: Development servers should not be public unless you intentionally make them so. Vite asks you to opt in, rather than expose you by accident.
Benefits of Exposing Vite to the Network
Enabling network exposure for your Vite dev server unlocks several helpful workflows:
- Responsive testing: Quickly check how your app looks and behaves on multiple devices, such as phones or tablets, without painful device emulators or upload steps.
- Team collaboration: Let colleagues see your live updates instantly during pair programming or presentations.
- BrowserStack/Sauce Labs testing: Run automated tests that require the server to be reachable over the network.
Potential Challenges and Aspects to Consider
While exposing your dev server is useful, there are a few things to keep in mind:
1. Firewall Restrictions
- Many operating systems will prompt you to allow incoming network connections. If you miss or deny this prompt, your server will not be visible to other devices.
- You may need to manually allow port 5173 (or your chosen port) through the firewall.
2. Network Segmentation
- Devices must be on the same Wi-Fi or local network as your computer.
- Some corporate networks isolate devices or block local traffic for security.
3. Changing IP Addresses
- Your local IP can change when you disconnect/reconnect to Wi-Fi or after a restart.
- Bookmark the address, but remember to double-check if you lose access.
4. External Access
- The steps above only expose your project to your local network.
- If you need remote access over the internet, consider solutions like [ngrok] or similar tunneling tools (with caution and proper security).
5. Sensitive Information
- Never use this setup for production deployments.
- Be cautious about revealing sensitive development data if others share your network.
Practical Tips and Best Practices
To make the most out of network exposure with the --host
flag, follow these suggestions:
Use the Correct Command for Your Package Manager
npm run dev -- --host
yarn dev --host
pnpm dev --host
Insert after your usual dev command. The double dash (--
) separates npm script arguments from your own.
Specify a Custom Port (If Needed)
If another app is using port 5173, add --port=3000
(or any open port):
npm run dev -- --host --port=3000
Update Your Hosts File for a Friendly Address (Advanced)
For ease during development, you can edit your system’s hosts file and point a custom name (like myapp.local
) to your computer’s IP. This lets you use http://myapp.local:5173
on network devices.
Use HTTPS for Device Testing (If Needed)
- For certain API authentication or service workers, you may need HTTPS.
- Vite can serve over HTTPS with certificate options (see Vite’s server documentation).
Secure Your Network
- Only expose to networks you trust.
- On public or shared Wi-Fi, avoid exposing your dev server unless necessary.
Troubleshooting Tips
- Can’t access from another device?
- Is your computer’s firewall blocking the port?
- Are both devices on the same network (Wi-Fi vs. Ethernet)?
-
Did you use the correct IP and port?
-
Still seeing “Network: use
--host
to expose” after using--host
? - Double-check how you’re passing the argument (watch for misplaced dashes or typos).
-
Try explicitly specifying your IP (
--host=192.168.1.100
). -
Mobile device can’t connect?
- Ensure mobile data is off (to force Wi-Fi use).
- Confirm your mobile firewall/security settings.
Recap and Summary
When you see Vite’s “Network: use --host
to expose” message, it’s a friendly reminder that, by default, your dev server is only reachable from your machine for safety. If you want to access your development site from other devices on your network, just start your server with the --host
flag.
Here’s the quick recipe:
- Look up your computer’s local IP address.
- Use
--host
when starting the server. - Access via
http://:
from devices on your network.
It’s a simple yet powerful feature to supercharge testing, collaboration, and development flexibility.
Frequently Asked Questions (FAQs)
What does the “Network: use --host
to expose” message mean?
This means that Vite’s dev server is running in localhost-only mode, so only your computer can access it. To allow other devices on the network to connect, you need to start Vite with the --host
flag.
How do I start Vite so other network devices can access it?
In your project directory, run npm run dev -- --host
or vite --host
. Then visit http://:5173
on another device connected to the same network.
Is it safe to expose my development server on the network?
For most local development and trusted networks, yes. However, never expose your dev server to the public internet, and always be aware that anyone on your local network could connect while the server is running.
Why can’t my phone/tablet access the Vite server after using --host
?
Check that both devices are on the same Wi-Fi/local network. Make sure your firewall allows incoming connections on the server port, and verify you’ve entered the correct IP address and port.
Can I expose my Vite server to the internet for remote access?
Not directly with --host
. For remote access, use tunneling tools like ngrok or similar services, but do so carefully. Make sure your app does not expose sensitive data or resources before sharing access.
With this approach, you’ll have your Vite dev server available across your network in no time—making device testing and collaboration a breeze!