Generated "enhance" function not found. Please run zenstack generate first.
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
global prisma client
route api
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
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 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.
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.
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.
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.
Thanks for the comments. Does anyone have a sharable repro project?
Yup, https://github.com/Kylyi/nuxt-zenstack.
Just make sure to use node@22.
It should work in dev mode.
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.
Had same issue with zenstack v2.12.1 node v23 ando v22. Using node v18 fixed it.
It seems like it's dependent on the Node specific version heavily.
Using the following Dockerfile works:
while the following results in the [nuxt] [request error] [unhandled] [500] Generated "enhance" function not found. Please run zenstack generate first. issue.
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.');
^
...
I made a few tries with node 20.19.0 as @sslotnick indicated:
tsxran into the issue, @sslotnick 's workaround worked for me- Nuxt ran into the issue as @Kylyi reported
ts-nodeworked fine, compiling withtscand then running directly withnodeworked 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.
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'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
I don't think this is related necessarily to using @regex. Issue still persists on my end.
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 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
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
@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?
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 worksShould you try it?
P.S. But I don't know if this will break the
async-functionlibrary
Right, I remember seeing it in the error stack. Not sure how it's involved into the build though.
I'm a little confused about the workaround from @sslotnick Where exactly does
--no-experimental-require-moduleneed to be added?For nuxt, the application is started with
node .output/server/index.mjsafter a build which causes the issue.
I didn't try but I believe you can use "NODE_OPTIONS" env var to pass the option.
I'm a little confused about the workaround from @sslotnick Where exactly does
--no-experimental-require-moduleneed to be added?For nuxt, the application is started with
node .output/server/index.mjsafter 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"