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.

pnpm workspace + nextjs: ENOENT: no such file or directory, open '...\.next\server\pages\api\schema.prisma'

See original GitHub issue

Bug description

I have a pnpm workspace with three projects:

  • / -root project
  • /packages/database - Prisma installed as a dependency, re-export generated prisma client
  • /packages/next - A standard next.js typescript template, importing the database project

When I run the next project I get this error:

error - Error: ENOENT: no such file or directory, open 'C:\Users\hkhen\dev\pnpm-next\packages\next\.next\server\pages\api\schema.prisma'

I’ve tried a few variations: prisma and @prisma/client 3.12.0 and 3.13.0-dev.15, standard output directory and output = "../src/generated/prisma", all give the same error.

It sounds like this is kind of the same error as #10361, but it seems like next.js does something special handling of dirname. Regular node-js server apps works fine with the same setup.

How to reproduce

Terminal 1:

git clone git@github.com:ineentho/pnpm-nextjs-repro.git
cd pnpm-nextjs-repro
pnpm i # prisma generate runs as a postinstall hook
pnpm start # run next in dev-mode

Termnal 2:

curl localhost:3000/api/hello

The first terminal will log the error:

ready - started server on 0.0.0.0:3000, url: http://localhost:3000
wait  - compiling...
event - compiled client and server successfully in 643 ms (124 modules)
wait  - compiling /api/hello...
wait  - compiling...
warn  - ../database/src/generated/prisma/runtime/index.js
Module not found: Can't resolve 'encoding' in 'C:\Users\hkhen\dev\pnpm-nextjs-repro\packages\database\src\generated\prisma\runtime'
2022-04-16T15:35:50.611Z prisma:tryLoadEnv Environment variables not found at null
2022-04-16T15:35:50.612Z prisma:tryLoadEnv Environment variables not found at undefined
2022-04-16T15:35:50.612Z prisma:tryLoadEnv No Environment variables loaded
2022-04-16T15:35:50.631Z prisma:tryLoadEnv Environment variables not found at null
2022-04-16T15:35:50.631Z prisma:tryLoadEnv Environment variables not found at undefined
2022-04-16T15:35:50.631Z prisma:tryLoadEnv No Environment variables loaded
2022-04-16T15:35:50.631Z prisma:client dirname C:\Users\hkhen\dev\pnpm-nextjs-repro\packages\next\.next\server\pages\api
2022-04-16T15:35:50.631Z prisma:client relativePath ..\..\..\prisma
2022-04-16T15:35:50.632Z prisma:client cwd C:\Users\hkhen\dev\pnpm-nextjs-repro\packages\next\.next\server\pages\api
2022-04-16T15:35:50.634Z prisma:client clientVersion: 3.12.0
2022-04-16T15:35:50.634Z prisma:client clientEngineType: library
error - Error: ENOENT: no such file or directory, open 'C:\Users\hkhen\dev\pnpm-nextjs-repro\packages\next\.next\server\pages\api\schema.prisma'
warn  - ../database/src/generated/prisma/runtime/index.js
Module not found: Can't resolve 'encoding' in 'C:\Users\hkhen\dev\pnpm-nextjs-repro\packages\database\src\generated\prisma\runtime'
wait  - compiling /_error (client and server)...
wait  - compiling...
warn  - ../database/src/generated/prisma/runtime/index.js
Module not found: Can't resolve 'encoding' in 'C:\Users\hkhen\dev\pnpm-nextjs-repro\packages\database\src\generated\prisma\runtime'
2022-04-16T15:35:50.868Z compression no compression: not acceptable

Environment & setup

  • OS: Windows 10
  • Database: Postgres
  • Node.js version: v17.3.0

Prisma Version

prisma                  : 3.12.0
@prisma/client          : 3.12.0
Current platform        : windows
Query Engine (Node-API) : libquery-engine 22b822189f46ef0dc5c5b503368d1bee01213980 (at ..\..\node_modules\.pnpm\@prisma+engines@3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980\node_modules\@prisma\engines\query_engine-windows.dll.node)
Migration Engine        : migration-engine-cli 22b822189f46ef0dc5c5b503368d1bee01213980 (at ..\..\node_modules\.pnpm\@prisma+engines@3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980\node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine    : introspection-core 22b822189f46ef0dc5c5b503368d1bee01213980 (at ..\..\node_modules\.pnpm\@prisma+engines@3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980\node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary           : prisma-fmt 22b822189f46ef0dc5c5b503368d1bee01213980 (at ..\..\node_modules\.pnpm\@prisma+engines@3.12.0-37.22b822189f46ef0dc5c5b503368d1bee01213980\node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash    : 22b822189f46ef0dc5c5b503368d1bee01213980
Studio                  : 0.459.0

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jean-smaugcommented, Nov 11, 2022

Possible workaround Context, I’m using PNPM workspaces and my prisma config is in a packages that is used by a Nest.js app. The Nest.js app bundle the generated Prisma build with. webpack.

In post build of prisma generate, I run the following script and it’s doing the trick (for now). If regularDirname variable name is changed, the script will need an update

const fs = require("node:fs");

const generateLibPath = __dirname + "/../build/index.js";

const file = fs.readFileSync(generateLibPath);
const lines = file.toString().split("\n");

const newLines = lines.map((line) =>
  line.startsWith("const regularDirname")
    ? "const regularDirname = path.dirname(require.resolve('.'))"
    : line
);

const joinedNewLine = newLines.join("\n");

fs.writeFileSync(generateLibPath, joinedNewLine);
0reactions
lord007tncommented, Dec 23, 2022

I faced the same problem for the past 2 days and I make it work and i don’t know why but here is the code of my singleton PrismaClient.

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

let prisma: PrismaClient;
declare module globalThis {
	// eslint-disable-next-line no-var
	let prisma: PrismaClient | undefined;
}

if (process.env.NODE_ENV === "production") {
	prisma = new PrismaClient({
		errorFormat: "minimal",
	});
} else {
	globalThis.prisma =
		globalThis.prisma ||
		new PrismaClient({
			errorFormat: "pretty",
		});
	prisma = globalThis.prisma;
}
export * from "./zod";
export * from "@prisma/client"; 

the previous singleton PrismaClient

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

declare global {
	// eslint-disable-next-line no-var
	var prisma: PrismaClient | undefined;
}

export const prisma =
	globalThis.prisma ||
	new PrismaClient({
		log: process.env.NODE_ENV === "development" ? ["error", "warn"] : ["error"],
	});

if (process.env.NODE_ENV !== "production") {
	globalThis.prisma = prisma;
}
export default prisma;
export * from "./zod";
export * from "@prisma/client";

Environment

  • Windows: 11
  • Node 16.15.1
  • Db: Mongodb
  • PNPM 7.14.0

Could anyone explain why it worked ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Next.js + pnpm + dgraph-js-http v20.07.2 = doesn't work
So, I recently tried to integrate my Next.js app with the recently ... internal/fs/utils.js:308 throw err; ^ Error: ENOENT: no such file or...
Read more >
next.js ENOENT trying to open schema.prisma - bytemeta
In a monorepo, when exporting PrismaClient from one package and consuming it in a next.js api route, I get error - Error: ENOENT:...
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