Running svelte dev on server

Viewed 15935

I am running svelte like this on my server:

$ npm run dev


  Your application is ready~! 

  - Local:      http://localhost:5000

────────────────── LOGS ──────────────────

Which is great. However, when I try to access through my public ip, the bundle is not found. I.E. When I type <publicIP>:5000 into the browser. It doesn't show up. The port is open and accessible. Is there any way to achieve this?

The request just fails. But shouldn't it work if it's running on localhost:5000? I have set up a node server and I can indeed access it on port 5000, but it doesn't serve the files properly like npm run dev does.

4 Answers

Declare the environment variable HOST=0.0.0.0

HOST=0.0.0.0 npm run dev

inspiration / possible source: https://github.com/lukeed/sirv/issues/29#issuecomment-497907602


You can also modify the dev script in the package.json and prepend HOST=0.0.0.0

  "scripts": {
    "build": "rollup -c",
    "dev": "HOST=0.0.0.0 rollup -c -w",
    "start": "sirv public"
  },

And now you can simply run npm run dev

Actually, the message:

  Your application is ready~!

  - Local:      http://localhost:5000
  - Network:    Add `--host` to expose

is telling you to put --host in sirv public instead of rollup -c -w which is a bit of a confusing message.

  "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public  --host"
  },

Just add the bold part to package.json

"start": "sirv public --host 0.0.0.0"

Many thx to folks with suggestions here. Update for SvelteKit. I just want to check the site in my local network using my mobile phone.

package.json extract...

"scripts": {
        "dev": "svelte-kit dev",
        "special": "svelte-kit dev --host 0.0.0.0",
        "build": "svelte-kit build",
        "preview": "svelte-kit preview",
        "lint": "eslint --ignore-path .gitignore ."
    },

And my startup console looks like:

C:\Program Files\nodejs\npm.cmd run-script special -- --open

> sveltekit_demo_app@0.0.1 special
> svelte-kit dev --host 0.0.0.0 "--open"

  SvelteKit v1.0.0-next.118

  network: http://192.168.1.14:3000
  network: http://192.168.56.1:3000
  network: http://169.254.131.201:3000
  local:   http://localhost:3000
Related