iway1 / trpc-panel

MIT License
663 stars 50 forks source link

Cannot use when deployed in production on Vercel #44

Open rklubenspies opened 1 year ago

rklubenspies commented 1 year ago

When trying to deploy a side-project to Vercel (where I've intentionally enabled trpc-panel on production), I'm getting HTTP 500 when navigating to the panel's API route.

Disclaimer: Not sure if this is a monorepo-specific issue, but I'm using Nx on this project. As it relates to this issue, my appRouter is exported from a local package in my Nx repo. Based on the error below it doesn't appear this issue would be related to Nx.

Vercel Error log:

[GET] /api/panel
03:54:41:26
2023-02-15T08:54:41.290Z    3a0a746a-2f76-4d96-b6c2-9c82a211bb81    ERROR   Error: ENOENT: no such file or directory, open '/var/task/node_modules/trpc-panel/lib/react-app/bundle.js'
    at Object.openSync (node:fs:600:3)
    at Object.readFileSync (node:fs:468:35)
    at Object.<anonymous> (/var/task/node_modules/trpc-panel/lib/index.js:350:21)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at 959 (/var/task/apps/account/.next/server/pages/api/panel.js:125:45) {
  errno: -2,
  syscall: 'open',
  code: 'ENOENT',
  path: '/var/task/node_modules/trpc-panel/lib/react-app/bundle.js'
}
RequestId: 3a0a746a-2f76-4d96-b6c2-9c82a211bb81 Error: Runtime exited with error: exit status 1
Runtime.ExitError

Here's a minimal reproduction of my API route:

import type { NextApiRequest, NextApiResponse } from 'next'
import { renderTrpcPanel } from 'trpc-panel'
import { appRouter } from '@ee/account-api' // appRouter is exported from a local Nx monorepo package
import { env } from '@/env/server.mjs'

function getBaseUrl() {
  if (typeof window !== 'undefined') return '' // browser should use relative url
  if (process.env.VERCEL_URL) return `https://${process.env.VERCEL_URL}` // SSR should use vercel url
  return `http://localhost:${process.env.PORT ?? 3000}` // dev SSR should use localhost
}

export default async function handler(
  req: NextApiRequest,
  res: NextApiResponse,
) {
  // Ensure user is an admin in production only
  if (env.NODE_ENV !== 'development') {
    if (<LOGIC TO CHECK USER IS ADMIN>) {
      res.status(401).send('Unauthorized')
      return
    }
  }

  res.status(200).send(
    renderTrpcPanel(appRouter, {
      url: `${getBaseUrl()}/api/trpc`,
      transformer: 'superjson',
    }),
  )
}
msalahz commented 12 months ago

I'm getting the same error after deploying my next 13 app router application

matannahmani commented 11 months ago

any updates on this just ran into it too..

ephraimduncan commented 11 months ago

I am using turborepo in Next.js 13 and I'm getting the same thing

geoffreydhuyvetters commented 10 months ago

It's not specific to monorepos. I had the same issue on my deployment.