-
Notifications
You must be signed in to change notification settings - Fork 27.2k
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
Next.js 12.0.2 + Framer 5.0.1 leading to errors in production #30750
Comments
Seeing this as well, there's a "minimal" reproducible repository here: https://github.com/jokull/next12-react-form-test/blob/main/pages/index.js - this uses I think any dependency that does things like Weirdly enough this doesn't seem to have anything to do with SWC - I've tried this both with and without a custom babel config file (which disables SWC on Next@12) and the error is the same either way. Seems to be something else that happens behind the scenes in Next@12, I'm reverting to Next@11 for now. I've also seen this sporadically in development (the above linked repo is consistent in development), I think it most likely works locally sometimes because of next's build cache, though I'm not sure. |
@KevinShiCA thanks for building on this. Are you saying if dependencies used |
Hah, I actually never bothered trying 12.0.1. It works for me, thanks for that! I'm convinced it's this PR: #30427. I tried out the various canary versions that led up to /cc @sokra who authored that PR
Yes, but ESM and CJS have a hell of a time interoping together. Here's a great article about it: https://simonplend.com/node-js-now-supports-named-imports-from-commonjs-modules-but-what-does-that-mean/ Unfortunately React only provides a CJS build. The community was hoping for an ESM build to come in React 18, but it sounds like it's going to come in version 19 or later. |
Downgrading to 12.0.1 worked for me too. This was a nasty bug to ship, where a site completely fails but only in production. How can this type of error be caught in the future? What tests were missing that allowed it to ship? As someone whose business depends on NextJS & Vercel, I'd love to see a retrospective on this issue to identify the gap in quality control and the mitigation steps being put in place to prevent a re-occurrence. |
Thanks for all this background @KevinShiCA! Super helpful. Any suspicions why this works locally but not hosted (on |
I'm having the same issues with importing hooks on 12.0.2. |
Same kinds of errors here but happens when using next@12.0.2 with react-hook-form@7.12.1 Luckily it failed for me in local dev instead of in production. Also reverted to next@12.0.1 to resolve the issue for now. |
It seems that it's SSR is broken for rendering hooks. Any hook I import on SSR gets imported as undefined. |
I had an issue, where Next.js built the code for Production just fine, but loading the page would seem to have no React components. Submitting the form would just do a simple HTML submit (reload the page, with form inputs as query string params). Downgrading to v12.0.1 seems to fix the issue. |
I'll look into the repro. If this is related to ESM externals, instead of downgrading Next.js you can temporary disable |
@KevinShiCA Regarding It claims to provide a version compatible with Node.js ESM (it has an
This should be fixed in the
PS: Please try |
I'm seeing similar errors in the latest version of file:///<project-path>/node_modules/zustand/esm/index.mjs:1
import { useReducer, useRef, useEffect, useLayoutEffect } from 'react';
^^^^^^
SyntaxError: Named export 'useRef' not found. The requested module 'react' 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';
const { useReducer, useRef, useEffect, useLayoutEffect } = pkg;
at ModuleJob._instantiate (internal/modules/esm/module_job.js:104:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:149:5)
at async Loader.import (internal/modules/esm/loader.js:177:24)
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason:
file:///<project-path>/zustand/esm/index.mjs:1
import { useReducer, useRef, useEffect, useLayoutEffect } from 'react';
^^^^^^
SyntaxError: Named export 'useRef' not found. The requested module 'react' 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';
const { useReducer, useRef, useEffect, useLayoutEffect } = pkg;
at ModuleJob._instantiate (internal/modules/esm/module_job.js:104:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:149:5)
at async Loader.import (internal/modules/esm/loader.js:177:24)
error - unhandledRejection: SyntaxError: Named export 'useRef' not found. The requested module 'react' 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';
const { useReducer, useRef, useEffect, useLayoutEffect } = pkg; |
I will take a look at Internally we were just having a debate about if any of our open source packages are being use by folks outside of Shopify so this is very cool to see 😄 |
@sokra just posted a minimal example! Interestingly, this works fine until the last commit ( As before, this all works in development—only crashes in production. Good luck! |
Hey @michenly
Just wanted to let you know we love |
puts on Shopify employee hat @sokra thank you for that investigation. I've identified the root cause as coming from our rollup babel plugin - it's not allowing us to rename the file its helpers live in. I've documented the root cause at rollup/plugins#1031. |
Hi, this has been updated in |
Just updated my example repo to |
Confirmed that |
react-hook-form/resolvers#271 does not work with Changing the import from Edit: |
I'm having the same problem with Zustand. I downgraded Next to 12.0.1 and it works now.
|
next >12.0.1 vil ikke builde ordentlig uten esmExternals satt til false i next.config.js. Vet ikke når dette blir fikset, se vercel/next.js#30750 (Måtte også legge til next.config.js i tsconfig.json siden eslint klikka når den prøvde å linte en fil som ikke var inkludert).
Yesssssss, I'm facing this issue , but now it works when I downgrade to next@12.0.1. Thanks you and everyone in this conversation. |
For note, with v12.0.4 & And to avoid issue with |
Using patch-package, this fixed it for me (with react-hook-form@7.21.0): diff --git a/node_modules/react-hook-form/dist/index.esm.js b/node_modules/react-hook-form/dist/index.mjs
similarity index 100%
rename from node_modules/react-hook-form/dist/index.esm.js
rename to node_modules/react-hook-form/dist/index.mjs
diff --git a/node_modules/react-hook-form/package.json b/node_modules/react-hook-form/package.json
index 579ab00..1e1c18e 100644
--- a/node_modules/react-hook-form/package.json
+++ b/node_modules/react-hook-form/package.json
@@ -3,11 +3,11 @@
"description": "Performant, flexible and extensible forms library for React Hooks",
"version": "7.21.0",
"main": "dist/index.cjs.js",
- "module": "dist/index.esm.js",
+ "module": "dist/index.mjs",
"umd:main": "dist/index.umd.js",
"unpkg": "dist/index.umd.js",
"jsdelivr": "dist/index.umd.js",
- "jsnext:main": "dist/index.esm.js",
+ "jsnext:main": "dist/index.mjs",
"source": "src/index.ts",
"types": "dist/index.d.ts",
"sideEffects": true,
@@ -18,7 +18,7 @@
"exports": {
"./package.json": "./package.json",
".": {
- "import": "./dist/index.esm.js",
+ "import": "./dist/index.mjs",
"require": "./dist/index.cjs.js"
}
}, Basically, the import target needs to use the |
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
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 #7095 Refs #7088 Refs react-hook-form/resolvers#271 Refs vercel/next.js#30750
The fix from @daniel-stockman-lark for react-hook-form has been implemented the other day but rolled back because of compatibility issues with CRA. @yordis has summarized the problem here: react-hook-form/resolvers#271 (comment) |
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
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
) * 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
… (#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>
FYI, @BPScott had added fix to |
Potential issue: we have a user reported |
…s to be an open issue with Next: vercel/next.js#30750
Hi, as the OP stated in #30750 (comment) Next.js should now work with Framer (and presumably with other properly configured libraries), which this issue was opened about. I tried to look for other reproductions after the OP confirmed the issue is fixed, but I only see other libraries being reported with similar issues, and some of them acknowledged the problem is on their side and have patched it already. I cannot fully see if all concerns have been addressed for the others (other than @bluebill1049, do you know if the issue still happens?), but to make any potential remaining problem easier to track/fix, I would like to ask those who have this kind of issue to test their code using our I'll keep this issue open for a while still, so anyone can comment on their concerns, but after that, I would like to consider this specific issue (about Next.js and Framer) to be closed. This helps us track down if the issue is indeed in Next.js or a certain library. Thank you! 🙏 |
Thanks for the follow-up @balazsorban44. The original issue was posted and has not gotten any more response and we suspect it to do with his own setup. I am going to close that issue there and follow up if other issues related pop up again. |
chiming in -- > next build
info - Checking validity of types
info - Creating an optimized production build
info - Compiled successfully
> Build error occurred
file:///Users/amardeepgill/personal-projects/ifrit/node_modules/zustand/esm/index.mjs:1
import { useReducer, useRef, useDebugValue, useEffect, useLayoutEffect } from 'react';
^^^^^^
SyntaxError: The requested module 'react' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
For example:
import pkg from 'react';
const { useReducer, useRef, useDebugValue, useEffect, useLayoutEffect } = pkg;
at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:137:5)
at async Loader.import (internal/modules/esm/loader.js:165:24) {
type: 'SyntaxError'
} Downgrading to Edit: looks to fail builds starting from |
@Amar-Gill please read #30750 (comment) Using the I'm closing this issue now. Any similar issues should be opened as a separate bug report, attaching a reproduction, after testing with the Thanks 🙏 |
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
What version of Next.js are you using?
12.0.2
What version of Node.js are you using?
14.16.0
What browser are you using?
Chrome 95
What operating system are you using?
macOS
How are you deploying your application?
Vercel
Describe the Bug
Seeing a
500
application error when deploying app toVercel
with the following dependencies:next: 12.0.2
framer: 5.0.1
App deploys fine with
next: 12.0.1
Vercel
error logs below:Expected Behavior
Production environment should mimic development, i.e., not crash
To Reproduce
Does not reproduce locally—only seeing on
Vercel
in productionThe text was updated successfully, but these errors were encountered: