icflorescu / trpc-sveltekit-example

A sample SvelteKit application built to illustrate the usage of ✨ trpc-sveltekit.
https://icflorescu.github.io/trpc-sveltekit
ISC License
135 stars 19 forks source link

"npm run build" throws superjson.registerCustom is not a function #3

Closed atresnjo closed 2 years ago

atresnjo commented 2 years ago

Hey, thanks for the great work on setting everything up! Wanted to give tRPC with SvelteKit a try, but the example doesn't build. It works fine when ran locally, but the build itself fails.

image

Thanks! 💯

thenbe commented 2 years ago

Does your svelte.config.js have the following? Notably, adding superjson to the ssr.noExternal array in production.

  kit: {
    adapter: adapter(),
    vite:
      process.env.NODE_ENV === 'production'
        ? {
            ssr: {
              noExternal: ['superjson']
            }
          }
        : undefined
  }

From this section of the docs.

atresnjo commented 2 years ago

Yeah. I'm using this repo. 👍

image

thenbe commented 2 years ago

The reason I asked is because when I comment out the enitre vite property, I get the exact same issue you're getting in the screenshot. To eliminate if your original issue is caused by this, try forcing the condition to true and see if it builds:

  kit: {
    adapter: adapter(),
    vite:
-     process.env.NODE_ENV === 'production'
+     true || process.env.NODE_ENV === 'production'
        ? {
            ssr: {
              noExternal: ['superjson']
            }
          }
        : undefined
  }
atresnjo commented 2 years ago

That worked, thank you very much. Obviously, I would need to set NODE_ENV to production, for it to work. Duh me, thanks again!

icflorescu commented 2 years ago

Thanks for clearing things out, @ambiguous48!

It's in the trpc-sveltekit docs: https://github.com/icflorescu/trpc-sveltekit#configure-superjson-to-correctly-handle-decimaljs--prismadecimal

To all: do you have any suggestion on how to make this clearer?

atresnjo commented 2 years ago

The issue wasn't missing documentation, in my opinion, just a slight oversight not seeing that the NODE_ENV environment variable is not being set anywhere. Maybe for this example just have the build script set the environment variable?

"build": "cross-env NODE_ENV=production && svelte-kit build",
icflorescu commented 2 years ago

The issue wasn't missing documentation, in my opinion, just a slight oversight not seeing that the NODE_ENV environment variable is not being set anywhere. Maybe for this example just have the build script set the environment variable?

"build": "cross-env NODE_ENV=production && svelte-kit build",

I don't know, is it really necessary? I was under the impression that NODE_ENV was automatically set to production when running svelte-kit build...

atresnjo commented 2 years ago

The issue wasn't missing documentation, in my opinion, just a slight oversight not seeing that the NODE_ENV environment variable is not being set anywhere. Maybe for this example just have the build script set the environment variable?

"build": "cross-env NODE_ENV=production && svelte-kit build",

I don't know, is it really necessary? I was under the impression that NODE_ENV was automatically set to production when running svelte-kit build...

Yep, I thought the same. My env could be somehow broken, at least it didn't set it for me.