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

use a default import so that the code is ESM compatible #99

Open
capaj opened this issue Feb 22, 2022 · 6 comments
Open

use a default import so that the code is ESM compatible #99

capaj opened this issue Feb 22, 2022 · 6 comments

Comments

@capaj
Copy link

capaj commented Feb 22, 2022

In one of the files generated I have:

import { Gender } from "@prisma/client"

export const _UserModel = z.object({
  id: z.string(),
  gender: z.nativeEnum(Gender),
})

this causes a problem when I use native ESM modules. Node.js is not able to import that as named export:

SyntaxError: Named export 'Gender' not found. The requested module '@prisma/client' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@prisma/client';
const { QuestionnaireScheduleType } = pkg;

it would be much better to generate the file like this:

import prisma from "@prisma/client"

export const _UserModel = z.object({
  id: z.string(),
  gender: z.nativeEnum(prisma.Gender),
})

this way it will work fine on commonJS and native ESM.

There is an issue in prisma repo which might explain the problem further:
prisma/prisma#4816

@CarterGrimmeisen
Copy link
Owner

Is this with "type": "module" or one of the command line flags for node?

@capaj
Copy link
Author

capaj commented Feb 23, 2022

I am using "type": "module"

capaj added a commit to MindMed/zod-prisma that referenced this issue Feb 23, 2022
@CarterGrimmeisen
Copy link
Owner

Okay, last question. I'm looking to try and test some of these changes myself. How are you running node on these typescript files? Are you just compiling them with tsc and then running them? Are you using ts-node? Thanks in advance for your help!

@capaj
Copy link
Author

capaj commented Feb 23, 2022

CarterGrimmeisen added a commit that referenced this issue Feb 23, 2022
…pt files for use with module: es2022 and type: module
@CarterGrimmeisen
Copy link
Owner

Could you test the fix from the fix/99 branch by placing this tarball (zod-prisma-v0.5.5.tar.gz) in your project directory and updating your package.json with the following?

"zod-prisma": "file:./zod-prisma-v0.5.5.tar.gz"

Thanks for your help!

@CarterGrimmeisen
Copy link
Owner

CarterGrimmeisen commented Feb 23, 2022

Whoops! You'll also have to add the following to your schema.prisma

generator zod {
  ...
  nodeEsModules = true // this line
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants