prisma: Next.js build: `RangeError: Maximum call stack size exceeded` in `typescript.js` when using TypeScript 5.1.3

Bug description

While using Prisma and Mongoose during the execution of “npm run build”, I’m receiving an error even with the simplest possible schema creation code.

> Build error occurred
RangeError: Maximum call stack size exceeded
    at couldContainTypeVariables (/node_modules/typescript/lib/typescript.js:65159:39)
    at inferFromTypes (/node_modules/typescript/lib/typescript.js:65453:14)
    at inferFromTypes (/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/node_modules/typescript/lib/typescript.js:65459:11) {
  type: 'RangeError'
}

After several hours of searching for the problem in a larger project, I have established several facts:

  • The problem only pertains to the latest version of TypeScript 5.1.3
  • The error only occurs if a PrismaClient query is first executed, and only then is the Mongoose schema defined.
  • In my case, the problem was caused by the Mongoose library, but it’s possible that someone may encounter such a problem with another library as well.

Has anyone encountered a similar problem and can suggest how to solve it?

How to reproduce

  1. npx create-next-app@latest
  2. Change into created folder
  3. npm install prisma --save-dev
  4. npm install @prisma/client mongoose
  5. npx prisma init --datasource-provider sqlite
  6. Add Test schema
    model Test {
      id    Int     @id @default(autoincrement())
      name  String
    }
    
  7. npx prisma generate
  8. Create lib/test.ts file with a content (it is the example code that illustrates the problem):
    import { PrismaClient } from '@prisma/client'
    import mongoose from 'mongoose';
    
    new PrismaClient().test.create({
      data: {
        name: "test",
      },
    });
    
    new mongoose.Schema({
      site: String
    });
    
  9. npm run build

Expected behavior

Error-free project build.

Prisma information

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

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

model Test {
  id    Int     @id @default(autoincrement())
  name  String
}
new PrismaClient().test.create({
  data: {
    name: "test",
  },
});

Environment & setup

  • OS: macOS
  • Database: SQLite
  • Node.js version: v18.16.0
  • TypeScript: 5.1.3
  • Next.js: 13.4.5

Prisma Version

prisma                  : 4.15.0
@prisma/client          : 4.15.0
Current platform        : darwin
Query Engine (Node-API) : libquery-engine 8fbc245156db7124f997f4cecdd8d1219e360944 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli 8fbc245156db7124f997f4cecdd8d1219e360944 (at node_modules/@prisma/engines/migration-engine-darwin)
Format Wasm             : @prisma/prisma-fmt-wasm 4.15.0-28.8fbc245156db7124f997f4cecdd8d1219e360944
Default Engines Hash    : 8fbc245156db7124f997f4cecdd8d1219e360944
Studio                  : 0.484.0

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Comments: 36 (11 by maintainers)

Commits related to this issue

Most upvoted comments

I had this issue as well while testing with Typescript 5.1.3. I just upgraded to Typescript v5.1.6 and my code seems to run without issues again. I suggest you give it a try and see if it helps fixing your problem too.

I upgraded to v5.1.6 (the latest as per the npm package) not 5.1.5. Version 5.1.5 does not work, 5.1.6 does work.

Installing tailwind@5.2.2 solved the issue for me.

Tailwind or Typescript?

Removing the Sharp package solved the problem for me. It seems that the Sharp package was causing conflicts or issues, and removing it resolved the issue.

@thanosoncode @phihungtf Optimally the TypeScript team releases a new patch version and you would switch to that instead of using the “dev” version.

Meanwhile, I think adding this in your package.json would unblock your projects

"overrides": {
  "typescript": "5.2.0-dev.20230606"
},

Or alternatively adding --force or --legacy-peer-deps to npm i like mentioned in the error message (Vercel docs to customize the command here).

We dug a bit deeper:

There are two related issues in the Typescript repo: https://github.com/microsoft/TypeScript/issues/54618 + https://github.com/microsoft/TypeScript/issues/54549 The second mentions that the 5.2.0-dev version might already fix this, and indeed: 5.2.0-dev.20230605 is still broken, but 5.2.0-dev.20230606 works again.

For me removing the package “sharp” from package.json resolved the issue.

A temporary workaround might be to downgrade to the previous typescript version: npm install typescript@5.0.4

PS C:\Users\Jan\Documents\throwaway\19715\my-app> npx next build
- info Loaded env from C:\Users\Jan\Documents\throwaway\19715\my-app\.env
- info Creating an optimized production build
- info Compiled successfully
- info Linting and checking validity of types
- info Collecting page data
- info Generating static pages (4/4)
- info Finalizing page optimization

Route (app)                                Size     First Load JS
┌ ○ /                                      4.78 kB          82 kB
└ ○ /favicon.ico                           0 B                0 B
+ First Load JS shared by all              77.3 kB
  ├ chunks/488-2398932694129a7b.js         24.9 kB
  ├ chunks/bce60fc1-49ee79ad31766ac6.js    50.5 kB
  ├ chunks/main-app-297b12279ce3dd6b.js    215 B
  └ chunks/webpack-737972036df45a17.js     1.64 kB

Route (pages)                              Size     First Load JS
─ ○ /404                                   181 B          74.7 kB
+ First Load JS shared by all              74.5 kB
  ├ chunks/framework-8883d1e9be70c3da.js   45.1 kB
  ├ chunks/main-66891548548be155.js        27.6 kB
  ├ chunks/pages/_app-b555d5e1eab47959.js  195 B
  └ chunks/webpack-737972036df45a17.js     1.64 kB

○  (Static)  automatically rendered as static HTML (uses no initial props)

Removing the Sharp package solved the problem for me. It seems that the Sharp package was causing conflicts or issues, and removing it resolved the issue.

confirmed!

RangeError: Maximum call stack size exceeded
19:07:47.104 | at RegExp.exec (<anonymous>)
19:07:47.105 | at create (/vercel/path0/node_modules/.pnpm/next@14.0.3_@babel+core@7.23.5_react-dom@18.2.0_react@18.2.0_sass@1.69.5/node_modules/next/dist/compiled/micromatch/index.js:15:18889)
19:07:47.105 | at create (/vercel/path0/node_modules/.pnpm/next@14.0.3_@babel+core@7.23.5_react-dom@18.2.0_react@18.2.0_sass@1.69.5/node_modules/next/dist/compiled/micromatch/index.js:15:18918)
19:07:47.105 | at parse.fastpaths (/vercel/path0/node_modules/.pnpm/next@14.0.3_@babel+core@7.23.5_react-dom@18.2.0_react@18.2.0_sass@1.69.5/node_modules/next/dist/compiled/micromatch/index.js:15:18997)
19:07:47.105 | at picomatch.makeRe (/vercel/path0/node_modules/.pnpm/next@14.0.3_@babel+core@7.23.5_react-dom@18.2.0_react@18.2.0_sass@1.69.5/node_modules/next/dist/compiled/micromatch/index.js:15:21635)
19:07:47.105 | at picomatch (/vercel/path0/node_modules/.pnpm/next@14.0.3_@babel+core@7.23.5_react-dom@18.2.0_react@18.2.0_sass@1.69.5/node_modules/next/dist/compiled/micromatch/index.js:15:19637)
19:07:47.105 | at /vercel/path0/node_modules/.pnpm/next@14.0.3_@babel+core@7.23.5_react-dom@18.2.0_react@18.2.0_sass@1.69.5/node_modules/next/dist/compiled/micromatch/index.js:15:19294
19:07:47.105 | at Array.map (<anonymous>)
19:07:47.106 | at picomatch (/vercel/path0/node_modules/.pnpm/next@14.0.3_@babel+core@7.23.5_react-dom@18.2.0_react@18.2.0_sass@1.69.5/node_modules/next/dist/compiled/micromatch/index.js:15:19286)
19:07:47.106 | at micromatch.isMatch (/vercel/path0/node_modules/.pnpm/next@14.0.3_@babel+core@7.23.5_react-dom@18.2.0_react@18.2.0_sass@1.69.5/node_modules/next/dist/compiled/micromatch/index.js:15:1090)
19:07:47.176 | ELIFECYCLE  Command failed with exit code 1.
19:07:47.198 | Error: Command "pnpm run build" exited with 1

This next13 drives me a lot of bugs

I’m observing the same error with "typescript": "^5.2.2" building my next.js app:

> Build error occurred
RangeError: Maximum call stack size exceeded
    at getTypeAtFlowBranchLabel (/Users/vick/Work/Carfeine/intelligent-inventory/node_modules/typescript/lib/typescript.js:67879:40)
    at getTypeAtFlowNode (/Users/vick/Work/Carfeine/intelligent-inventory/node_modules/typescript/lib/typescript.js:67683:50)
    at getTypeAtFlowBranchLabel (/Users/vick/Work/Carfeine/intelligent-inventory/node_modules/typescript/lib/typescript.js:67889:28)
    at getTypeAtFlowNode (/Users/vick/Work/Carfeine/intelligent-inventory/node_modules/typescript/lib/typescript.js:67683:50)
    at getTypeAtFlowBranchLabel (/Users/vick/Work/Carfeine/intelligent-inventory/node_modules/typescript/lib/typescript.js:67889:28)
    at getTypeAtFlowNode (/Users/vick/Work/Carfeine/intelligent-inventory/node_modules/typescript/lib/typescript.js:67683:50)
    at getTypeAtFlowBranchLabel (/Users/vick/Work/Carfeine/intelligent-inventory/node_modules/typescript/lib/typescript.js:67889:28)
    at getTypeAtFlowNode (/Users/vick/Work/Carfeine/intelligent-inventory/node_modules/typescript/lib/typescript.js:67683:50)
    at getTypeAtFlowCondition (/Users/vick/Work/Carfeine/intelligent-inventory/node_modules/typescript/lib/typescript.js:67831:26)
    at getTypeAtFlowNode (/Users/vick/Work/Carfeine/intelligent-inventory/node_modules/typescript/lib/typescript.js:67675:20) {
  type: 'RangeError'
}

To get it to build on Vercel, I had to make my build script set the stack-size to 5000.

@janpio Starting with typescript 5.1.3, it is no longer necessary to give @ts-expect-error to the server component. However, updating to 5.1.3 is causing the same error as this issue

Vercel Build log:

- info Linting and checking validity of types...
> Build error occurred
RangeError: Maximum call stack size exceeded
    at inferFromTypes (/vercel/path0/frontend/node_modules/.pnpm/typescript@5.1.3/node_modules/typescript/lib/typescript.js:65452:30)
    at inferFromTypes (/vercel/path0/frontend/node_modules/.pnpm/typescript@5.1.3/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/vercel/path0/frontend/node_modules/.pnpm/typescript@5.1.3/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/vercel/path0/frontend/node_modules/.pnpm/typescript@5.1.3/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/vercel/path0/frontend/node_modules/.pnpm/typescript@5.1.3/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/vercel/path0/frontend/node_modules/.pnpm/typescript@5.1.3/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/vercel/path0/frontend/node_modules/.pnpm/typescript@5.1.3/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/vercel/path0/frontend/node_modules/.pnpm/typescript@5.1.3/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/vercel/path0/frontend/node_modules/.pnpm/typescript@5.1.3/node_modules/typescript/lib/typescript.js:65459:11)
    at inferFromTypes (/vercel/path0/frontend/node_modules/.pnpm/typescript@5.1.3/node_modules/typescript/lib/typescript.js:65459:11) {
  type: 'RangeError'
}
 ELIFECYCLE  Command failed with exit code 1.
Error: Command "pnpm run build" exited with 1
BUILD_UTILS_SPAWN_1: Command "pnpm run build" exited with 1

Currently my project is downgrading a dependency to temporarily avoid the error.