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

Uncaught ReferenceError: exports is not defined #71

Closed
batamire opened this issue Nov 5, 2020 · 52 comments
Closed

Uncaught ReferenceError: exports is not defined #71

batamire opened this issue Nov 5, 2020 · 52 comments
Labels
bug Something isn't working

Comments

@batamire
Copy link

batamire commented Nov 5, 2020

trying to:

import { zodResolver } from "@hookform/resolvers/zod";

gives me:

Uncaught ReferenceError: exports is not defined
    at Module../node_modules/@hookform/resolvers/dist/zod.js (zod.js:11)
    at __webpack_require__ (bootstrap:789)
    at fn (bootstrap:100)
    at Object../node_modules/@hookform/resolvers/zod.js (zod.js:1)
    at __webpack_require__ (bootstrap:789)
    at fn (bootstrap:100)
    ...

This is a Rails webpacker + typescript app.

@bluebill1049
Copy link
Member

more detail... which version and etc... can you reproduce that in CSB?

@bluebill1049 bluebill1049 added the question Further information is requested label Nov 5, 2020
@batamire
Copy link
Author

batamire commented Nov 5, 2020

@hookform/resolvers": "^1.0.0"

I'm afraid I cannot reproduce it as this is a large Rails app with TypeScript React and GraphQL. Could it be the way you export modules? I really lack JS packaging knowledge...

my babel.config.js:

module.exports = function(api) {
  var validEnv = ["development", "test", "production"];
  var currentEnv = api.env();
  var isDevelopmentEnv = api.env("development");
  var isProductionEnv = api.env("production");
  var isTestEnv = api.env("test");

  if (!validEnv.includes(currentEnv)) {
    throw new Error(
      "Please specify a valid `NODE_ENV` or " +
        '`BABEL_ENV` environment variables. Valid values are "development", ' +
        '"test", and "production". Instead, received: ' +
        JSON.stringify(currentEnv) +
        "."
    );
  }

  return {
    presets: [
      isTestEnv && [
        "@babel/preset-env",
        {
          targets: {
            node: "current"
          },
          modules: "commonjs"
        },
        "@babel/preset-react"
      ],
      (isProductionEnv || isDevelopmentEnv) && [
        "@babel/preset-env",
        {
          forceAllTransforms: true,
          useBuiltIns: "entry",
          corejs: 3,
          modules: false,
          exclude: ["transform-typeof-symbol"]
        }
      ],
      [
        "@babel/preset-react",
        {
          development: isDevelopmentEnv || isTestEnv,
          useBuiltIns: true
        }
      ],
      ['@babel/preset-typescript', { 'allExtensions': true, 'isTSX': true }]
    ].filter(Boolean),
    plugins: [
      "babel-plugin-macros",
      "@babel/plugin-syntax-dynamic-import",
      isTestEnv && "babel-plugin-dynamic-import-node",
      "@babel/plugin-transform-destructuring",
      [
        "@babel/plugin-proposal-class-properties",
        {
          loose: true
        }
      ],
      [
        "@babel/plugin-proposal-object-rest-spread",
        {
          useBuiltIns: true
        }
      ],
      [
        "@babel/plugin-transform-runtime",
        {
          helpers: false,
          regenerator: true,
          corejs: false
        }
      ],
      [
        "@babel/plugin-transform-regenerator",
        {
          async: false
        }
      ],
      isProductionEnv && [
        "babel-plugin-transform-react-remove-prop-types",
        {
          removeImport: true
        }
      ]
    ].filter(Boolean)
  };
};

tsconfig.json

{
  "compilerOptions": {
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5",
    "jsx": "react",
    "allowSyntheticDefaultImports": true,
    "noEmit": true,
    "noImplicitAny": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "strictNullChecks": true
  },
  "exclude": ["**/*.spec.ts", "node_modules", "public"],
  "compileOnSave": false
}

@jorisre
Copy link
Member

jorisre commented Nov 5, 2020

@batamire Hi 👋🏻

Does that error occur at build time ?

Can you provide more informations please ?
Your code snippets doesn't help me :/

@sfederowicz
Copy link

I'm running into that same runtime error (also using Rails + webpacker but no Typescript). I'm importing the yup resolver rather than the zod resolver, but it's the same error otherwise.

My Babel config is identical to the one posted above, excluding only the Typescript preset line (since we don't use Typescript):
['@babel/preset-typescript', { 'allExtensions': true, 'isTSX': true }]

Issue occurs when using the 1.0.0 or 2.0.0-rc.1 version of the lib but didn't occur with the 0.1.1 or 0.2.0-beta.12.

Adding this plugin to Babel fixes the issue and prevents the runtime error from being thrown:
@babel/plugin-transform-modules-commonjs

Unfortunately, adding that plugin also causes a different runtime errors to be thrown for me in some Rails-related JS code (code that is unrelated to react-hook-form), so that alone likely won't be your panacea. Still trying to figure out how to get around this since I'm not great with webpack.

@batamire
Copy link
Author

batamire commented Nov 5, 2020

It builds without problems, the error is pasted from Chrome Devtools, happens on component mount...

Uncaught ReferenceError: exports is not defined

Offending line:

Object.defineProperty(exports, "__esModule", {
  value: true
});

@sfederowicz same thing happens with yup...

@studnik18
Copy link

Up for yup...

@bluebill1049
Copy link
Member

bluebill1049 commented Nov 16, 2020

Is this still an issue for everyone? (i couldn't reproduce)

@studnik18
Copy link

Yes. Can't update from 0.0.6 because of it. My most relevant dependencies:
"react": "^17.0.1", "react-hook-form": "^6.11.0", "@hookform/resolvers": "^0.0.6", "yup": "^0.29.3", "@rails/webpacker": "^5.2.1", "react-on-rails": "^12.0.3"

@bluebill1049
Copy link
Member

bluebill1049 commented Nov 17, 2020

@studnik18 have you try to upgrade to the latest? and what build tool are you using?

@studnik18
Copy link

Yes, I tried just before posting :) Our setting is quite complicated - it's a monorepo with rails 6 and webpacker as ruby gems, on the frontend side it's @rails/webpacker 5.2.1 which features webpack 4.x.x according to the docs (no TypeScript).

@brentmulligan
Copy link

Having the same issue, also using rails/webpacker. Downgrading to 0.1.1 did resolve.

@johnandresmedina
Copy link

I was having the same issue, I tried to replicate it in codesandbox with no luck and after spending some more time it turned out to be a reference to the old way of importing the package: @hookform/resolvers instead of @hookform/resolvers/yup, so you might want to check all the references.

@studnik18
Copy link

I had this lightbulb moment too :) But unfortunately that solution alone didn't help. Thanks anyway John.

@ramaboo
Copy link

ramaboo commented Dec 14, 2020

Getting this too for yup import { yupResolver } from "@hookform/resolvers/yup"; on a Rails 6.1 project

@jorisre
Copy link
Member

jorisre commented Dec 14, 2020

Thanks for reporting, I'm looking for a solution

@YimingIsCOLD
Copy link

YimingIsCOLD commented Dec 23, 2020

I am getting this too for yup on a non-rails project. I am only getting the error on production build and not in development build.

Edit: after adding sourceType: 'unambiguous' into babel.config.js, I am able to resolve the runtime error. The issue might be webpack/webpack#4039 (comment).

@jorisre
Copy link
Member

jorisre commented Dec 31, 2020

Hi all, the last version 1.3.1 should fix this issue, someone can confirm ? Thanks

@amankkg
Copy link

amankkg commented Dec 31, 2020

@jorisre 1.3.1 using snowpack on import {zodResolver} from '@hookform/resolvers/zod throws following error

SyntaxError: The requested module '../../web_modules/@hookform/resolvers/zod.js' does not provide an export named 'zodResolver'

@jorisre
Copy link
Member

jorisre commented Dec 31, 2020

@amankkg thank for your feedbacks. Could you provide a snowpack example please ?

ℹ️ I'm working on resolvers v2 with a better module support

@amankkg
Copy link

amankkg commented Dec 31, 2020

@jorisre hmm on 1.3.0 it works properly

here is my WIP https://github.com/amankkg/frontend-template

@amankkg
Copy link

amankkg commented Dec 31, 2020

@jorisre it is quite messy 😅 let me know if you need clear and stable reproduction repo

@amankkg
Copy link

amankkg commented Jan 5, 2021

My case is fixed with 1.4.0-beta.1

@jorisre jorisre mentioned this issue Jan 5, 2021
5 tasks
@jorisre
Copy link
Member

jorisre commented Jan 5, 2021

@amankkg I'm glad it solves your problem ! :) But 1.4.0-beta.1 was a mistake with semantic-release. Please use 2.0.0-beta.3 instead.


Next.js's users have the same problem since 1.3.1 : #100
Workaround possible: use the v1.3.0 or try the v2 beta

EDIT (2/1/2020): Same for Snowpack users


For everyone which want to try the v2, you can install the @hookform/resolvers@beta, at this time the beta.3 contains:

  • A TypeScript fix for Yup
  • A better modules support (without breaking changes)

Feedbacks are really appreciated :) If some of you get some errors with modules, please reply here.

@jorisre jorisre added bug Something isn't working and removed question Further information is requested labels Jan 5, 2021
@MontoyaAndres
Copy link

MontoyaAndres commented Jan 24, 2021

I'm facing the same issue as #100 ,

andres@andres:~/Entrepreneurship/TeVi$ yarn build

yarn run v1.22.10
$ next build
Browserslist: caniuse-lite is outdated. Please run:
npx browserslist@latest --update-db
info  - Using external babel configuration from /home/andres/Entrepreneurship/TeVi/.babelrc
info  - Creating an optimized production build  
info  - Compiled successfully

> Build error occurred
/home/andres/Entrepreneurship/TeVi/node_modules/@hookform/resolvers/dist/yup.js:2
import { transformToNestObject } from 'react-hook-form';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (/home/andres/Entrepreneurship/TeVi/node_modules/@hookform/resolvers/yup.js:1:18)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) {
  type: 'SyntaxError'
}
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Now, I can't deploy to vercel :(

The versions I'm using are:

    "@hookform/resolvers": "^1.2.0",
    "next": "^10.0.4",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-hook-form": "^6.13.1",
    "yup": "^0.32.8"

@MontoyaAndres
Copy link

Also, I updated to "@hookform/resolvers": "1.3.2" and I get the same error...

@bluebill1049
Copy link
Member

@MontoyaAndres try to use the latest beta version.

@MontoyaAndres
Copy link

@bluebill1049 Works great! Thank you so much! I was deploying normally to Vercel when that error appears surprisingly 🤔🤔🤔.

@bluebill1049
Copy link
Member

@bluebill1049 Works great! Thank you so much! I was deploying normally to Vercel when that error appears surprisingly 🤔🤔🤔.

not sure about that :( sounds like picking update the latest, but your package.json says another story. (pick up the latest patch)

@MontoyaAndres
Copy link

Very strange, I didn't update anything in my code about react-hook-form... Just appeared when the building was executing in Vercel :(

@asotog
Copy link

asotog commented Jan 27, 2021

@amankkg I'm glad it solves your problem ! :) But 1.4.0-beta.1 was a mistake with semantic-release. Please use 2.0.0-beta.3 instead.

Next.js's users have the same problem since 1.3.1 : #100
Workaround possible: use the v1.3.0 or try the v2 beta

For everyone which want to try the v2, you can install the @hookform/resolvers@beta, at this time the beta.3 contains:

  • A TypeScript fix for Yup
  • A better modules support (without breaking changes)

Feedbacks are really appreciated :) If some of you get some errors with modules, please reply here.

This worked for me, thanx, when are you planning to release this version?

@jorisre
Copy link
Member

jorisre commented Jan 27, 2021

@asotog At the same time of react-hook-form v7

@bluebill1049 Do we have a "deadline" ?

@bluebill1049
Copy link
Member

@asotog At the same time of react-hook-form v7

@bluebill1049 Do we have a "deadline" ?

No, we don't. It's open-source, we are all working on this in our free time. So no pressure, we get it right and we ship it out. I am planning to do a quick video to cover some of the major changes and improvements, then we can release a beta version and get more users to "test" this out.

@borivojevic
Copy link

borivojevic commented Jan 30, 2021

I had the same issue with 1.3.2 using snowpack. 2.0.0-beta.3 didn't work either.

Although snowpack suggests this workaround that seems to be working for me:

// doesn't work
import { yupResolver } from '@hookform/resolvers/yup';

// it works
import type { yupResolver } from '@hookform/resolvers/yup';

@borivojevic
Copy link

Another workaround that works with snowpack v3.0.11 would be:

import hookforResolvers from '@hookform/resolvers/yup';
const yupResolver = hookforResolvers.yupResolver;

@bluebill1049
Copy link
Member

@jorisre any idea on this one above?

@jorisre
Copy link
Member

jorisre commented Feb 1, 2021

It seems to work for @tajdien, can you share a working example please ?

@borivojevic I'm not familiar with Snowpack but if you have a reproductible example to provide then I can investigate thanks !

@borivojevic
Copy link

@jorisre I hope this example helps.

yarn install && yarn start should result in error.

This is the implementation that's problematic.

@jorisre
Copy link
Member

jorisre commented Feb 1, 2021

@borivojevic Happy to try Snowpack, esbuild is fast as expected 🚀

In your example you use the version 1.3.3 which have an issue with module exports (solved in v2).
In your case, I recommend to use the 1.3.0 or try the v2 beta.
I get your example working with these both versions.

I added a note for Snowpack users : #71 (comment)

Hope it helps

@borivojevic
Copy link

Thanks @jorisre. I can confirm that v2 beta works on this smaller example as well.

@WorldOfMaze
Copy link

I was having this issue with yup and nextjs. After downgrading to 1.3.0, everything seems to be working OK. Thanks for the workaround @jorisre

@hamzahasbi
Copy link

using "@hookform/resolvers": "2.0.0-beta.3" with "next": "10.0.7" seems to work perfectly!
Thanks !

@hawkcookie
Copy link

hawkcookie commented Mar 2, 2021

using "@hookform/resolvers": "2.0.0-beta.3" with "next": "10.0.7" seems to work perfectly!

@hamzahasbi
It works well for me, thanks.

@javierfuentesm
Copy link

@amankkg I'm glad it solves your problem ! :) But 1.4.0-beta.1 was a mistake with semantic-release. Please use 2.0.0-beta.3 instead.

Next.js's users have the same problem since 1.3.1 : #100
Workaround possible: use the v1.3.0 or try the v2 beta

EDIT (2/1/2020): Same for Snowpack users

For everyone which want to try the v2, you can install the @hookform/resolvers@beta, at this time the beta.3 contains:

  • A TypeScript fix for Yup
  • A better modules support (without breaking changes)

Feedbacks are really appreciated :) If some of you get some errors with modules, please reply here.

It didnt work for me
image

image

@javierfuentesm
Copy link

using "@hookform/resolvers": "2.0.0-beta.3" with "next": "10.0.7" seems to work perfectly!

@hamzahasbi
It works well for me, thanks.

For me too !! :D

@72636c
Copy link

72636c commented Mar 12, 2021

^1.3.0 will install the latest version >=1.3.0 <2.0.0. If you want to pin a specific version you should remove the caret ^.

@iltonbarbosa
Copy link

I have the same problem:

exports.__esModule = true;
^

ReferenceError: exports is not defined

@jorisre
Copy link
Member

jorisre commented Mar 17, 2021

I have the same problem:

exports.__esModule = true;
^

ReferenceError: exports is not defined

#71 (comment)

@jorisre
Copy link
Member

jorisre commented Mar 26, 2021

👋🏻
You can try the last version v1.3.6. The issue should be solved

@jorisre jorisre closed this as completed Mar 28, 2021
@decebal
Copy link

decebal commented Sep 9, 2021

1.3.9 seems to have the same issue again :(

@studnik18
Copy link

There is 2.8.0 released. Why would you bother with 1.3.9 ?

@decebal
Copy link

decebal commented Sep 10, 2021

thanks @studnik18 🙈 👍🏽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests