Recent updated version of `@types/node` is creating an error. The previous version was working fine

Viewed 974

I'm getting this error

error TS2694: Namespace 'NodeJS' has no exported member 'Global'.
4 interface CustomNodeJsGlobal extends NodeJS.Global 

While running this

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

// add prisma to the NodeJS global type
interface CustomNodeJsGlobal extends NodeJS.Global {
  prisma: PrismaClient;
}

// Prevent multiple instances of Prisma Client in development
declare const global: CustomNodeJsGlobal;

const prisma = global.prisma || new PrismaClient();

if (process.env.NODE_ENV === "development") global.prisma = prisma;

export default prisma;

IT'S THE PROBLEM WITH @types/node VERSION.
--> With "@types/node": "^15.4.0" (i don't remember precisely but it started from 15 (15.x.x))
--> The error is shown after updating to the latest version "@types/node": "^16.3.0"

What is the standard way to make it work with the latest version "@types/node": "^16.3.0", ?

1 Answers

As of node@16 the NodeJS.Global interface has been removed in favor of globalThis.

You can declare new global variable in a module file as:

declare global {
  var NEW_GLOBAL: string;
}

And in a non-module file (no top-level import/export) as:

declare var NEW_GLOBAL: string;

Important note: variable must be declared as var. let or const variables doesn't show up on globalThis.

So, your code may be rewritten as:

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

declare global {
  var prisma: PrismaClient;
}

const prisma = global.prisma || new PrismaClient();

if (process.env.NODE_ENV === "development") global.prisma = prisma;

export default prisma;
Related