Skip to main content

#lib

还没翻译译自英文(svelte.dev)本页 4 段都还没译

When scaffolding a new SvelteKit project through the sv CLI, it automatically creates a #lib import alias for your src/lib directory, by adding the following to your package.json:

{
	"imports": {
		"#lib": "./src/lib/index.js",
		"#lib/*": "./src/lib/*"
	}
}

The # prefix leverages Node's built-in subpath imports feature, which reserves # for package-internal aliases. Vite and TypeScript both resolve these natively.

Legacy mode

src/lib/Component.svelte
A reusable component
src/routes/+page.svelte
<script>
	import Component from '#lib/Component.svelte';
</script>

<Component />