Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finding schema.prisma without __dirname #14484

Open
revmischa opened this issue Jul 25, 2022 · 6 comments
Open

Finding schema.prisma without __dirname #14484

revmischa opened this issue Jul 25, 2022 · 6 comments
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: deployment/aws-lambda topic: __dirname topic: esm topic: schema file

Comments

@revmischa
Copy link

Bug description

From slack: https://prisma.slack.com/archives/CCWDULGUW/p1657602998648169

I have an issue with creating lambda function bundles with prisma. I'm using ESM and __dirname is not defined, but used in the prisma client to find the schema.prisma file as best I can tell.

I can hack around this by creating an ESM shim for dealing with legacy CJS code like the prisma client. This defines __dirname using import.meta. See evanw/esbuild#2067 (comment) for details.

However I can't tell prisma where my bundle path is since it assumes my schema.prisma is in __dirname. I can work around this by hardcoding __dirname in my shim to /var/task and just always copying the schema.prisma file in there. It doesn't seem like the best solution.

How to reproduce

  1. Create lambda function that runs as ESM (output a .mjs bundle or set type:module in package.json of function bundle)
  2. Import @prisma/client

Should get an error

  errorType: 'ReferenceError',
  errorMessage: '__dirname is not defined in ES module scope\n' +
    `This file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.`,
  stack: [
    'ReferenceError: __dirname is not defined in ES module scope',
    `This file is being treated as an ES module because it has a '.js' file extension and '/var/task/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.`,
    '    at node_modules/.prisma/client/index.js (file:///var/task/packages/infra/lib/lambdas/cognito/preTokenGeneration.js:91:53)',
    '    at __require2 (file:///var/task/packages/infra/lib/lambdas/cognito/preTokenGeneration.js:20:50)',
    '    at node_modules/@prisma/client/index.js (file:///var/task/packages/infra/lib/lambdas/cognito/preTokenGeneration.js:1472:10)',
    '    at __require2 (file:///var/task/packages/infra/lib/lambdas/cognito/preTokenGeneration.js:20:50)',
    '    at file:///var/task/packages/infra/lib/lambdas/cognito/preTokenGeneration.js:75914:29',
    '    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)',
    '    at async Promise.all (index 0)',
    '    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)',
    '    at async _tryAwaitImport (file:///var/runtime/index.mjs:660:16)',
    '    at async _tryRequire (file:///var/runtime/index.mjs:709:37)'

Expected behavior

What would be nice (from best to least best):

  • Not to have to include the schema.prisma file in my deployment
  • For prisma to generate runtime code that compatible with ESM (i.e. not use __dirname)
  • To have an env var to tell prisma where to find the schema.prisma file

Prisma information

4.0.0

Environment & setup

  • OS:
  • Database:
  • Node.js version:
    Node 16 lambda

Prisma Version

prisma                  : 4.0.0
@prisma/client          : 4.0.0
Current platform        : darwin
Query Engine (Node-API) : libquery-engine da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/libquery_engine-darwin.dylib.node)
Migration Engine        : migration-engine-cli da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine    : introspection-core da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary           : prisma-fmt da41d2bb3406da22087b849f0e911199ba4fbf11 (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash    : da41d2bb3406da22087b849f0e911199ba4fbf11
Studio                  : 0.465.0
@revmischa revmischa added the kind/bug A reported bug. label Jul 25, 2022
@aqrln aqrln added domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. topic: deployment/aws-lambda topic: esm topic: schema file process/candidate bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jul 26, 2022
@matthewmueller
Copy link
Contributor

I believe this will be solved by #5030.

@janpio janpio changed the title Finding schema.prisma without __dirname Finding schema.prisma without __dirname Dec 7, 2022
@maietta
Copy link

maietta commented Aug 31, 2023

It broke again. Not sure why.

@hahagu
Copy link

hahagu commented Apr 9, 2024

@maietta Have you been able to work around this by any chance?

@maietta
Copy link

maietta commented Apr 9, 2024

@maietta Have you been able to work around this by any chance?

I moved to Drizzle ORM, so I have no idea.

@hahagu
Copy link

hahagu commented Apr 9, 2024

@maietta Have you been able to work around this by any chance?

I moved to Drizzle ORM, so I have no idea.

Oh, okay :/

Thank you for the prompt response!

@runlong-yao
Copy link

Drizzle ORM

yes, another choice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. domain/client Issue in the "Client" domain: Prisma Client, Prisma Studio etc. kind/bug A reported bug. topic: deployment/aws-lambda topic: __dirname topic: esm topic: schema file
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants