question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose types to the frontend for Next.js

See original GitHub issue

Bug description

I am playing around with prisma and getting that async_hooks error and its a rather weird thing. Let me explain: If i have this:

import { prisma } from "../utils/prismaClient";

import { startingForm } from "../utils/startingForm";

Nothing breaks. here, prisma is jsut a new PrismaClient(). Nothing else. In this application I am just using prisma for the getStaticProps of Next.js

However, if I add a index.js to the utils folder and just do:

import { prisma, startingForm } from '../utils"

I get the error again. Is there a reason for this?

You can take a look at the repo here:

https://github.com/itstheandre/build-cv-messing-around

Not a lot of code written. Just a little small thing I was messing around to build a resume and the async_hooks issue came up again. Now I know I am not using prisma on the frontend per se. I am only using it in the api of Next.js. Any idea of what might be happening?

How to reproduce

Expected behavior

Prisma information

Prisma Schema

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model Experience {
  id          Int     @default(autoincrement()) @id
  name        String
  company     String?
  personal    Boolean @default(false)
  from        String?
  to          String?
  description String
  website     String?

}

Prisma Query -> Literally the only query I am doing

const experiences = await prisma.experience.findMany({})

Environment & setup

- OS: Mac OS
- Database: PostgreSQL
- Node.js version: 12.16.3
- Prisma version:  2.4.1

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:26 (12 by maintainers)

github_iconTop GitHub Comments

7reactions
flybayercommented, Jan 14, 2021

A way to get Types in frontend without using prisma():

import {PrismaClient} from "@prisma/client"
export * from "@prisma/client"

const ProxyPrisma = new Proxy(PrismaClient, {
  construct(target, args) {
    if (typeof window !== "undefined") return {}
    globalThis["db"] = globalThis["db"] || new target(...args)
    return globalThis["db"]
  },
})

const prisma = new ProxyPrisma()

export default prisma
7reactions
stefnnncommented, Sep 13, 2020

I hit this bug as well. Digging deeper for me the issue was, that I use generated types from @prisma/client quite a lot in the frontend code, which works well enough for type checking. However, when I use an Enum in the frontend code (generated from my prisma schema), then the async_hooks error from above comes up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Micro-frontend with React and Next.js - LogRocket Blog
In this article, we'll cover the basics of what a micro-frontend is, its advantages, and how to implement it using Next.js.
Read more >
Basic Features: Environment Variables - Next.js
Use .env.local to load environment variables; Expose environment variables to the browser ... All types of Environment Variables should be configured there.
Read more >
Advanced Features: Custom Server - Next.js
A custom Next.js server allows you to start a server 100% programmatically in order to use custom server patterns. Most of the time,...
Read more >
API Routes: Introduction - Next.js
API routes provide a solution to build your API with Next.js. Any file inside the folder pages/api is mapped to /api/* and will...
Read more >
Basic Features: TypeScript - Next.js
Next.js supports TypeScript by default and has built-in types for pages and the API. You can get started with TypeScript in Next.js here....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found