resolvers: NextJs 12: Named export `set` not found
Describe the bug
I’ve upgraded to NextJS v12 which supports ES modules out of the box and now I’m getting the following error whenever I try to load any page that imports uses zodResolver
.
file:///node_modules/@hookform/resolvers/dist/resolvers.mjs:1
import{get as r,set as o}from"react-hook-form";var a=function(a,e){var f={};for(var t in a){var n=r(e,t);o(f,t,Object.assign(a[t],{ref:n&&n.ref}))}return f};export{a as toNestError};
error - unhandledRejection: SyntaxError: Named export 'set' not found. The requested module 'react-hook-form' 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 'react-hook-form';
const {get: r,set: o}from"react-hook-form";var a=function(a,e){var f={};for(var t in a){var n=r(e,t);o(f,t,Object.assign(a[t],{ref:n&&n.ref}))}return f};export{a: toNestError} = pkg;
To Reproduce
- Create a new NextJS project on v12
- Install
@hookform/resolvers
- Try to use any resolver (e.g. zod)
- See error
import { zodResolver } from '@hookform/resolvers/zod';
export const useZodForm = ({
schema,
...formConfig
}) => {
return useForm({
...formConfig,
resolver: zodResolver(schema),
});
};
Expected behavior Import is resolved.
Additional context
"@hookform/resolvers": "^2.8.2"
, "react-hook-form": "^7.18.0"
, "next": "^12.0.2"
.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 61
- Comments: 85 (22 by maintainers)
Commits related to this issue
- yupResolver in next 12 https://github.com/react-hook-form/resolvers/issues/271 — committed to takuji31/circle-manager by takuji31 3 years ago
- build(esm): Use explicit .mjs extension for ESM This ensures that Node always treats the ESM output as ESM, despite the package.json (implicit) type of commonjs. Bundlers that expect ESM, such as Nex... — committed to evocateur/react-hook-form by evocateur 3 years ago
- 🏗️ build(esm): Use explicit .mjs extension for ESM (#7244) This ensures that Node always treats the ESM output as ESM, despite the package.json (implicit) type of commonjs. Bundlers that expect ESM,... — committed to react-hook-form/react-hook-form by evocateur 3 years ago
- build(esm): Use explicit .mjs extension for ESM This ensures that Node always treats the ESM output as ESM, despite the package.json (implicit) type of commonjs. Bundlers that expect ESM, such as Nex... — committed to evocateur/react-hook-form by evocateur 3 years ago
- build(esm): Use explicit .mjs extension for ESM This ensures that Node always treats the ESM output as ESM, despite the package.json (implicit) type of commonjs. Bundlers that expect ESM, such as Nex... — committed to evocateur/react-hook-form by evocateur 3 years ago
- build(esm): Use explicit .mjs extension for ESM, with CJS interop (#7261) * build(esm): Use explicit .mjs extension for ESM This ensures that Node always treats the ESM output as ESM, despite the ... — committed to react-hook-form/react-hook-form by evocateur 3 years ago
- 🏛️ build(esm): Use explicit .mjs extension for ESM, with CJS interop (#7… (#7262) * build(esm): Use explicit .mjs extension for ESM, with CJS interop (#7261) * build(esm): Use explicit .mjs exten... — committed to react-hook-form/react-hook-form by bluebill1049 3 years ago
Hey all 👋🏻 I’m working on that issue, I’ll back to you soon as possible
@mrodrigues95 I faced the same issue just now. Fixed it temporarily by replacing
import { yupResolver } from '@hookform/resolvers/yup';
withimport { yupResolver } from '@hookform/resolvers/yup/dist/yup.umd';
. Maybe it’s going to work for you as well.Another fix would be
For me 7.20.0 with next 12.0.4 works fine 🎉
https://gitlab.com/yo/devparty/-/jobs/1799427151
I verified that https://github.com/react-hook-form/react-hook-form/pull/7261 works for NextJS 12.0.7, but I don’t have a Storybook or CRA project offhand to test the backward-compatibility.
Next.js 12.0.4 didn’t solve this issue either.
Same issue.
@Yhozen Solution Worked, Thank you 🎉
My versions :
hopefully, this is the last time i close this issue: https://github.com/react-hook-form/react-hook-form/releases/tag/v7.22.0 🙏
the above beta pass test for my with CRA and nextjs thank so much @evocateur life saver.
I also can confirm that
v7.20.2
in combination with nextv12.0.4
does not compile, butv.7.20.1
does. Just want to add that I added somewebpack
related settingsnext.config.js
. So in my case next does not compile with webpack.import { yupResolver } from '@hookform/resolvers/yup/dist/yup'
worked for meNextjs 12.0.3 was released, but it doesn’t resolve this issue, unfortunately.
How about Typescript? I tried @allwd 's suggestion, but it doesn’t work well.
Adding
experimental.esmExternals: false
to next.config file works for me.https://github.com/vercel/next.js/issues/30750#issuecomment-962198711
I can’t update to
v7.20.2
when usingv12.0.4
of Next.js, as I experience the same issue as the OP. The latest version ofreact-hook-form
I can use isv7.20.1
.downgrading to 12.0.1 fix the issue
Yes still this is blocking 😕
7.21.1
works well, but not7.21.2
😅v7.21.3-beta.0 works perfectly!
can we test this beta version?
react-hook-form@7.21.1-0
Using patch-package, this fixed it for me (with react-hook-form@7.21.0):
Basically, the import target needs to use the
.mjs
extension, preserving the defaulttype
of CommonJS for the overall package.@bluebill1049 I’m merely the longtime maintainer (presently a bit delinquent), not the author, of Lerna. I appreciate your responsiveness and effort as creator and maintainer of React Hook Form!
I believe the root cause for the CRA issue is that RHF uses the
import * as React from 'react'
form when it should be usingimport React from 'react'
. React itself is not ESM, it’s just a default export that Babel et al make-believe is a series of named exports, thus the error message.I’m surprised RHF isn’t using
esModuleInterop: true
in its tsconfig. Digging in locally to see if my hunch bears fruit.Hi @sgonv. For Typescript, you need to import resolver like
import { zodResolver } from '@hookform/resolvers/zod/dist/zod'
instead@hookform/resolvers/zod
. It works for me 😊oh wow, I didn’t check evocateur’s profile, he is the creator/author of Lerna. Respect 🎩
I confirm, that version 7.21.1 and Next 12.0.7 works well. 7.21.1. Thanks a lot.
@bluebill1049 and that is why I mentioned to you a few weeks ago that I gave up on ESM unless I have a strong reason …
The tools are still figuring out how to deal with it … especially those that are not Webpack since it was doing a lot of magical things.
Not sure what is the right resolution here … technically blame Webpack for some of the mess since it trained people to get used to “wrong things” (kind of misleading word but close enough, I cant find a better word).
Anyway … back to broken NextJS on my side … which is really annoying, I stopped using CRA in 2018 for a reason … (before I even adopted NextJS) that is all I will say.
closing: https://github.com/react-hook-form/react-hook-form/releases/tag/v7.21.1
@bluebill1049
react-hook-form@7.21.1-0
works withnext@12.0.7
, thanks!Seems working for me @bluebill1049 👏🏻
This is likely not an issue with
react-hook-form
as the module itself does provide ESM entry point: https://www.skypack.dev/view/react-hook-formSome news? I still get this issue, it is 20 day 😮
@azarmehri Oh, it worked for me. Thanks!
Many thanks @bluebill1049 & @evocateur 🙏
Had to lock the version to
7.21.1
for now at least@bluebill1049 that one is on CRA … they are way behind updating anything being honest on the topic … but I hear you … this Python 2v3 situation is really frustrating
This fixed seems to have fixed it for me. The fix from @allwd didn’t though.
react-hook-form
7.20.2 fix this same problem in Next 11.1.2. Thanks @bluebill1049 !@bluebill1049 yeah I checked the implementation and it seems like … but I hear you … I am still try-and-error with setups because we are under the mercy of how the underline tools originally load the components.
And, I think, unless the Node version supports the
package.json#exports
it wouldn’t work, which I am not sure if VercelNodeJS@14
supports it.@dsacramone I’m using “react-hook-form”: “^7.18.1”,
Thanks @justyn-clark as a temp workaround this works
import { zodResolver } from '@hookform/resolvers/zod/dist/zod'
@dsacramone No I think I am just importing it as usual:
import { yupResolver } from '@hookform/resolvers/yup';
That is what I am using to import it. The versions I have in my package.json are:
"@hookform/resolvers": "^2.8.3",
"next": "12.0.1",
So far it is working both locally and when I build/deploy it. I hope that is of some use !? 😃
@dsacramone Whilst not ideal, you can see my reply above in that dropping the version of Next down to 12.0.1 (specifically) in package.json and then re-running yarn (or npm) fixed all the build issues for me. As others have linked, this thread over at the Next repo seems to be dealing with the same (or similar) problem: https://github.com/vercel/next.js/issues/30750