DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Building A Simple AI Application in 2023 for Fun and Profit
  • Configuring SSO Using WSO2 Identity Server
  • What Developers Need to Know About the Price Checker App
  • Auto-Scaling a Spring Boot Native App With Nomad

Trending

  • How to Merge HTML Documents in Java
  • What’s Got Me Interested in OpenTelemetry—And Pursuing Certification
  • Monoliths, REST, and Spring Boot Sidecars: A Real Modernization Playbook
  • Enhancing Business Decision-Making Through Advanced Data Visualization Techniques

Creating Your First Svelte App With SvelteKit

Svelte is a front-end tool for creating web applications. In this tutorial, we'll be looking at how to make your first Svelte application.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Mar. 17, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
2.8K Views

Join the DZone community and get the full member experience.

Join For Free

Svelte is a lightweight framework for building web applications. When you use it, it looks and feels a lot like other frontend frameworks like React and Vue, but leaves the virtual DOM behind. That, along with other optimizations, means it does far less work in the browser, optimizing user experience and load time.

In this guide, we'll be going over how to set up your first Svelte application using SvelteKit. Svelte has a number of different ways to make applications, and SvelteKit is one of the official packages from Svelte for doing that. If you're interested in other frameworks, you might enjoy a similar guide we have on making your first Vue application.

Creating Your First Svelte Application

To get started, open up a new terminal window and initiate your Svelte application using the command below. Note if you don't have npm installed, you'll need to get it. You can install npm by installing Node.JS, via the link here.

Once you have Node.JS and NPM installed, run the command below. Before you do that, though, make sure you use cd to move into the folder you want to create your new Svelte application in.

npm init svelte@next my-svelte-app


When you run this command, you'll auto-generate a Svelte template in a folder called my-svelte-app. Svelte will guide you through a number of options. Select your preferences. The image below shows the one's I have selected. For the purposes of this guide, I will be using the Skeleton project.

Finally, run the following command to cd into your Svelte directory:

cd my-svelte-app


Then install all of your dependencies using the following line:

npm i


Svelte File Structure

If you are familiar with other frameworks, then Svelte will feel familiar. Here is an overview of the file structure in Svelte for the project we have just made:

static                 <-- where we store all of our public assets like favicons, images, and fonts
|- favicon.png         <-- our favicon
tests                  <-- a folder to store our tests
|- test.js             <-- an example test using @playwright
src                    <-- our main Svelte app files
|- routes              <-- a folder to store all of our routes in
|-- index.svelte       <-- our index route file. This will be the file displayed at the route of the site
|- app.d.ts            <-- our core Svelte app file
|- app.html            <-- our main index file where the app will appear
.gitignore             <-- files we wish to ignore for git
.npmrc                 <-- config file for npm
.prettierrc            <-- config file for prettier
.eslintrc.cjs          <-- config file for eslint
package.json           <-- our NPM installed packages
playwright.config.js   <-- config file for playwright
svelte.config.js       <-- config file for svelte itself
tsconfig.json          <-- config file for typescript


Our basic Svelte application is ready to go. If you want to see how it looks, you can serve it on your local computer on the URL http://localhost:3000 by running the following command in your Svelte application folder:

npm run dev


If you visit http://localhost:3000 in your browser, you should see something like this:

Welcome to SvelteKit

Creating New Pages or Routes in Svelte

To make a new route in SvelteKit, simply make a new file within the routes folder. For example, if you make a file called "about.svelte", then it will show up at http://localhost:3000/about. Another way you can do this is to make a new folder called about, put "index.svelte" in that folder, and http://localhost:3000/about will work.

Try It Yourself

Create a new page within your /src/routes folder, called about.svelte. Now when you go to http://localhost:3000/, you will be able to access that page. Similarly, you can try making a folder called about with a file placed inside called index.svelte.

How To Run Your SvelteKit App on Node.js

To run your Svelte application on a server or locally on a Node.js server, you need to use an adapter. If you want to run your Svelte application on a Node Server, install @sveltejs/adapter-node@next via the following line:

npm i @sveltejs/adapter-node@next 


Now we have to change our svelte.config.js file. We need to use the new adapter and change our kit.adapter. You can replace the contents of your svelte.config.js with the code below, but we're only changing two lines: our adapter import, and then adding the build directory in your config:

// We have changed the adapter line to use adapter-node@next
import adapter from '@sveltejs/adapter-node@next';
import preprocess from 'svelte-preprocess';

/** @type {import('@sveltejs/kit').Config} */
const config = {
    // Consult https://github.com/sveltejs/svelte-preprocess
    // for more information about preprocessors
    preprocess: preprocess(),

    kit: {
        // We have changed this to point to a build directory
        adapter: adapter({ out: 'build' })
    }
};

export default config;


Other SvelteKit Adapters

If you want to run your Svelte application on Cloudflare, Netlify, or Vercel, then you need to use one of these adapters, and you don't need to do anything. These are all included by default in adapter-auto so only change your svelte.config.js file if you aren't planning on using a Node.JS server.

How To Build Your SvelteKit App for Production

Now that we've configured our adapter, let's build our application. In SvelteKit, it's easy to make your app ready to run in a production environment. Simply run the following command, which will create a new folder called .svelte-kit with all your production-ready files in it.

npm run build


Now, if you want to preview your production build, simply run the following command:

npm run preview


If you are running your application on a Node.JS server and have updated your adapter, as shown in the previous section, then you can run your new Svelte application locally by running the following command in your Svelte directory:

node build/index.js


Now when you navigate to http://localhost:3000/, your Svelte application should show, only this time it will be ready for production.

Conclusion

In this guide, we've looked at how to use SvelteKit to create your first Svelte application with routes. Let's look at what we've learned:

  1. How to set up SvelteKit and create the basic structure of your Svelte application
  2. How to use routes in SvelteKit, so you can have multiple pages on your application
  3. How to update your config file to use the right adapter, based on where you want to deploy your application
  4. How to build and run your application locally on a Node.js server

Next, you can try playing around with Svelte to start customizing your application. For more web content, don't forget to follow me on Twitter.

Svelte application app

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building A Simple AI Application in 2023 for Fun and Profit
  • Configuring SSO Using WSO2 Identity Server
  • What Developers Need to Know About the Price Checker App
  • Auto-Scaling a Spring Boot Native App With Nomad

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!