zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

Generated "enhance" function not found. Please run zenstack generate first.

Open Izag-Larm opened this issue 10 months ago • 22 comments

Description and expected behavior I started the development of a Nuxt application and after defining my zenstack schema, I wrote an API route for the server to retrieve a file entity from the database. But I get an error after having at the execution level of enhance(prisma). However I have already run npx zenstack generate and npx prisma db push.

Screenshots zmodel

Image

Image

global prisma client

Image

route api

Image

Environment (please complete the following information):

  • ZenStack version: 2.11.5
  • Prisma version: 6.3.x
  • Database type: Postgresql

Additional context I running nuxt with compatibility version 4

Izag-Larm avatar Feb 08 '25 13:02 Izag-Larm

Hi @Izag-Larm , just to confirm, do you have any regex validation patterns in the ZModel? We recently discovered an issue that invalid regex patterns can cause the "enhance function not found" error.

ymc9 avatar Feb 23 '25 23:02 ymc9

@ymc9 I am also having this issue. I'm using regex in the zmodel, but the regex appears fine to me, and the same error happens when I remove the regex.

color       String               @default('#787878') @regex('^#(?:[0-9a-fA-F]{3}){1,2}$')

Interestingly, the error only happens when I run my app with tsx. If I build it and run it with node, it works fine. Additionally, the error does not appear in my Next.js app at all - it only appears in my express app.

I know you can't do much without a repro, so I'll see if I can set one up and open another issue if need be.

sanny-io avatar Feb 24 '25 05:02 sanny-io

Hi @Izag-Larm , just to confirm, do you have any regex validation patterns in the ZModel? We recently discovered an issue that invalid regex patterns can cause the "enhance function not found" error.

I don't have any regex validation pattern in my zmodel.

Izag-Larm avatar Feb 26 '25 16:02 Izag-Larm

I tried to rebuild my zmodel piece by piece, trying to identify each time what could cause the problem. And I get the error when I try to apply gte() validation on a field of one type.

Image

Izag-Larm avatar Feb 26 '25 16:02 Izag-Larm

I am having the same issue in [email protected].

All works well in dev but once built, the following happens: [nuxt] [request error] [unhandled] [500] Generated "enhance" function not found. Please run zenstack generate first.

Somehow, when I downgrade from node@22 to node@20, it works perfectly fine.

Kylyi avatar Mar 11 '25 16:03 Kylyi

Thanks for the comments. Does anyone have a sharable repro project?

ymc9 avatar Mar 11 '25 16:03 ymc9

Yup, https://github.com/Kylyi/nuxt-zenstack.

Just make sure to use node@22. It should work in dev mode.

Kylyi avatar Mar 11 '25 16:03 Kylyi

This is actually starting to be a bigger issue as it somehow breaks in docker as well in some cases, even when using node@20. WIll ty to prepare another repro.

Kylyi avatar Mar 14 '25 11:03 Kylyi

Had same issue with zenstack v2.12.1 node v23 ando v22. Using node v18 fixed it.

alfredosepulveda avatar Mar 14 '25 16:03 alfredosepulveda

It seems like it's dependent on the Node specific version heavily.

Using the following Dockerfile works: Image

while the following results in the [nuxt] [request error] [unhandled] [500] Generated "enhance" function not found. Please run zenstack generate first. issue.

Image

Kylyi avatar Mar 14 '25 20:03 Kylyi

I just encountered this issue going from node 20.18.3 to 20.19.0. Using the flag --no-experimental-require-module fixes it.

$ pnpm tsx --version
tsx v4.19.3
node v20.19.0
$ pnpm tsx --no-experimental-require-module ./scripts/myscript.ts
...
$ pnpm tsx ./scripts/myscript.ts
...
        throw new Error('Generated "enhance" function not found. Please run `zenstack generate` first.');
              ^
...

sslotnick avatar Mar 14 '25 21:03 sslotnick

I made a few tries with node 20.19.0 as @sslotnick indicated:

  • tsx ran into the issue, @sslotnick 's workaround worked for me
  • Nuxt ran into the issue as @Kylyi reported
  • ts-node worked fine, compiling with tsc and then running directly with node worked fine
  • Next.js worked fine

So it seems later node versions broke something related loading ESM with require, however the issue only happens with certain bundler (nuxt) and nodejs loaders (tsx) ...

I don't have further insight into the issue so far, and please use @sslotnick 's workaround for now.

ymc9 avatar Mar 20 '25 23:03 ymc9

I'm a little confused about the workaround from @sslotnick Where exactly does --no-experimental-require-module need to be added?

For nuxt, the application is started with node .output/server/index.mjs after a build which causes the issue.

genu avatar May 31 '25 20:05 genu

I'm also having this issue with a sveltekit project when upgrading from 2.14.2 to 2.15. Issue persists even when using node 18. I've removed regex from the zmodel to rule that out as an issue

bbozzay avatar Jun 04 '25 20:06 bbozzay

I don't think this is related necessarily to using @regex. Issue still persists on my end.

genu avatar Jun 05 '25 13:06 genu

I'm also having this issue with a sveltekit project when upgrading from 2.14.2 to 2.15. Issue persists even when using node 18. I've removed regex from the zmodel to rule that out as an issue

What sveltekit version are you on @bbozzay ?

ymc9 avatar Jun 05 '25 17:06 ymc9

@ymc9 Thanks for responding. Here are some relevant dependencies:

"@sveltejs/kit": "2.21.0",
"prisma": "6.7.0",
"@prisma/client": "6.7.0"

Also, I'm using pnpm and turborepo

bbozzay avatar Jun 05 '25 21:06 bbozzay

I found problem in "https://github.com/ljharb/async-function/blob/main/require.mjs" file If I comment export { getAsyncFunction as 'module.exports' }; part in file or remove "module-sync": "./require.mjs" from package json (library - async-function) - tsx works

Should you try it?

P.S. But I don't know if this will break the async-function library

DoctorFTB avatar Jun 17 '25 22:06 DoctorFTB

@ymc9 Thanks for responding. Here are some relevant dependencies:

"@sveltejs/kit": "2.21.0",
"prisma": "6.7.0",
"@prisma/client": "6.7.0"

Also, I'm using pnpm and turborepo

Hi @bbozzay , I just tried with a starter sveltekit project but couldn't reproduce the issue:

@sveltejs/kit: 2.22.0 node: v20.10.0

Have you checked if you're possibly accidentally calling enhance from client-side code? If not, do you have a sharable repro?

ymc9 avatar Jun 20 '25 08:06 ymc9

I found problem in "https://github.com/ljharb/async-function/blob/main/require.mjs" file If I comment export { getAsyncFunction as 'module.exports' }; part in file or remove "module-sync": "./require.mjs" from package json (library - async-function) - tsx works

Should you try it?

P.S. But I don't know if this will break the async-function library

Right, I remember seeing it in the error stack. Not sure how it's involved into the build though.

ymc9 avatar Jun 20 '25 08:06 ymc9

I'm a little confused about the workaround from @sslotnick Where exactly does --no-experimental-require-module need to be added?

For nuxt, the application is started with node .output/server/index.mjs after a build which causes the issue.

I didn't try but I believe you can use "NODE_OPTIONS" env var to pass the option.

ymc9 avatar Jun 20 '25 08:06 ymc9

I'm a little confused about the workaround from @sslotnick Where exactly does --no-experimental-require-module need to be added?

For nuxt, the application is started with node .output/server/index.mjs after a build which causes the issue.

I didn't try but I believe you can use "NODE_OPTIONS" env var to pass the option.

Thanks @ymc9

Indeed this solves the issue in Nuxt.

For anyone using nuxt, updating the start script like so solves the issue:

"start": "NODE_OPTIONS='--no-experimental-require-module' node .output/server/index.mjs"

genu avatar Jun 21 '25 16:06 genu