dbushell / sveltekit-adapter-deno

🦕 SvelteKit Adapter for Deno and Deno Deploy
MIT License
51 stars 2 forks source link

Build: Deno is not defined #7

Closed ribizli closed 11 months ago

ribizli commented 1 year ago

I cannot use Deno API in +page.server.ts (probably also .js) files, because vite build ends with an error:

node:internal/event_target:1012
  process.nextTick(() => { throw err; });
                           ^
ReferenceError [Error]: Deno is not defined
    at file:///[...path removed for privacy...]/.svelte-kit/output/server/entries/pages/_page.server.ts.js:1:12
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Emitted 'error' event on Worker instance at:
    at [kOnErrorMessage] (node:internal/worker:300:10)
    at [kOnMessage] (node:internal/worker:311:37)
    at MessagePort.<anonymous> (node:internal/worker:212:57)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:737:20)
    at exports.emitMessage (node:internal/per_context/messageport:23:28)

Node.js v18.16.0

The error happens during or after "Analysing routes" step.

The +page.server.ts is something like this:

const kv = await Deno.openKv();

export async function load() {
    return {
        count: (await kv.get<number>(['count'])).value ?? 0
    };
}
dbushell commented 1 year ago

SvelteKit/vite uses Node for the build process so using Deno APIs isn't so easy.

This would technically work:

export async function load() {
  let count = 0;
  if ('Deno' in globalThis) {
    const kv = await Deno.openKv();
    count = (await kv.get<number>(['count'])).value ?? 0;
  }
  return {count};
}

I'm not sure I'd recommend this style for maintainability. At least create something like $lib/deno.js and keep all this logic in one place.

What I've done in production is have an entirely separate Deno server to handle data and provide API routes for SvelteKit.

ribizli commented 1 year ago

@dbushell thanks for the reply, I will go to this direction.

Maybe a last question: do you also experience a huge performance penalty when vite dev is started with deno?

dbushell commented 1 year ago

I've just been using Node for development

ribizli commented 1 year ago

ok, the whole idea to use KV came from your blog here: https://dbushell.com/2023/06/26/sveltekit-oauth-deno-deploy/.

but this is then just a theoretical possibility, not really a good practice, I see 😉

dbushell commented 1 year ago

I wouldn't say it's bad practice it's just that SvelteKit & vite are basically Node.js projects so using another runtime in development isn't easy.

This adapter is just for build/production runtime where Deno KV would be available. I don't have any solid advice on how to handle that during development!