Hypermystic
Hypermystic

Reputation: 1020

How to change the default port 5000 in Svelte?

I am not getting how to change the default 5000 port in Svelte to some other port if we install the sample template through degit.

Upvotes: 47

Views: 29463

Answers (5)

Antrikshy
Antrikshy

Reputation: 3106

For anyone who'd prefer to configure this in vite.config.js instead of the CLI/package.json, add it here;

export default defineConfig({
    plugins: [sveltekit()],
    
    // ... other stuff ...

    server: {
        port: 4000
    }
});

I also tried reading it from an env variable using process.env.YOUR_PORT_VARIABLE in vite.config.js, and that works too.

Upvotes: 0

sk shahriar ahmed raka
sk shahriar ahmed raka

Reputation: 1179

As now svelte uses vitejs so for both svelte and sveltekit.

If you want to change it to a fixed port for your project. Inside your package.json file under "scripts": change the dev script

 "dev": "vite --port 3333",

If you want to change it at the time of starting development server

npm run dev -- --port=3333

Upvotes: 5

Ronivon de Matos
Ronivon de Matos

Reputation: 83

Go to package.json, you will find this line:

"start": "sirv public --no-clear"

Change it to this, or to any other port that you want:

"start": "sirv public --no-clear  --port 8089"

Upvotes: 3

CD..
CD..

Reputation: 74176

The sveltejs/template uses sirv-cli.
You can add --port or -p in your start:dev script in package.json.

Instead of:

"start:dev": "sirv public --single --dev"

Use:

"start:dev": "sirv public --single --dev --port 5555"

You can see more of sirv-cli options:

https://github.com/lukeed/sirv/tree/master/packages/sirv-cli

Upvotes: 53

Francky
Francky

Reputation: 341

You can use env vars HOST and PORT.

From https://www.npmjs.com/package/sirv-cli:

Note: The HOST and PORT environment variables will override flag values.

Like this:

HOST=0.0.0.0 PORT=6000 npm run dev

Upvotes: 34

Related Questions