WebStorm 2023.2 Help

Svelte

WebStorm provides basic Svelte framework functionality such as syntax highlighting, code completion with automatic imports, refactorings, navigation, intentions, code folding, live templates support, and correct formatting.

Before you start

  1. Make sure you have Node.js on your computer.

  2. Install and enable the Svelte plugin on the Settings | Plugins page, tab Marketplace, as described in Installing plugins from JetBrains Marketplace.

Create a Svelte application

The recommended way to create a Svelte application is to use SvelteKit, the official application framework from the Svelte team.

  1. Open your command-line shell or the embedded Terminal (Alt+F12) and type one of the following commands depending on the package manager you are using:

    • npm create svelte@latest ./<project_name> for npm

    • pnpm create svelte@latest ./<project_name> for pnpm

  2. Answer the questions in the wizard that starts.

    Svelte: project generation wizard
  3. Open the generated application with Svelte-specific structure (File | Open) and run npm install to install the dependencies.

    Svelte: open an app and install dependencies

Learn more from the Svelte official website.

Start with an existing Svelte application

To continue developing an existing Svelte application, open it in WebStorm and download the required dependencies.

Open the application sources that are already on your machine

  • Click Open on the Welcome screen or select File | Open from the main menu. In the dialog that opens, select the folder where your sources are stored.

Check out the application sources from your version control

  1. Click Get from VCS on the Welcome screen.

    Alternatively, select File | New | Project from Version Control or Git | Clone… from the main menu.

    Instead of Git in the main menu, you may see any other Version Control System that is associated with your project. For example, Mercurial or Perforce.

  2. In the dialog that opens, select your version control system from the list and specify the repository to check out the application sources from. For more information, refer to Check out a project (clone).

Download the dependencies

  1. Click Run 'npm install' or Run 'yarn install' in the popup:

    Opening an Angular application and downloading the dependencies from package.json
  2. Select Run 'npm install' or Run 'yarn install' from the context menu of package.json in the editor or in the Project tool window.

Project security

When you open a project that was created outside WebStorm and was imported into it, WebStorm displays a dialog where you can decide how to handle this project with unfamiliar source code.

Untrusted project warning

Select one of the following options:

  • Preview in Safe Mode: in this case, WebStorm opens the project in a preview mode. It means that you can browse the project's sources but you cannot run tasks and script or run/debug your project.

    WebStorm displays a notification on top of the editor area, and you can click the Trust project… link and load your project at any time.

  • Trust Project: in this case, WebStorm opens and loads a project. That means the project is initialized, project's plugins are resolved, dependencies are added, and all WebStorm features are available.

  • Don't Open: in this case, WebStorm doesn't open the project.

Learn more from Project security.

Configure coding assistance in Svelte code

You can get type checking and completion in your Svelte code through integration with the Svelte Language Server. Alternatively, you can use only WebStorm internal inspections and code completion.

  1. In the Settings dialog (Control+Alt+S), go to Languages & Frameworks | TypeScript | Svelte.

  2. Specify where to get coding assistance from:

    • To use the Svelte Language Server, select Enabled.

    • To use WebStorm internal WebStorm inspections and code completion, select Disabled.

Run your application

Run a Svelte application
  1. Open your package.json, click the Run button in the gutter next to the dev script, and select Run 'dev' from the context menu.

    Alternatively, you can use the dev run/debug configuration of the type npm that WebStorm generates when you run the dev script for the first time.

    Autogenerated dev run/debug configuration
  2. To open the application in the browser, click the link in the Run tool window.

    While the application is running, all the changes you make to your code are immediately reflected on the page.

Debug your application

To start debugging a Svelte application, you need a run/debug configuration of the type JavaScript Debug.

Debug Svelte applications running on the localhost

  1. Set the breakpoints in your code.

  2. Run your application in the development mode as described above.

  3. The Run tool window or the Terminal shows the URL at which your application is running, by default it is http://localhost:5173/. Hold Control+Shift and click this URL link. WebStorm starts a debugging session with an automatically generated localhost configuration of the type JavaScript Debug.

    Starting a debugging session from the Run tool window
  4. When the first breakpoint is hit, switch to the Debug tool window and proceed as usual: step through the program, stop and resume program execution, examine it when suspended, explore the call stack and variables, set watches, evaluate variables, view actual HTML DOM, and so on.

Debug Svelte applications running on custom URLs

  1. Set the breakpoints in your code.

  2. Run your application in the development mode as described above.

  3. The Run tool window or the Terminal shows the URL at which your application is running. Copy this URL address, you will later specify it in a debug configuration. To view your application, just click the link.

  4. Create a JavaScript Debug configuration. To do that, go to Run | Edit Configurations on the main menu, click the Add icon, and select JavaScript Debug from the list. In the Run/Debug Configuration: JavaScript Debug dialog, paste the saved URL in the URL field and save the configuration.

    Svelte app: create a run/debug configuration
  5. When the first breakpoint is hit, switch to the Debug tool window and proceed as usual: step through the program, stop and resume program execution, examine it when suspended, explore the call stack and variables, set watches, evaluate variables, view actual HTML DOM, and so on.

Write and edit your code

In .svelte components and related files WebStorm provides syntax highlighting, code completion with automatic imports, refactorings, navigation, intentions, code folding, live templates support, and correct formatting.

Create Svelte components

  • In the Project tool window, select the parent folder for the new component, and then choose Svelte Component from the list.

    Create Svelte component

Code completion

Besides the common completion, WebStorm provides completion for Svelte-specific symbols.

WebStorm shows suggestions for completion as you type. Alternatively, place the caret at the symbol to complete and press Control+Space.

Svelte: code completion

Auto import

WebStorm generates import statements on the fly, as you type or complete your code in .svelte and in JavaScript files.

Auto import on completion
Auto import on completion

Symbols that have not been imported are marked with an underscore line. To insert an import statement, hover over an underscored symbol, and click the Import <symbol name> link in the popup.

Svelte: import missing, quick fix popup
Svelte: import missing, quick fix popup

Alternatively, place the caret at the symbol to import, press Alt+Enter, and select Import <symbol name> from the list.

Svelte: insert import quick fix
Svelte: insert import quick fix

Learn more from Auto import.

Refactoring code

Svelte: refactoring
  • Select a code fragment to refactoring or place the caret inside it, press Control+Alt+Shift+T, and select the required refactoring from the list.

Learn more from Code refactoring, Refactoring JavaScript, and Refactoring TypeScript.

Test your application

Test Svelte application

The recommended way to test Svelte applications in WebStorm is to use the Vitest framework.

If you create your application with SvelteKit as described above, select the Add Vitest for unit testing option when passing the project generation wizard.

Add Vitest support
Last modified: 01 September 2023