### Define Add-on with Next Steps Guidance Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/99-community.md Example of defining an add-on with custom console guidance for users after installation. ```js import { color } from '@sveltejs/sv-utils'; export default defineAddon({ // ... nextSteps: ({ options }) => [ `Run ${color.command('npm run dev')} to start developing`, `Check out the docs at https://...` ] }); ``` -------------------------------- ### Start Svelte Library Development Server Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/create/shared/+library/README.md After installing dependencies, run 'npm run dev' to start the development server. Use '-- --open' to automatically open the app in a new browser tab. ```sh npm run dev # or start the server and open the app in a new browser tab npm run dev -- --open ``` -------------------------------- ### Start Svelte Development Server Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/cli/tests/snapshots/create-only/README.md Run this command after installing dependencies to start the development server. The `--open` flag will automatically open the application in your default browser. ```bash npm run dev ``` ```bash npm run dev -- --open ``` -------------------------------- ### Install @my-org/sv Add-on Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/cli/tests/snapshots/@my-org/sv/README.md Run this command to install the community add-on. ```shell npx sv add @my-org ``` -------------------------------- ### START Record Example Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/30-sv-check.md The initial record indicates the start of the check and includes the workspace folder path. ```text 1590680325583 START "/home/user/language-tools/packages/language-server/test/plugins/typescript/testfiles" ``` -------------------------------- ### Configure Svelte MCP with Setup Option Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/17-mcp.md Install the Svelte MCP add-on and specify the setup type, for example, 'local'. ```sh npx sv add mcp="setup:local" ``` -------------------------------- ### Install @sveltejs/sv-utils Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/50-api/20-sv-utils.md Install the package as a development dependency. ```sh npm install -D @sveltejs/sv-utils ``` -------------------------------- ### Install Svelte MCP Add-on Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/17-mcp.md Run this command to install the Svelte MCP add-on. ```sh npx sv add mcp ``` -------------------------------- ### Add ESLint to Project Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/10-eslint.md Run this command to install ESLint and its necessary configurations. ```bash npx sv add eslint ``` -------------------------------- ### Install Svelte CLI Add-on Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/create/shared/+addon/README.md Use this command to install a community add-on. Replace '~SV-PROTOCOL-NAME-TODO~' with the actual protocol name. ```shell npx sv add ~SV-PROTOCOL-NAME-TODO~ ``` -------------------------------- ### Configure Vitest for sv/testing global setup Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/99-community.md Ensure your `vitest.config.js` includes the global setup script from `sv/testing` to properly initialize the testing environment. ```js import { defineConfig } from 'vitest/config'; export default defineConfig({ test: { include: ['tests/**/*.test.{js,ts}'], globalSetup: ['tests/setup/global.js'] } }); ``` -------------------------------- ### Install Add-on with Specific Version Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/99-community.md Users can specify a particular version of an add-on when installing. ```sh npx sv add @my-org/sv@1.2.3 ``` -------------------------------- ### Run the Svelte CLI Source: https://github.com/sveltejs/cli/blob/main/packages/sv/README.md Execute the Svelte CLI to create and maintain Svelte applications. No specific setup is required beyond having Node.js and npm installed. ```sh npx sv ``` -------------------------------- ### Clone and Install Dependencies Source: https://github.com/sveltejs/cli/blob/main/CONTRIBUTING.md Clone the Svelte CLI repository and install all project dependencies using pnpm. This command should be run from the project's root directory. ```bash git clone https://github.com/sveltejs/cli.git cd cli pnpm i ``` -------------------------------- ### Vercel Install Command Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/create/templates/demo/README.md Use this command to install pnpm and build the project for Vercel. ```bash npm install -g pnpm && pnpm install && pnpm build -r ``` -------------------------------- ### Install svelte-check Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/30-sv-check.md Install the `svelte-check` package as a development dependency in your project. ```sh npm i -D svelte-check ``` -------------------------------- ### Run sv add with specified add-ons Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/20-sv-add.md Provide a space-separated list of add-ons to install directly when running the `sv add` command. ```sh npx sv add [add-ons] ``` -------------------------------- ### Add Storybook Add-on Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/40-storybook.md Run this command to install and configure the Storybook add-on for your project. ```bash npx sv add storybook ``` -------------------------------- ### Global test setup script for sv/testing Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/99-community.md This script, `tests/setup/global.js`, is required by Vitest's global setup. It configures the test directory for `sv/testing`. ```js import { fileURLToPath } from 'node:url'; import { setupGlobal } from 'sv/testing'; const TEST_DIR = fileURLToPath(new URL('../../.test-output/', import.meta.url)); export default setupGlobal({ TEST_DIR }); ``` -------------------------------- ### Add SvelteKit Adapter Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/45-sveltekit-adapter.md Installs and configures the chosen SvelteKit adapter. Use this command to add the adapter functionality to your project. ```bash npx sv add sveltekit-adapter ``` -------------------------------- ### Install community add-on by organization Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/20-sv-add.md Install a community add-on by specifying its organization name. The command will look for packages prefixed with `@org/sv`. ```sh # Install a community add-on by org name (it will look at @org/sv) npx sv add @supacool ``` -------------------------------- ### Install Drizzle ORM with PostgreSQL, postgres.js, and Docker Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/05-drizzle.md Install the Drizzle ORM add-on with PostgreSQL, the postgres.js client, and include Docker Compose configuration. ```bash npx sv add drizzle="database:postgresql+client:postgres.js+docker:yes" ``` -------------------------------- ### Install pnpm Source: https://github.com/sveltejs/cli/blob/main/CONTRIBUTING.md Install the pnpm package manager globally. This is a prerequisite for managing the monorepo dependencies. ```bash npm i -g pnpm ``` -------------------------------- ### Configure Better Auth Demo Pages Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/02-better-auth.md Specify which demo authentication pages to include when installing Better Auth. Options include Email & Password, GitHub OAuth, or both. ```bash npx sv add better-auth="demo:password" ``` ```bash npx sv add better-auth="demo:github" ``` ```bash npx sv add better-auth="demo:password,github" ``` -------------------------------- ### Enable Paraglide Demo Page Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/25-paraglide.md Optionally generate a demo page to showcase Paraglide.js usage. Set the 'demo' option to 'yes' during installation. ```bash npx sv add paraglide="demo:yes" ``` -------------------------------- ### Install Better Auth Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/02-better-auth.md Use this command to add the Better Auth add-on to your SvelteKit project. ```bash npx sv add better-auth ``` -------------------------------- ### Install Add-on with Scoped Package Name and Sub-package Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/99-community.md Users can install specific add-ons from scoped packages by providing the full package name. ```sh npx sv add @my-org/core ``` -------------------------------- ### Configure Node Adapter for SvelteKit Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/45-sveltekit-adapter.md Installs and configures the Node.js adapter for SvelteKit. This is useful for deploying to Node.js environments. ```bash npx sv add sveltekit-adapter="adapter:node" ``` -------------------------------- ### createSetupTest Source: https://github.com/sveltejs/cli/blob/main/packages/sv/api-surface-testing.md Creates a setup function for Vitest tests, optionally integrating with Playwright. ```APIDOC ## createSetupTest ### Description Creates a setup function for Vitest tests, optionally integrating with Playwright. ### Signature ```typescript (vitest: VitestContext, playwright?: PlaywrightContext) => (addons: Addons, options?: SetupTestOptions) => { test: _$vitest.TestAPI; testCases: Array>; prepareServer: typeof prepareServer; } ``` ### Parameters #### Arguments - **vitest** (VitestContext) - Required - The Vitest context. - **playwright** (PlaywrightContext) - Optional - The Playwright context. ``` -------------------------------- ### Add-on package.json configuration Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/99-community.md This `package.json` example shows the required configuration for publishing a Svelte CLI add-on, including `sv` as a peer dependency and specific keywords. ```jsonc { "name": "@my-org/sv", "version": "1.0.0", "type": "module", // bundled entrypoint (tsdown outputs .mjs for ESM) "exports": { ".": {"default": "./dist/index.mjs"} }, "publishConfig": { "access": "public" }, // cannot have dependencies "dependencies": {}, "peerDependencies": { // minimum version required to run by this add-on "sv": "^0.13.0" }, // Add the "sv-add" keyword so users can discover your add-on "keywords": ["sv-add", "svelte", "sveltekit"] } ``` -------------------------------- ### Run sv migrate Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/40-sv-migrate.md Execute the sv migrate command to start the migration process. This will typically prompt for migration choices or run a default migration. ```sh npx sv migrate ``` -------------------------------- ### Define a custom SvelteKit add-on with options Source: https://context7.com/sveltejs/cli/llms.txt Create a custom add-on using `defineAddon`. This example demonstrates defining options for a greeting message, including conditional styling and dependencies. ```js import { transforms } from '@sveltejs/sv-utils'; import { defineAddon, defineAddonOptions } from 'sv'; export default defineAddon({ id: 'my-greeting', shortDescription: 'Adds a greeting to your SvelteKit home page', options: defineAddonOptions() .add('name', { question: 'What name should be greeted?', type: 'string', default: 'World' }) .add('style', { question: 'Use bold styling?', type: 'boolean', default: false }) .build(), setup: ({ dependsOn, unsupported, isKit }) => { if (!isKit) unsupported('Requires SvelteKit'); dependsOn('prettier'); // will run after prettier if both are selected }, run: ({ sv, options, cancel, directory }) => { // Install a runtime dependency sv.dependency('some-pkg', '^2.0.0'); // Install a dev dependency sv.devDependency('some-dev-pkg', '^1.0.0'); // Append a greeting paragraph to the root page sv.file( directory.kitRoutes + '/+page.svelte', transforms.svelte(({ ast, svelte }) => { const tag = options.style ? `Hello ${options.name}!` : `

Hello ${options.name}!

`; svelte.addFragment(ast, tag); }) ); // Add a DATABASE_URL variable to .env sv.file( '.env', transforms.text(({ content }) => content + '\nGREETING_ENABLED=true') ); }, nextSteps: ({ options }) => [ `Your greeting for "${options.name}" has been added`, "Run `npm run dev` to see it in action" ] }); ``` -------------------------------- ### Install Playwright Add-on Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/30-playwright.md Run this command to add the Playwright add-on to your SvelteKit project. This will set up Playwright, add necessary scripts to package.json, create a Playwright config file, update .gitignore, and include a demo test. ```sh npx sv add playwright ``` -------------------------------- ### Install Drizzle ORM Add-on Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/05-drizzle.md Run this command to add the Drizzle ORM add-on to your SvelteKit project. ```bash npx sv add drizzle ``` -------------------------------- ### Run sv add interactively Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/20-sv-add.md Execute the `sv add` command without any arguments to start an interactive prompt for selecting add-ons. ```sh npx sv add ``` -------------------------------- ### Add Vitest Add-on Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/55-vitest.md Installs Vitest and sets up testing for your Svelte project. ```bash npx sv add vitest ``` -------------------------------- ### Add Vitest with Specific Usages Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/55-vitest.md Installs Vitest and configures it for specific test types like unit and component testing. ```bash npx sv add vitest="usages:unit,component" ``` -------------------------------- ### Cloudflare Pages Build Command Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/create/templates/demo/README.md Use this command to install pnpm and build the project for Cloudflare Pages. ```bash npm i -g pnpm && pnpm i && pnpm build -r ``` -------------------------------- ### Add Tailwind CSS with Plugins Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/50-tailwind.md Use this command to install Tailwind CSS along with specific plugins like 'typography'. ```bash npx sv add tailwindcss="plugins:typography" ``` -------------------------------- ### Add Paraglide Add-on Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/25-paraglide.md Install the Paraglide add-on using the Svelte CLI. This command sets up the necessary files and configurations for Paraglide.js. ```bash npx sv add paraglide ``` -------------------------------- ### Migrate lowercase 'get' to uppercase 'GET' Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_server/samples.md Ensure that GET function handlers are named `GET` (uppercase) for SvelteKit to recognize them. This example shows the migration of a lowercase `get` function. ```javascript export function get() { return { body: { a: 1 } }; } ``` ```javascript import { json } from '@sveltejs/kit'; export function GET() { return json({ a: 1 }); } ``` -------------------------------- ### Basic sv create Usage Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/10-sv-create.md Use this command to initialize a new SvelteKit project in the current or a specified directory. ```bash npx sv create [options] [path] ``` -------------------------------- ### Install Drizzle ORM with PostgreSQL and postgres.js Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/05-drizzle.md Configure the Drizzle ORM add-on to use PostgreSQL with the postgres.js SQL client. ```bash npx sv add drizzle="database:postgresql+client:postgres.js" ``` -------------------------------- ### setupGlobal Source: https://github.com/sveltejs/cli/blob/main/packages/sv/api-surface-testing.md Sets up global configurations for testing, including pre and post execution hooks. ```APIDOC ## setupGlobal ### Description Sets up global configurations for testing, including pre and post execution hooks. ### Signature ```typescript ({ TEST_DIR, pre, post }: { TEST_DIR: string; pre?: () => Promise; post?: () => Promise; }): ({ provide }: TestProject) => Promise<() => Promise> ``` ### Parameters #### Options - **TEST_DIR** (string) - Required - The directory for test files. - **pre** (() => Promise) - Optional - A function to execute before tests. - **post** (() => Promise) - Optional - A function to execute after tests. ``` -------------------------------- ### Build and Publish a Community Add-on Source: https://context7.com/sveltejs/cli/llms.txt Commands to build and publish a Svelte add-on to npm. Includes logging in, publishing (which automatically triggers the build via `prepublishOnly`), and instructions for users to install the add-on using `npx sv add`. ```sh # Build and publish npm login npm publish # prepublishOnly runs the build automatically # Users install it with just the org handle: npx sv add @my-org # Or pin to a specific version: npx sv add @my-org/sv@1.2.3 ``` -------------------------------- ### Configure Svelte CLI Add-on Option Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/create/shared/+addon/README.md Install an add-on with a specific option. The 'who' option sets the name for a greeting. Replace '~SV-PROTOCOL-NAME-TODO~' and 'your-name' accordingly. ```shell npx sv add ~SV-PROTOCOL-NAME-TODO~="who:your-name" ``` -------------------------------- ### Recreate Project with Specific Configuration Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/cli/tests/snapshots/create-only/README.md Recreate an existing project using a specific template, type definitions, and version of the Svelte CLI. This is useful for ensuring consistent project setups. ```bash npx sv@0.0.0 create --template minimal --types ts --no-install packages/sv/.test-output/cli/create-only ``` -------------------------------- ### Configure Svelte MCP with IDE Option Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/17-mcp.md Install the Svelte MCP add-on and specify the desired IDEs for integration, such as Cursor and VS Code. ```sh npx sv add mcp="ide:cursor,vscode" ``` -------------------------------- ### prepareServer Source: https://github.com/sveltejs/cli/blob/main/packages/sv/api-surface-testing.md Prepares a development server for testing with specified build and preview commands. ```APIDOC ## prepareServer ### Description Prepares a development server for testing with specified build and preview commands. ### Signature ```typescript (options: PrepareServerOptions): Promise ``` ### Parameters #### Options - **cwd** (string) - Required - The current working directory. - **page** (Page) - Required - The Playwright Page object. - **buildCommand** (string) - Optional - The command to build the project. - **previewCommand** (string) - Optional - The command to preview the project. ``` -------------------------------- ### Create a new SvelteKit project with sv create Source: https://context7.com/sveltejs/cli/llms.txt Scaffolds a new SvelteKit project from a template. Supports interactive prompts or fully non-interactive flags for template selection, type-checking, add-ons, and dependency installation. Can also create projects from Svelte Playground URLs. ```sh npx sv create my-app ``` ```sh npx sv create my-app --template minimal --types ts --add eslint prettier --install pnpm ``` ```sh npx sv create my-app --from-playground="https://svelte.dev/playground/hello-world" ``` ```sh npx sv create my-lib --template library --types jsdoc --no-add-ons --no-install ``` ```sh npx sv create my-app --no-dir-check ``` ```sh pnpm dlx sv create my-app bunx sv create my-app deno run npm:sv create my-app ``` -------------------------------- ### Define a Svelte Add-on with `defineAddon` Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/50-api/10-sv.md Use `defineAddon` to create a custom add-on definition. Configure setup, run, and next steps, including dependencies and file transformations. Requires SvelteKit. ```javascript import { transforms } from '@sveltejs/sv-utils'; import { defineAddon, defineAddonOptions } from 'sv'; export default defineAddon({ id: 'my-addon', options: defineAddonOptions().build(), // called before run — declare dependencies and environment requirements setup: ({ dependsOn, unsupported, isKit }) => { if (!isKit) unsupported('Requires SvelteKit'); dependsOn('eslint'); }, // the actual work — add files, edit files, declare dependencies run: ({ sv, options, cancel }) => { // add a dependency sv.devDependency('my-lib', '^1.0.0'); // create or edit files using transforms from @sveltejs/sv-utils sv.file('src/lib/foo.ts', (content) => { return 'export const foo = true;'; }); sv.file( 'src/routes/+page.svelte', transforms.svelte(({ ast, svelte }) => { svelte.addFragment(ast, '

Hello!

'); }) ); // cancel at any point if something is wrong // cancel('reason'); }, // displayed after the add-on runs nextSteps: ({ options }) => ['Run `npm run dev` to get started'] }); ``` -------------------------------- ### Add Prettier using npx Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/35-prettier.md Run this command to install and configure Prettier in your SvelteKit project. It adds scripts to package.json and creates configuration files. ```bash npx sv add prettier ``` -------------------------------- ### Create a new add-on project Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/99-community.md Use this command to quickly scaffold a new add-on project with a standard structure. ```sh npx sv create --template addon [path] ``` -------------------------------- ### Add ESLint and Prettier to New Project Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/10-sv-create.md Create a new SvelteKit project and simultaneously add ESLint and Prettier as development dependencies. This streamlines the setup for projects requiring code linting and formatting. ```bash npx sv create --add eslint prettier [path] ``` -------------------------------- ### Set up add-on testing with sv/testing Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/99-community.md Configure your test environment using `createSetupTest` from `sv/testing`. This factory helps create SvelteKit projects, run your add-on, and provides access to the resulting files for assertions. ```js import { expect } from '@playwright/test'; import fs from 'node:fs'; import path from 'node:path'; import { createSetupTest } from 'sv/testing'; import * as vitest from 'vitest'; import addon from './index.js'; const { test, testCases } = createSetupTest(vitest)( { addon }, { kinds: [ { type: 'default', options: { 'your-addon-name': { who: 'World' } } } ], filter: (testCase) => testCase.variant.includes('kit'), browser: false } ); test.concurrent.for(testCases)('my-addon $kind.type $variant', async (testCase, ctx) => { const cwd = ctx.cwd(testCase); const page = fs.readFileSync(path.resolve(cwd, 'src/routes/+page.svelte'), 'utf8'); expect(page).toContain('Hello World!'); }); ``` -------------------------------- ### Configure Cloudflare Adapter for SvelteKit Workers Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/45-sveltekit-adapter.md Installs and configures the Cloudflare adapter for SvelteKit, specifically targeting Cloudflare Workers. This is used for deploying to Cloudflare Workers. ```bash npx sv add sveltekit-adapter="adapter:cloudflare+cfTarget:workers" ``` -------------------------------- ### `defineAddon` — create a custom add-on Source: https://context7.com/sveltejs/cli/llms.txt Use `defineAddon` to build custom add-ons for SvelteKit. It allows defining an ID, description, options, setup logic, and the core `run` function for modifications. ```APIDOC ### `defineAddon` — create a custom add-on The primary API for building community or internal add-ons. `setup` declares dependencies and requirements; `run` performs the actual file and dependency changes. ```js import { transforms } from '@sveltejs/sv-utils'; import { defineAddon, defineAddonOptions } from 'sv'; export default defineAddon({ id: 'my-greeting', shortDescription: 'Adds a greeting to your SvelteKit home page', options: defineAddonOptions() .add('name', { question: 'What name should be greeted?', type: 'string', default: 'World' }) .add('style', { question: 'Use bold styling?', type: 'boolean', default: false }) .build(), setup: ({ dependsOn, unsupported, isKit }) => { if (!isKit) unsupported('Requires SvelteKit'); dependsOn('prettier'); // will run after prettier if both are selected }, run: ({ sv, options, cancel, directory }) => { // Install a runtime dependency sv.dependency('some-pkg', '^2.0.0'); // Install a dev dependency sv.devDependency('some-dev-pkg', '^1.0.0'); // Append a greeting paragraph to the root page sv.file( directory.kitRoutes + '/+page.svelte', transforms.svelte(({ ast, svelte }) => { const tag = options.style ? `Hello ${options.name}!` : `

Hello ${options.name}!

`; svelte.addFragment(ast, tag); }) ); // Add a DATABASE_URL variable to .env sv.file( '.env', transforms.text(({ content }) => content + '\nGREETING_ENABLED=true') ); }, nextSteps: ({ options }) => [ `Your greeting for "${options.name}" has been added`, "Run `npm run dev` to see it in action" ] }); ``` ``` -------------------------------- ### Create Project from Playground URL Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/10-sv-create.md Initialize a SvelteKit project by downloading files from a specified Svelte playground URL. This automatically sets up the project structure and detects external dependencies. ```bash npx sv create --from-playground="https://svelte.dev/playground/hello-world" ``` -------------------------------- ### Define typed add-on options with conditional visibility Source: https://context7.com/sveltejs/cli/llms.txt Use `defineAddonOptions` to build a typed configuration object for add-ons. This example shows how to define select, boolean, and multiselect options with conditional logic. ```js import { defineAddonOptions } from 'sv'; const options = defineAddonOptions() .add('database', { question: 'Which database?', type: 'select', default: 'postgresql', options: [ { value: 'postgresql', label: 'PostgreSQL' }, { value: 'mysql', label: 'MySQL' }, { value: 'sqlite', label: 'SQLite' } ] }) .add('docker', { question: 'Add a Docker Compose file?', type: 'boolean', default: false, // Only ask this question when the database is not sqlite condition: (opts) => opts.database !== 'sqlite' }) .add('tables', { question: 'Which demo tables to generate?', type: 'multiselect', default: ['users'], options: [ { value: 'users' }, { value: 'posts' }, { value: 'comments' } ] }) .build(); // Non-interactive usage: "database:postgresql+docker:yes" ``` -------------------------------- ### Run add-on locally using file protocol Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/99-community.md Use the `file:` protocol to link your local add-on to a test project for rapid iteration. This command should be run from within the test project directory. ```sh cd /path/to/test-project npx sv add file:../path/to/my-addon ``` -------------------------------- ### Install Drizzle ORM with PostgreSQL Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/05-drizzle.md Specify PostgreSQL as the database variant when installing the Drizzle ORM add-on. ```bash npx sv add drizzle="database:postgresql" ``` -------------------------------- ### Handle GET functions that return nothing Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_server/samples.md GET functions that explicitly return nothing (or implicitly return `undefined`) do not require migration. ```javascript export function GET() { return; } ``` ```javascript export function GET() { return; } ``` -------------------------------- ### Create a New Svelte Project with sv Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/cli/tests/snapshots/create-only/README.md Use this command to initialize a new Svelte project in your current directory. ```bash npx sv create my-app ``` -------------------------------- ### Add functionality to an existing SvelteKit project with sv add Source: https://context7.com/sveltejs/cli/llms.txt Installs and configures official or community add-ons into an existing Svelte(Kit) project. Options can be passed non-interactively using key:value pairs separated by '+'. Supports local add-ons and specifying versions. ```sh npx sv add ``` ```sh npx sv add eslint prettier vitest ``` ```sh npx sv add drizzle="database:postgresql+client:postgres.js+docker:yes" ``` ```sh npx sv add drizzle="database:sqlite+client:better-sqlite3" ``` ```sh npx sv add better-auth="demo:password,github" ``` ```sh npx sv add tailwindcss="plugins:typography" ``` ```sh npx sv add vitest="usages:unit,component" ``` ```sh npx sv add sveltekit-adapter="adapter:vercel" ``` ```sh npx sv add sveltekit-adapter="adapter:cloudflare+cfTarget:workers" ``` ```sh npx sv add mcp="ide:cursor,vscode+setup:local" ``` ```sh npx sv add @supacool ``` ```sh npx sv add @my-org/sv@1.2.3 ``` ```sh npx sv add file:../path/to/my-addon ``` ```sh npx sv add eslint @supacool ``` ```sh npx sv add --cwd ./packages/my-app eslint ``` ```sh npx sv add eslint --no-git-check --no-download-check ``` -------------------------------- ### Migrate arrow function GET to return JSON Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_server/samples.md Arrow function GET handlers can also be migrated to use the `json` helper for returning JSON objects. ```javascript export const GET = () => ({ body: { a: 1 } }); ``` ```javascript import { json } from '@sveltejs/kit'; export const GET = () => json({ a: 1 }); ``` -------------------------------- ### Migrate arrow function GET to load Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_page_server/samples.md Converts an arrow function `GET` RequestHandler to an arrow function `load` function. The `body` property is removed from the return object. ```javascript /** @type {import('./$types').RequestHandler} */ export const GET = () => ({ body: { a: 1 } }); ``` ```javascript /** @type {import('./$types').PageServerLoad} */ export const load = () => ({ a: 1 }); ``` -------------------------------- ### Migrate GET with status to load function Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_page_server/samples.md Simplifies a `GET` RequestHandler that explicitly returns a 200 status code to a `load` function. The status is implicitly handled by the framework. ```javascript export function GET() { return { status: 200, body: { a: 1 } }; } ``` ```javascript export function load() { return { a: 1 }; } ``` -------------------------------- ### Migrate GET in +layout-foo.server.js Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_page_server/samples.md Adapts a `GET` RequestHandler within a layout server file to the new `load` function signature. The `body` property is omitted from the return value. ```javascript /** @type {import('./$types').RequestHandler} */ export function GET() { return { body: { a: 1 } }; } ``` ```javascript /** @type {import('./$types').LayoutServerLoad.foo} */ export function load() { return { a: 1 }; } ``` -------------------------------- ### Migrate GET with external import to load function Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_page_server/samples.md Converts a JavaScript `GET` RequestHandler that imports external modules to a `load` function. The `body` property is removed from the return object. ```javascript import something from 'somewhere'; /** @type {import('./$types').RequestHandler} */ export function get() { return { body: { a: something } }; } ``` ```javascript import something from 'somewhere'; /** @type {import('./$types').PageServerLoad} */ export function load() { return { a: something }; } ``` -------------------------------- ### Run Svelte CLI with Deno Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/10-introduction/20-faq.md Use this command to create a new Svelte project with Deno. ```bash deno run npm:sv create ``` -------------------------------- ### Migrate GET to load function (TS) Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_page_server/samples.md Converts a TypeScript `GET` RequestHandler to a `load` function for server-side page data loading. The `body` property is removed from the return object. ```typescript import type { RequestHandler } from './$types'; export const GET: RequestHandler = () => { return { body: { a: 1 } }; } ``` ```typescript import type { PageServerLoad } from './$types'; export const load: PageServerLoad = () => { return { a: 1 }; } ``` -------------------------------- ### `create` — create a project programmatically Source: https://context7.com/sveltejs/cli/llms.txt Use the `create` function to programmatically generate new SvelteKit projects with specified templates and type checking options. ```APIDOC ## Programmatic API (`sv`) ### `create` — create a project programmatically ```js import { create } from 'sv'; // Create a minimal TypeScript SvelteKit app await create('./my-app', { name: 'my-app', template: 'minimal', types: 'typescript' }); // Create a demo app with JSDoc types await create('./demo-app', { name: 'demo-app', template: 'demo', types: 'checkjs' }); ``` ``` -------------------------------- ### Migrate GET to load function (JS) Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_page_server/samples.md Converts a JavaScript `GET` RequestHandler to a `load` function for server-side page data loading. The `body` property is removed from the return object. ```javascript /** @type {import('./$types').RequestHandler} */ export function GET() { return { body: { a: 1 } }; } ``` ```javascript /** @type {import('./$types').PageServerLoad} */ export function load() { return { a: 1 }; } ``` -------------------------------- ### Use a local add-on Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/20-sv-add.md Integrate a local add-on by providing its file path, useful for development or internal use. ```sh # Use a local add-on (for development or internal use) npx sv add file:../path/to/my-addon ``` -------------------------------- ### Handle unknown return values in GET functions Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_server/samples.md If a GET function returns an unknown value that cannot be automatically migrated, a migration task error is thrown. Manual intervention is required to convert it to a `Response` or `json` object. ```javascript export const GET = () => createResponse('text'); ``` ```javascript throw new Error("@migration task: Migrate this return statement (https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292701)"); export const GET = () => createResponse('text'); ``` -------------------------------- ### sv Testing API Surface Source: https://github.com/sveltejs/cli/blob/main/packages/sv/api-surface-testing.md Defines types and functions for testing Svelte projects, including project variants, creation options, global setup, fixtures, addon test cases, server preparation, and test setup creation. ```typescript type ProjectVariant = 'kit-js' | 'kit-ts' | 'vite-js' | 'vite-ts'; declare const variants: ProjectVariant[]; type CreateProject = (options: { testId: string; variant: ProjectVariant; clean?: boolean; }) => string; declare module 'vitest' { interface ProvidedContext { testDir: string; templatesDir: string; variants: ProjectVariant[]; } } declare function setupGlobal({ TEST_DIR, pre, post }: { TEST_DIR: string; pre?: () => Promise; post?: () => Promise; }): ({ provide }: TestProject) => Promise<() => Promise>; type Fixtures = { page: Page; cwd(addonTestCase: AddonTestCase): string; }; type AddonTestCase = { variant: ProjectVariant; kind: { type: string; options: OptionMap; }; }; type SetupTestOptions = { kinds: Array['kind']>; filter?: (addonTestCase: AddonTestCase) => boolean; browser?: boolean; preAdd?: (o: { addonTestCase: AddonTestCase; cwd: string }) => Promise | void; }; type PrepareServerOptions = { cwd: string; page: Page; buildCommand?: string; previewCommand?: string; }; type PrepareServerReturn = { url: string; close: () => Promise; }; declare function prepareServer({ cwd, page, buildCommand, previewCommand }: PrepareServerOptions): Promise; type PlaywrightContext = Pick; type VitestContext = Pick; declare function createSetupTest( vitest: VitestContext, playwright?: PlaywrightContext ): ( addons: Addons, options?: SetupTestOptions ) => { test: _$vitest.TestAPI; testCases: Array>; prepareServer: typeof prepareServer; }; export { AddonTestCase, CreateProject, Fixtures, PlaywrightContext, PrepareServerOptions, PrepareServerReturn, ProjectVariant, SetupTestOptions, VitestContext, createSetupTest, prepareServer, setupGlobal, variants }; ``` -------------------------------- ### Migrate complex GET return types Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_server/samples.md This snippet shows how to migrate GET functions returning different types of bodies, including streams and strings, to the new `Response` object or `json` helper. Unmigrated or complex cases may require manual intervention. ```javascript export function GET() { if (a) { return { body: { a: 1 } }; } else if (b) { return { body: new ReadableStream(), headers: { 'content-type': 'octasomething' } } } else if (c) { return { body: 'string', headers: { 'x-foo': 'bar' } } } } ``` ```javascript import { json } from '@sveltejs/kit'; export function GET() { if (a) { throw new Error("@migration task: Migrate this return statement (https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292701)"); // Suggestion (check for correctness before using): // return json(body); return { body }; } else if (b) { throw new Error("@migration task: Migrate this return statement (https://github.com/sveltejs/kit/discussions/5774#discussioncomment-3292701)"); // Suggestion (check for correctness before using): // return new Response(new ReadableStream(), { // headers: { // 'content-type': 'octasomething' // } // }); return { body: new ReadableStream(), headers: { 'content-type': 'octasomething' } } } else if (c) { return new Response('string', { headers: { 'x-foo': 'bar' } }) } } ``` -------------------------------- ### Recreate Project with All Addons Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/cli/tests/snapshots/create-with-all-addons/README.md Recreates a project with a specific template and a comprehensive set of addons, including linters, testing frameworks, UI enhancements, adapters, databases, authentication, markdown processing, and IDE integrations. Use this to ensure a consistent development environment. ```sh npx sv@0.0.0 create --template minimal --types ts --add prettier eslint vitest="usages:unit,component" playwright tailwindcss="plugins:typography,forms" sveltekit-adapter="adapter:node" drizzle="database:sqlite+sqlite:libsql" better-auth="demo:password,github" mdsvex paraglide="languageTags:en,es+demo:yes" mcp="ide:claude-code,cursor,gemini,opencode,vscode,other+setup:local" --no-install packages/sv/.test-output/cli/create-with-all-addons ``` -------------------------------- ### Development Scripts for Add-ons Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/cli/tests/snapshots/@my-org/sv/CONTRIBUTING.md Convenient npm scripts for developing add-ons. Use `demo-create` to set up a new project, `demo-add` to integrate your add-on, and `test` to run tests. ```sh npm run demo-create npm run demo-add npm run test ``` -------------------------------- ### PNPM Utilities Source: https://github.com/sveltejs/cli/blob/main/packages/sv-utils/api-surface.md Utilities for managing PNPM package installations, specifically related to build processes. ```APIDOC ## allowBuilds ### Description Allows builds for specified packages. ### Signature allowBuilds(...packages: string[]): TransformFn ## onlyBuiltDependencies ### Description (Deprecated) Use `allowBuilds` instead. Configures package installations to only include built dependencies. ### Signature onlyBuiltDependencies(...packages: string[]): TransformFn ``` -------------------------------- ### Run Formatter Source: https://github.com/sveltejs/cli/blob/main/CONTRIBUTING.md Format the code according to the project's style guidelines. This command should be run from the project's root directory. ```bash # from root of project pnpm format ``` -------------------------------- ### Run Svelte CLI with Bun Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/10-introduction/20-faq.md Use this command to create a new Svelte project with Bun. ```bash bunx sv create ``` -------------------------------- ### Add mdsvex to Svelte Project Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/30-add-ons/20-mdsvex.md Use this command to install and configure mdsvex in your Svelte project. ```sh npx sv add mdsvex ``` -------------------------------- ### Create a minimal TypeScript SvelteKit app programmatically Source: https://context7.com/sveltejs/cli/llms.txt Use the `create` function from the `sv` library to programmatically generate a new SvelteKit project with TypeScript and a minimal template. ```js import { create } from 'sv'; // Create a minimal TypeScript SvelteKit app await create('./my-app', { name: 'my-app', template: 'minimal', types: 'typescript' }); ``` -------------------------------- ### Get or Create Object Property Source: https://github.com/sveltejs/cli/blob/main/packages/sv-utils/api-surface.md Retrieves a property from an ESTree ObjectExpression, returning a fallback if it doesn't exist. ```typescript declare function property( node: estree.ObjectExpression, options: { name: string; fallback: T; } ): T; ``` -------------------------------- ### Run Svelte CLI with npm Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/10-introduction/20-faq.md Use this command to create a new Svelte project with npm. ```bash npx sv create ``` -------------------------------- ### Create a demo SvelteKit app with JSDoc types programmatically Source: https://context7.com/sveltejs/cli/llms.txt Programmatically create a SvelteKit demo application using the `create` function, specifying 'demo' template and 'checkjs' for JSDoc type checking. ```js import { create } from 'sv'; // Create a demo app with JSDoc types await create('./demo-app', { name: 'demo-app', template: 'demo', types: 'checkjs' }); ``` -------------------------------- ### FAILURE Record Example Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/30-sv-check.md Indicates a runtime error occurred during the check process, along with a brief message. ```text 1590680328921 FAILURE "Connection closed" ``` -------------------------------- ### Migrate function returning nothing Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_page_server/samples.md Converts a `GET` function that returns `undefined` to a `load` function. The behavior remains the same. ```javascript export function GET() { return; } ``` ```javascript export function load() { return; } ``` -------------------------------- ### create Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/50-api/10-sv.md Programmatically creates a new Svelte project in the specified directory with given configuration options. ```APIDOC ## create Programmatically create a new Svelte project. ```js import { create } from 'sv'; create('./my-app', { name: 'my-app', template: 'minimal', types: 'typescript' }); ``` ``` -------------------------------- ### Migrate GET to return a Response object Source: https://github.com/sveltejs/cli/blob/main/packages/migrate/migrations/routes/migrate_server/samples.md Functions that already return a `Response` object do not require migration for this aspect. ```javascript export const GET = () => new Response('text'); ``` ```javascript export const GET = () => new Response('text'); ``` -------------------------------- ### Run Svelte CLI with pnpm Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/10-introduction/20-faq.md Use this command to create a new Svelte project with pnpm. ```bash pnpm dlx sv create ``` -------------------------------- ### Get Object Property Node Source: https://github.com/sveltejs/cli/blob/main/packages/sv-utils/api-surface.md Retrieves the ESTree Property node for a given property name in an ObjectExpression, returning a fallback. ```typescript declare function propertyNode( node: estree.ObjectExpression, options: { name: string; fallback: T; } ): estree.Property; ``` -------------------------------- ### Build the Project Source: https://github.com/sveltejs/cli/blob/main/CONTRIBUTING.md Build all packages within the monorepo. The output will be placed in the /packages/PACKAGE/dist/ directory for each package. ```bash # from root of project pnpm build ``` -------------------------------- ### Add add-ons during project creation Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/20-sv-add.md Specify add-ons to be installed when creating a new Svelte project using `sv create`. ```sh # Also works when creating a new project directly npx sv create --add eslint @supacool ``` -------------------------------- ### Build with Watch Mode Source: https://github.com/sveltejs/cli/blob/main/CONTRIBUTING.md Build the project and enable watch mode for continuous development. Changes will trigger automatic rebuilds. ```bash pnpm dev ``` -------------------------------- ### Build Svelte Library for Publishing Source: https://github.com/sveltejs/cli/blob/main/packages/sv/src/create/shared/+library/README.md Package your library for distribution using 'npm pack'. This command creates a tarball of your library. ```sh npm pack ``` -------------------------------- ### COMPLETED Record Example Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/30-sv-check.md The final record summarizes the total number of files processed, errors, warnings, and files with problems. ```text 1590680326807 COMPLETED 20 FILES 21 ERRORS 1 WARNINGS 3 FILES_WITH_PROBLEMS ``` -------------------------------- ### Mix official and community add-ons Source: https://github.com/sveltejs/cli/blob/main/documentation/docs/20-commands/20-sv-add.md Combine official and community add-ons in a single `sv add` command execution. ```sh # Mix and match official and community add-ons npx sv add eslint @supacool ```