{"id":14952,"library":"svelte-language-server","title":"Svelte Language Server","description":"The Svelte Language Server (package `svelte-language-server`, currently at version 0.17.30) provides language-specific smarts for Svelte development within various editors by implementing the Language Server Protocol (LSP). It acts as the core engine for features like diagnostics, autocompletion, hover information, formatting, and code actions for Svelte, HTML, CSS/SCSS/LESS, and TypeScript/JavaScript within Svelte components. It is an actively maintained component of the broader `sveltejs/language-tools` monorepo, with frequent patch releases addressing performance improvements and bug fixes, typically on a weekly or bi-weekly cadence. Key differentiators include its deep integration with the Svelte compiler and TypeScript, leveraging `svelte2tsx` for seamless type-checking, and its ability to provide comprehensive language features across multiple embedded languages within `.svelte` files. Unlike many libraries consumed directly via `import`, this package is primarily designed to be run as a separate process by editor extensions (like the official VS Code Svelte extension) or custom LSP clients, offering a robust and extensible foundation for Svelte tooling. Its current stable versions require Node.js >= 18.0.0.","status":"active","version":"0.17.30","language":"javascript","source_language":"en","source_url":"https://github.com/sveltejs/language-tools","tags":["javascript","svelte","vscode","atom","editor","language-server","typescript"],"install":[{"cmd":"npm install svelte-language-server","lang":"bash","label":"npm"},{"cmd":"yarn add svelte-language-server","lang":"bash","label":"yarn"},{"cmd":"pnpm add svelte-language-server","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for TypeScript and JavaScript language features; listed as a peer dependency.","package":"typescript","optional":false},{"reason":"Internal dependency for transforming Svelte code into TypeScript for type-checking.","package":"svelte2tsx","optional":false}],"imports":[{"note":"For programmatic access to the language server's configuration manager, useful for custom tooling or extensions.","symbol":"LSConfigManager","correct":"import { LSConfigManager } from 'svelte-language-server'"},{"note":"Represents a Svelte file within the language server's document model, useful for advanced programmatic interactions.","symbol":"SvelteDocument","correct":"import { SvelteDocument } from 'svelte-language-server'"},{"note":"The base class for language server plugins, used when extending the server's capabilities with custom language features.","symbol":"SveltePlugin","correct":"import { SveltePlugin } from 'svelte-language-server'"}],"quickstart":{"code":"import { spawn } from 'child_process';\nimport { resolve } from 'path';\n\n// This quickstart demonstrates how to programmatically launch the Svelte Language Server\n// and send an 'initialize' request, as a basic example for building an LSP client.\n// In a typical setup, users interact via editor extensions (e.g., VS Code Svelte extension).\n\nconst serverExecutable = resolve(__dirname, 'node_modules', 'svelte-language-server', 'bin', 'server.js');\n\n// Spawn the language server process\nconst lsProcess = spawn('node', [serverExecutable], {\n    stdio: ['pipe', 'pipe', 'pipe'] // stdin, stdout, stderr pipes\n});\n\nlet requestId = 1;\n\nfunction sendLSPRequest(method: string, params: any) {\n    const request = {\n        jsonrpc: '2.0',\n        id: requestId++,\n        method,\n        params\n    };\n    const jsonRequest = JSON.stringify(request);\n    const contentLength = Buffer.byteLength(jsonRequest, 'utf8');\n    const header = `Content-Length: ${contentLength}\\r\\n\\r\\n`; // Simplified header\n    lsProcess.stdin.write(header + jsonRequest);\n}\n\n// Listen for responses (simplified, real clients handle streaming and headers)\nlsProcess.stdout.on('data', (data) => {\n    console.log('Language Server Output:', data.toString().trim());\n});\n\nlsProcess.stderr.on('data', (data) => {\n    console.error('Language Server Error:', data.toString().trim());\n});\n\nlsProcess.on('close', (code) => {\n    console.log(`Language Server process exited with code ${code}`);\n});\n\n// Send the initial 'initialize' request, crucial for LSP setup\nsendLSPRequest('initialize', {\n    processId: process.pid,\n    clientInfo: { name: 'Svelte LS Quickstart', version: '1.0' },\n    rootUri: 'file:///home/user/my-svelte-project', // Replace with actual project root\n    capabilities: {}, // Define client capabilities as per LSP spec\n    initializationOptions: { // Pass initial configuration\n        configuration: {\n            svelte: {\n                plugin: {\n                    typescript: { enable: true },\n                    html: { enable: true }\n                },\n                // Other Svelte language server specific settings\n            },\n            typescript: { /* TypeScript specific settings */ },\n            javascript: { /* JavaScript specific settings */ },\n        }\n    }\n});\n\n// In a real application, you would continue to send requests (e.g., didOpen, textDocument/hover)\n// and handle responses. For this quickstart, we'll just demonstrate initialization.\n// Ensure to properly manage the lifecycle of the child process.\nprocess.on('exit', () => lsProcess.kill()); // Clean up on parent exit","lang":"typescript","description":"Demonstrates how to launch the Svelte Language Server as a child process and send an `initialize` request, illustrating basic programmatic interaction for building an LSP client."},"warnings":[{"fix":"Upgrade your Node.js environment to version 18.0.0 or higher.","message":"As of `svelte-language-server@0.17.0` (part of `language-tools` v100.1.0), the minimum Node.js version required is 18.0.0. Older Node.js versions will prevent the server from starting.","severity":"breaking","affected_versions":">=0.17.0"},{"fix":"Refer to the official documentation for `svelte-language-server` settings and the `svelte-vscode` extension settings for common configurations. Use the `initializationOptions.configuration` object for programmatic setup.","message":"The language server's configuration is highly customizable. Incorrect or conflicting settings, especially those related to TypeScript, CSS preprocessors, or formatting (Prettier), can lead to unexpected behavior or disabled features.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Ensure `typescript` is installed in your project's `devDependencies` (e.g., `npm install -D typescript@^5.0.0`) and that its version is compatible with the language server's requirements.","message":"The `typescript` package is a peer dependency. If it's not installed in your project or if an incompatible version is present, TypeScript-related language features (diagnostics, autocompletion for TS/JS) will not function correctly.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Configure `svelte.plugin.typescript.workspace.exclude` and similar options in your editor or language server initialization options. For CI/CD, use `svelte-check --incremental`.","message":"Performance issues in large Svelte projects can arise if the language server is not properly configured with workspace exclusions (e.g., `node_modules`, `dist` directories) or if `svelte-check` is run without `--incremental` flags where applicable.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Install TypeScript in your project: `npm install -D typescript` or `yarn add -D typescript`.","cause":"The `typescript` package is a peer dependency but is not installed or not resolvable in the project.","error":"Cannot find module 'typescript' or its corresponding type declarations."},{"fix":"Check your Node.js version (must be >= 18.0.0). Inspect the server's `stderr` output for more specific error messages if available.","cause":"This generic error often indicates an unhandled exception within the server or an incompatible Node.js environment.","error":"The Svelte Language Server process terminated unexpectedly with exit code X."},{"fix":"Update `svelte-language-server` to version `0.17.30` or newer, as this issue has been addressed in recent patches. Ensure your `tsconfig.json`'s `moduleResolution` is correctly configured.","cause":"This was a known bug in older versions related to module resolution strategies in specific TypeScript environments.","error":"Could not resolve Svelte files with NodeNext in --incremental/tsgo mode."},{"fix":"Ensure `svelte.plugin.typescript.enable` is set to `true` in your language server configuration (e.g., editor settings). Verify your TypeScript installation and version.","cause":"The TypeScript plugin within the language server might be disabled or misconfigured, or there's an issue with the underlying TypeScript installation.","error":"TypeScript/JavaScript features (diagnostics, autocompletion) are not working in Svelte files."}],"ecosystem":"npm"}