sveltesveltekitsvelte-5

No longer able to import $lib files - ENOENT: no such file or directory


In my Svelte 5 / Sveltekit project, when I npm run build, I get the following error:

[vite:load-fallback] Could not load "path\to\project\frontend\src\lib/types/pocketbase": (imported by src/routes/add-games/+page.server.ts) ENOENT: no such file or directory , open 'path\to\project\frontend\src\lib\types\pocketbase'

my import looks like this:

import { SomeEnum } from '$lib/types/pocketbase';

The file in question is in frontend\src\lib\types\pocketbase.d.ts

I've tried creating new files that export a single functions in $lib with different names and exporting / importing them. No luck.

These imports from $lib were working before and what is even weirder is that there are pre-existing functions in files from $lib that still seem to be importing just fine.

It seems to me like this is some kind of bug, I've tried deleting svelte-kit folder and reloading npm packages but that didn't help either.

I notice one of the paths in the error has a mix of backward and forward slashes. Not sure if that could be the culprit or even why they are not normalised.

running npm run dev runs the app without errors.

Any help would be much appreciated.

My tsconfig:

{
    "extends": "./.svelte-kit/tsconfig.json",
    "compilerOptions": {
        "allowJs": true,
        "checkJs": true,
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "resolveJsonModule": true,
        "skipLibCheck": true,
        "sourceMap": true,
        "strict": true,
        "moduleResolution": "bundler"
    },
    "exclude": [
        "build",
        ".svelte-kit",
        "dist",
        "node_modules"
    ]

My viteconfig:

export default defineConfig({
    plugins: [sveltekit()],
    test: {
        include: ['src/**/*.{test,spec}.{js,ts}']
    }
});

svelte.config:

import adapter from '@sveltejs/adapter-auto';
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';

/** @type {import('@sveltejs/kit').Config} */
const config = {
    // Consult https://kit.svelte.dev/docs/integrations#preprocessors
    // for more information about preprocessors
    preprocess: vitePreprocess(),

    kit: {
        // adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.
        // If your environment is not supported, or you settled on a specific environment, switch out the adapter.
        // See https://kit.svelte.dev/docs/adapters for more information about adapters.
        adapter: adapter()
    }
};

export default config;

Solution

  • You can use alias attribute in svelte.config to manually setup your folder aliases

    for example:

    import adapter from '@sveltejs/adapter-netlify';
    import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
    
    /** @type {import('@sveltejs/kit').Config} */
    const config = {
      preprocess: vitePreprocess(),
      kit: {
        adapter: adapter(),
        alias: {
          $components: './src/components',
          $lib: './src/lib',
          $utils: './src/utils',
          $routes: './src/routes',
        },
      },
    };
    
    export default config;