ignore packages only on production build

Hi, I'm trying to use Lucia Auth in my SvelteKit application. I want to use better-sqlite3 for local developement and d1 for live deployment. Is there any way to do that? I figured the local part, but when I try to deploy to cf pages, it fails because of the better-sqlite3 dependancey. Is there any way to only install better-sqlite3 in local and ignore it's installation while building for production? I mean, better-sqlite3 uses fs which is not available on cf.
1 Reply
mukhtharcm
mukhtharcm11mo ago
fixed it by moving imports into functions which are only called on local dev eg:
adapter: dev ? await getLocalDb() : getLiveDb(db),
adapter: dev ? await getLocalDb() : getLiveDb(db),
async function getLocalDb() {
const { default: sqlite } = await import('better-sqlite3');
// import betterSqlite3 from @lucia-auth/adapter-sqlite
const { betterSqlite3: betterSqlite3 } = await import('@lucia-auth/adapter-sqlite');

const localDb = sqlite('local-db/db.sqlite');
return betterSqlite3(localDb, {
user: 'user',
key: 'user_key',
session: 'user_session'
});
}
async function getLocalDb() {
const { default: sqlite } = await import('better-sqlite3');
// import betterSqlite3 from @lucia-auth/adapter-sqlite
const { betterSqlite3: betterSqlite3 } = await import('@lucia-auth/adapter-sqlite');

const localDb = sqlite('local-db/db.sqlite');
return betterSqlite3(localDb, {
user: 'user',
key: 'user_key',
session: 'user_session'
});
}