8

I am following this guide https://vercel.com/guides/nextjs-prisma-postgres to create a full stack app. Typescript is throwing an error in this snippet of code:

import { PrismaClient } from '@prisma/client';
let prisma: PrismaClient;

if (process.env.NODE_ENV === 'production') {
  prisma = new PrismaClient();
} else {
  if (!global.prisma) {
    global.prisma = new PrismaClient();
  }
  prisma = global.prisma;
}

export default prisma;

TypeScript is throwing a ts7017 on the global.prisma:

Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.

Can someone help me understand this and how to fix? I set 'strict' to false in the tsconfig for the meantime and that supressed the issue for the meantime, though I'm sure having it off defeats the purpose of TS.

3
  • Do you know how global.prisma is declared? If you don't have access to its declaration, have you tried casting it with global.prisma as PrismaClient? Commented Nov 5, 2021 at 9:04
  • I would just not use the global.. It's not even clear why there doing that in the first place.. strange!!! They even import later. import prisma from '../lib/prisma'; Commented Nov 5, 2021 at 9:17
  • 1
    Nikolas from the Prisma team here! We recommend the instantiation of PrismaClient in this way because otherwise the DB connection limit gets exhausted in development due to Next.js' hot reloading. Here's our docs page about this: prisma.io/docs/support/help-articles/… I'm checking back with some of our TypeScript engineers to see how this issue can be solved. Commented Nov 5, 2021 at 9:26

3 Answers 3

20

I could reproduce the same error with strict mode to true and @types/node package version 16

Update: see the other answer recommended from the docs here https://stackoverflow.com/a/69851359/1345244

This should work:

declare global {
  var prisma: PrismaClient; // This must be a `var` and not a `let / const`
}

import { PrismaClient } from "@prisma/client";
let prisma: PrismaClient;

if (process.env.NODE_ENV === "production") {
  prisma = new PrismaClient();
} else {
  if (!global.prisma) {
    global.prisma = new PrismaClient();
  }
  prisma = global.prisma;
}

export default prisma;
Sign up to request clarification or add additional context in comments.

1 Comment

i was trying the same but i was adding const instead of let. i am confused why it didnt worked with const?
5

According to the docs you need to declare the variable global first:

import { PrismaClient } from '@prisma/client'

declare global {
  var prisma: PrismaClient | undefined
}

export const prisma =
  global.prisma ||
  new PrismaClient({
    log: ['query'],
  });

if (process.env.NODE_ENV !== 'production') global.prisma = prisma;

You can also have a separate file globals.d.ts with the declaration in it.

3 Comments

Can you explain a little more the purpose of writing the | undefined ?
The pipe in this case is used to separate the different types the variable prisma can potentially be. By adding ‘| undefined’ typescript will warn if the variable is used without checking if it’s defined.
thanks for the clarification and helping me understand :)
0

I tried this by creating a separate globals.d.ts file with the declaration in it, and it worked.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.