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

NextJs 12: Named export set not found #271

Closed
mrodrigues95 opened this issue Oct 31, 2021 · 85 comments
Closed

NextJs 12: Named export set not found #271

mrodrigues95 opened this issue Oct 31, 2021 · 85 comments
Labels
dependencies Pull requests that update a dependency file help wanted Extra attention is needed

Comments

@mrodrigues95
Copy link

mrodrigues95 commented Oct 31, 2021

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

  1. Create a new NextJS project on v12
  2. Install @hookform/resolvers
  3. Try to use any resolver (e.g. zod)
  4. 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".

@allwd
Copy link

allwd commented Oct 31, 2021

@mrodrigues95
I faced the same issue just now. Fixed it temporarily by replacing import { yupResolver } from '@hookform/resolvers/yup'; with import { yupResolver } from '@hookform/resolvers/yup/dist/yup.umd';. Maybe it's going to work for you as well.

@mrodrigues95
Copy link
Author

@allwd That seems to work as a work around for the time being, thanks.

@bluebill1049 bluebill1049 added the bug Something isn't working label Oct 31, 2021
@jhg023
Copy link

jhg023 commented Nov 1, 2021

I'm facing this bug as well after updating to Nextjs 12.0.2, and I did not encounter this issue on Nextjs 12.0.1

@Czer0C
Copy link

Czer0C commented Nov 1, 2021

Having the same issue with NextJS 12.0.2, the temporary fix provided by @allwd works.

@sgonv
Copy link

sgonv commented Nov 1, 2021

How about Typescript? I tried @allwd 's suggestion, but it doesn't work well.

Type error: Could not find a declaration file for module '@hookform/resolvers/yup/dist/yup.umd'.
'/path/to/my_project/node_modules/@hookform/resolvers/yup/dist/yup.umd.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/hookform__resolvers` if it exists or add a new declaration (.d.ts) file containing `declare module '@hookform/resolvers/yup/dist/yup.umd';`

@azarmehri
Copy link

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 😊

@jorisre
Copy link
Member

jorisre commented Nov 1, 2021

Hey all 👋🏻
I'm working on that issue, I'll back to you soon as possible

@bluebill1049
Copy link
Member

thanks @jorisre 🙏

@sgonv
Copy link

sgonv commented Nov 2, 2021

@azarmehri Oh, it worked for me. Thanks!

@hongdeyuan
Copy link

hongdeyuan commented Nov 2, 2021

Having the same issue with NextJS 12.0.2;

import { zodResolver } from '@hookform/resolvers/zod/dist/zod' instead @hookform/resolvers/zod

This is a way to bypass the Rust compiler and does not solve the fundamental problem. If you install a dependency that does not satisfy the CommonJS module, the project will still not run properly

@uDaniAlves
Copy link

I'm having the same issue and @allwd solution worked from me but Storybook stopped working.

The errors are:
ERROR in ./node_modules/@hookform/resolvers/yup/dist/yup.mjs 1:909-910 Can't import the named export 'appendErrors' from non EcmaScript module (only default export is available)

ERROR in ./node_modules/@hookform/resolvers/yup/dist/yup.mjs 1:710-711 Can't import the named export 'toNestError' from non EcmaScript module (only default export is available)

ERROR in ./node_modules/@hookform/resolvers/yup/dist/yup.mjs 1:582-583 Can't import the named export 'validateFieldsNatively' from non EcmaScript module (only default export is available)

@Yhozen
Copy link

Yhozen commented Nov 2, 2021

Another fix would be

// eslint-disable-next-line @typescript-eslint/no-var-requires
const { yupResolver } = require('@hookform/resolvers/yup')

@bluebill1049 bluebill1049 added the help wanted Extra attention is needed label Nov 2, 2021
@JamesWong1999
Copy link
Contributor

similar issue: vercel/next.js#30750 ?

@PaulHaze
Copy link

PaulHaze commented Nov 3, 2021

Facing the same issue with Next Js, tried @allwd & @Yhozen solutions. Neither worked



10:44:06.116 | > Build error occurred
-- | --
10:44:06.117 | file:///vercel/path0/node_modules/@hookform/resolvers/dist/resolvers.mjs:1
10:44:06.117 | import{get  as r,set as e}from"react-hook-form";var i=function(e,i){for(var a in  i.fields){var  f=i.fields[a];if(f&&f.ref&&"reportValidity"in f.ref){var   t=r(e,a);f.ref.setCustomValidity(t&&t.message\|\|""),f.ref.reportValidity()}}},a=function(a,f){f.shouldUseNativeValidation&&i(a,f);var  t={};for(var o in a){var  s=r(f.fields,o);e(t,o,Object.assign(a[o],{ref:s&&s.ref}))}return  t};export{a as toNestError,i as validateFieldsNatively};
10:44:06.118 | ^^^
10:44:06.118 | 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.

If anyone has any other solutions I would love to hear them!

EDIT Next.Js versions 11.1.3 and 12.0.1 have fixed the build issue so at least it building for now....

@dsacramone
Copy link

dsacramone commented Nov 4, 2021

Same issue. No solutions presented here works.

These did not work:
import { yupResolver } from '@hookform/resolvers/yup/dist/yup.umd';
const { yupResolver } = require('@hookform/resolvers/yup');
import { yupResolver } from '@hookform/resolvers/yup/dist/yup';

Any other solutions?

@jorisre - any updates to report?

@PaulHaze
Copy link

PaulHaze commented Nov 4, 2021

Same issue. No solutions presented here works.

These did not work: import { yupResolver } from '@hookform/resolvers/yup/dist/yup.umd'; const { yupResolver } = require('@hookform/resolvers/yup'); import { yupResolver } from '@hookform/resolvers/yup/dist/yup';

Any other solutions?

@jorisre - any updates to report?

@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:
vercel/next.js#30750

@bluebill1049
Copy link
Member

can we try nextjs v12.0.3-canary.2? see if this issue is getting resolved?

@PaulHaze
Copy link

PaulHaze commented Nov 4, 2021

can we try nextjs v12.0.3-canary.2? see if this issue is getting resolved?

I actually tried that version before reverting back to 12.0.1 and from what I could tell on my end it didn't do anything to resolve it. I tried the 3 methods of importing that @allwd & @Yhozen mentioned.

@dsacramone
Copy link

dsacramone commented Nov 5, 2021

Facing the same issue with Next Js, tried @allwd & @Yhozen solutions. Neither worked



10:44:06.116 | > Build error occurred
-- | --
10:44:06.117 | file:///vercel/path0/node_modules/@hookform/resolvers/dist/resolvers.mjs:1
10:44:06.117 | import{get  as r,set as e}from"react-hook-form";var i=function(e,i){for(var a in  i.fields){var  f=i.fields[a];if(f&&f.ref&&"reportValidity"in f.ref){var   t=r(e,a);f.ref.setCustomValidity(t&&t.message\|\|""),f.ref.reportValidity()}}},a=function(a,f){f.shouldUseNativeValidation&&i(a,f);var  t={};for(var o in a){var  s=r(f.fields,o);e(t,o,Object.assign(a[o],{ref:s&&s.ref}))}return  t};export{a as toNestError,i as validateFieldsNatively};
10:44:06.118 | ^^^
10:44:06.118 | 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.

If anyone has any other solutions I would love to hear them!

EDIT Next.Js versions 11.1.3 and 12.0.1 have fixed the build issue so at least it building for now....

Hey @PaulHaze , are you still importing it the same? I did as you suggest and am using 12.0.1, and it did build, but when viewed in a browser, the page doesn't work - as expected if my hookform isn't working. Did you use a special "import" or way to get at it?

my error is:

excludeEmptyString is not defined

@jhg023
Copy link

jhg023 commented Nov 6, 2021

Nextjs 12.0.3 was released, but it doesn't resolve this issue, unfortunately.

@bigint
Copy link

bigint commented Nov 7, 2021

Yes still this is blocking 😕

@phanvanluan94
Copy link

Same issue.

@bluebill1049 bluebill1049 added the dependencies Pull requests that update a dependency file label Nov 8, 2021
@jeromeSH26
Copy link

downgrading to 12.0.1 fix the issue

@PaulHaze
Copy link

PaulHaze commented Nov 8, 2021

Hey @PaulHaze , are you still importing it the same? I did as you suggest and am using 12.0.1, and it did build, but when viewed in a browser, the page doesn't work - as expected if my hookform isn't working. Did you use a special "import" or way to get at it?

my error is:

excludeEmptyString is not defined

@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 !? :)

@bluebill1049
Copy link
Member

all valid opinions @yordis

@pronovaso
Copy link

I confirm, that version 7.21.1 and Next 12.0.7 works well. 7.21.1. Thanks a lot.

@simontaisne
Copy link

7.21.1 works well, but not 7.21.2 😅

@evocateur
Copy link

I believe the root cause for the CRA issue is that RHF uses the import * as React from 'react' form when it should be using import 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.

@sveggiani
Copy link

For me neither 7.21.1 or 7.21.2 are working with Next.js 12.0.7.

evocateur added a commit to evocateur/react-hook-form that referenced this issue Dec 10, 2021
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 Next v12, no longer explode when encountering this module.

Refs react-hook-form#7095
Refs react-hook-form#7088
Refs react-hook-form/resolvers#271
Refs vercel/next.js#30750
evocateur added a commit to evocateur/react-hook-form that referenced this issue Dec 10, 2021
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 Next v12, no longer explode when encountering this module.

Refs react-hook-form#7244
Refs react-hook-form#7095
Refs react-hook-form#7088
Refs react-hook-form/resolvers#271
Refs vercel/next.js#30750
@evocateur
Copy link

I verified that react-hook-form/react-hook-form#7261 works for NextJS 12.0.7, but I don't have a Storybook or CRA project offhand to test the backward-compatibility.

@bluebill1049
Copy link
Member

thank you again @evocateur

bluebill1049 pushed a commit to react-hook-form/react-hook-form that referenced this issue Dec 11, 2021
)

* 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 Next v12, no longer explode when encountering this module.

Refs #7244
Refs #7095
Refs #7088
Refs react-hook-form/resolvers#271
Refs vercel/next.js#30750

* fix(esm-interop): Import non-ESM React as default
@bluebill1049
Copy link
Member

beta: Version 7.21.3-beta.0

@bluebill1049
Copy link
Member

the above beta pass test for my with CRA and nextjs thank so much @evocateur life saver.

@JuanM04
Copy link

JuanM04 commented Dec 11, 2021

v7.21.3-beta.0 works perfectly!

@bluebill1049
Copy link
Member

oh wow, I didn't check evocateur's profile, he is the creator/author of Lerna. Respect 🎩

@Alexandredc
Copy link

Many thanks @bluebill1049 & @evocateur 🙏

@bluebill1049
Copy link
Member

thanks @Alexandredc All effort and credit goes to evocateur ❤️

bluebill1049 added a commit to react-hook-form/react-hook-form that referenced this issue Dec 13, 2021
… (#7262)

* 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 package.json (implicit) type of commonjs. Bundlers that expect ESM, such as Next v12, no longer explode when encountering this module.

Refs #7244
Refs #7095
Refs #7088
Refs react-hook-form/resolvers#271
Refs vercel/next.js#30750

* fix(esm-interop): Import non-ESM React as default

* 7.21.3-beta.0

* update with v2 compress

* fix lint error and update packages

Co-authored-by: Daniel Stockman <5605+evocateur@users.noreply.github.com>
@evocateur
Copy link

@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!

@bluebill1049
Copy link
Member

hopefully, this is the last time i close this issue: https://github.com/react-hook-form/react-hook-form/releases/tag/v7.22.0 🙏

@mustafaabobakr
Copy link

@Yhozen Solution Worked, Thank you 🎉

const { yupResolver } = require('@hookform/resolvers/yup');

My versions :

{
    "@hookform/resolvers": "^2.8.8",
    "next": "^12.1.4",
    "react-hook-form": "^7.29.0",
    "yup": "^0.32.11"
}

@ayepRahman
Copy link

ayepRahman commented Nov 19, 2022

This reoccur in next js v13 when I am trying to use zodResolver and try running yarn build with nextjs 13. I've also created a ticket in vercel

Link - vercel/next.js#43121

 "dependencies": {
    "@heroicons/react": "^2.0.13",
    "@hookform/error-message": "^2.0.1",
    "@hookform/resolvers": "^2.9.10",
    "@tanstack/react-query": "^4.16.1",
    "@types/node": "18.11.9",
    "@types/react": "18.0.25",
    "@types/react-dom": "18.0.9",
    "aws-sdk": "^2.1257.0",
    "axios": "^1.1.3",
    "clsx": "^1.2.1",
    "dotenv": "^16.0.3",
    "eslint": "8.27.0",
    "eslint-config-next": "^13.0.3",
    "flowbite": "^1.5.4",
    "flowbite-react": "^0.3.5",
    "mongodb": "^4.11.0",
    "multer": "^1.4.5-lts.1",
    "next": "^13.0.3",
    "next-connect": "^0.13.0",
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-dropzone": "^14.2.3",
    "react-hook-form": "^7.39.4",
    "react-tailwindcss-select": "^1.3.3",
    "typescript": "4.9.3",
    "zod": "^3.19.1"
  },
ReferenceError: File is not defined
    at 3608 (/Users/arifrahman/Code/personal/pokedex-nextjs13/.next/server/app/pokemon/create/page.js:412:31)
    at __webpack_require__ (/Users/arifrahman/Code/personal/pokedex-nextjs13/.next/server/webpack-runtime.js:25:42)
    at B (/Users/arifrahman/Code/personal/pokedex-nextjs13/node_modules/next/dist/compiled/react-server-dom-webpack/client.js:924:142)
    at H (/Users/arifrahman/Code/personal/pokedex-nextjs13/node_modules/next/dist/compiled/react-server-dom-webpack/client.js:922:264)
    at /Users/arifrahman/Code/personal/pokedex-nextjs13/node_modules/next/dist/compiled/react-server-dom-webpack/client.js:927:218

@Junnygram
Copy link

how do i fix the error in my next13 app
Type 'Resolver<{ password?: string | undefined; email: string; }>' is not assignable to type 'Resolver<LoginModel, any>'.
Types of parameters 'values' and 'values' are incompatible.
Type 'LoginModel' is not assignable to type '{ password?: string | undefined; email: string; }'

export type LoginModel = { email?: string; password?: string; };
const { handleSubmit, register, formState: { errors, isSubmitting, isValid }, } = useForm<LoginModel>({ resolver: yupResolver(schema), mode: 'all', });

const schema = yup.object().shape({ email: yup.string().email().required(), password: yup.string().password(), });

@hongdeyuan
Copy link

hongdeyuan commented Sep 11, 2023 via email

@Junnygram
Copy link

its been fixed , i just had to through the documentation well
const schema = yup.object().shape({ email: yup.string().email().required(), password: yup.string().password().required(), });

for the type
export type LoginModel = { email: string; password: string; };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependencies Pull requests that update a dependency file help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests