DallasHoff / sqlocal

SQLocal makes it easy to run SQLite3 in the browser, backed by the origin private file system.
https://sqlocal.dallashoffman.com
MIT License
212 stars 8 forks source link

Error during SvelteKit Server-side Rendering #10

Closed Feroks closed 7 months ago

Feroks commented 7 months ago

I am using latest SvelteKit version (2.0.6), which uses bundler moduleResolution. When i try to build or run vite dev on my project i am getting following error:

Cannot find module 'C:\sources\my-app\node_modules\sqlocal\dist\kysely\client' imported from C:\sources\my-app\node_modules\sqlocal\dist\kysely\index.js

I have created repository that reproduces issue when you run dev command. Unlike my actual project, build command seems to be working. Any ideas how to solve it?

DallasHoff commented 7 months ago

Since you are importing SQLocal in the component, you need to make sure that the component is not rendered on the server. SQLocal is only designed to run on the client. You can fix this by adding this to +layout.ts:

export const ssr = false;

Or if it better fits your use-case, you could alternatively use a dynamic import inside of onMount to import SQLocal or a lib file that imports it:

 const { SQLocalKysely } = await import('sqlocal/kysely');