How To Change The Default Port 5000 in Svelte?
Last Updated :
16 Aug, 2024
In Svelte, the application is by default rendered on port 5000 when using the development server. However, you might want to change this port to avoid conflicts with other applications or to fit specific requirements.
It can be done through different approaches, such as updating the package.json file or using environment variables to specify a new port number.
Steps to Create a Svelte Application
Step 1: Install Node.js
Make sure Node.js is installed on your machine. You can download it from nodejs.org.
Step 2: Install Svelte
Open your terminal and run the following command to set up a new Svelte project:
npx degit sveltejs/template svelte-app
Step 3: Navigate to Your Project Directory
Change to the newly created project directory:
cd svelte-app
Step 4: Install Dependencies
Install the required npm packages:
npm install
Project Structure
Folder StructureDependencies
"devDependencies": {
"@rollup/plugin-commonjs": "^24.0.0",
"@rollup/plugin-node-resolve": "^15.0.0",
"@rollup/plugin-terser": "^0.4.0",
"rollup": "^3.15.0",
"rollup-plugin-css-only": "^4.3.0",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-svelte": "^7.1.2",
"svelte": "^3.55.0"
},
"dependencies": {
"sirv-cli": "^2.0.0"
}
Approach 1: Updating package.json
File
In this approach, we are using the sirv CLI tool to serve the static files and specify the desired port directly in the package.json file. By updating the "start" script with the --port flag followed by the desired port number (e.g., 3000), we override the default port and set the server to listen on the new port.
Syntax:
"start": "sirv public --no-clear --port port_number"
Update the “scripts” section in package.json to specify the desired port:
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public --no-clear --port 3000" // Changed port number to 3000
}
Example: The below example is displayed on the port number which is updated in package.json file.
JavaScript
<!--App.svelte-->
<script>
// Function to extract the port number from the URL
function getPortFromUrl() {
const url = new URL(window.location.href);
return url.port || 'Port not specified';
}
// Extract the port number on component mount
let portNumber = getPortFromUrl();
</script>
<style>
h1 {
color: green;
}
h3 {
color: #555;
}
</style>
<main>
<h1>GeeksforGeeks</h1>
<h3>Approach 1: Updating package.json File</h3>
<p>The port number extracted from the URL is: {portNumber}</p>
</main>
Output
Change The Default Port 5000 in SvelteApproach 2: Using Environment Variables
In this approach, we are using environment variables to set the port number dynamically. By modifying the "start" script in package.json to use the $PORT environment variable, and then setting the PORT variable in the command line or terminal (e.g., $env:PORT=5005), we can control the port on which the server listens.
Syntax
"start": "sirv public --no-clear --port $PORT"
Set the Environment Variable
$env:PORT=5005
Start the Application
npm run dev
Change The Default Port 5000 in SvelteExample: The below example is displayed on the port number which is specified using environment variable.
JavaScript
<!--App.svelte-->
<script>
// Function to extract the port number from the URL
function getPortFromUrl() {
const url = new URL(window.location.href);
return url.port || 'Port not specified';
}
// Extract the port number on component mount
let portNumber = getPortFromUrl();
</script>
<style>
h1 {
color: green;
}
h3 {
color: #555;
}
</style>
<main>
<h1>GeeksforGeeks</h1>
<h3>Approach 2: Using Environment Variables</h3>
<p>The port number extracted from the URL is: {portNumber}</p>
</main>
Output
Change The Default Port 5000 in Svelte
Similar Reads
How to Change the Default Port in PostgreSQL
PostgreSQL is one of the most powerful and widely used relational database management systems (RDBMS) in the world. By default, PostgreSQL listens for incoming connections on port 5432. In this article, we will describe the process of changing the default port for PostgreSQL in detailed and step-by-
6 min read
How to Change the Default Port in Spring Boot?
Spring Boot framework provides a default embedded server i.e. the Tomcat server for many configuration properties to run the Spring Boot application. The application runs on the default port which is 8080. As per the application need, we can also change this default port for the embedded server. In
4 min read
How to Change the Default Port in Jenkins?
Configuring the port in Jenkins is a fundamental aspect of managing the Jenkins server. By default, Jenkins runs on port 8080, but there are scenarios where you may need to change this port to avoid conflicts with other services or security reasons. Understanding how to configure the port in Jenkins
5 min read
Change the Django Default Runserver Port
Changing the default run server port in Django is a simple yet useful adjustment for various development scenarios. By specifying a different port, you can avoid conflicts, manage multiple projects, and test your application in different environments. Remember to adjust your development and testing
3 min read
How to Change Port in Flask app
In this article, we will learn to change the port of a Flask application. The default port for the Flask application is 5000. So we can access our application at the below URL. http://127.0.0.1:5000/ We may want to change the port may be because the default port is already occupied. To do that we ju
1 min read
How to Change Default MySQL/MariaDB Port in Linux?
The default port that the MySQL database server runs under Linux is 3306/TCP. Use the commands below to change the default MySQL/MariaDB Database port in Linux. vi /etc/mysql/mariadb.conf.d/50-server.cnf Search for the line MYSQL, find port under this line, and replace port values accordingly. [mysq
1 min read
How to Change FTP Port in Linux?
Files are either uploaded or downloaded to the FTP server. The files are moved from a personal computer to the server when you upload files. The files are moved from the cloud to your personal computer when the files are downloaded. In order to transfer files through FTP, TCP/IP (Transmission Contro
1 min read
How to Import SVG Icons into a Svelte App?
SVG Icons are used in Svelte applications for creating scalable, high-quality graphics that can be easily styled and animated. They are lightweight, making them ideal for enhancing the visual appeal of modern web applications. In this article, we'll explore two different approaches to importing and
3 min read
How to change port in Next.js App
Next.js provides the flexibility to change the default port of the development server using command-line options such as -p <port_number> or by updating the "scripts" section in the package.json file, offering developers multiple ways to customize the port configuration based on their requirem
3 min read
How to Loop Each Block X Amount of Times in Svelte?
Svelte is a modern JavaScript framework that offers a unique approach to building web applications by compiling components at build time. One common requirement in web development is iterating over a block of code a specific number of times. In Svelte, this can be achieved using the {#each} block. T
3 min read