Skip to content

How to import components in Svelte

Learn how to import components in Svelte

Svelte provides single file components. Every component is declared into a .svelte file, and in there you can write the HTML markup, the CSS and the JavaScript needed.

Here’s a simple Svelte component example, living in a file called Button.svelte:

<button>A button</button>

You can add CSS and JS to this component, but this plain HTML markup is already the markup of the component, there’s no need to wrap it in another special tag or anything.

To export this markup from this component you don’t have to do anything. You can now import it into any other Svelte component using the import ComponentName from 'componentPath' syntax:

<script>
import Button from './Button.svelte';
</script>

And now you can use the newly imported component in the markup, like an HTML tag:

<Button />

→ Get my Svelte Handbook

→ I wrote 17 books to help you become a better developer:

  • C Handbook
  • Command Line Handbook
  • CSS Handbook
  • Express Handbook
  • Git Cheat Sheet
  • Go Handbook
  • HTML Handbook
  • JS Handbook
  • Laravel Handbook
  • Next.js Handbook
  • Node.js Handbook
  • PHP Handbook
  • Python Handbook
  • React Handbook
  • SQL Handbook
  • Svelte Handbook
  • Swift Handbook
...download them all now!

Also, JOIN MY CODING BOOTCAMP, an amazing cohort course that will be a huge step up in your coding career - covering React, Next.js - next edition February 2025

Bootcamp 2025

Join the waiting list