Next.js 13: SWC Emotion Transform Plugin Breaks with root layout Server Component in `app/`
See original GitHub issueVerify canary release
- I verified that the issue exists in the latest Next.js canary release
Provide environment information
Operating System:
Platform: linux
Arch: x64
Version: Ubuntu 20.04.0 LTS Thu Oct 27 2022 20:19:36 GMT+0200 (Central European Summer Time)
Binaries:
Node: 16.14.2
npm: 7.17.0
Yarn: 1.22.19
pnpm: 7.13.6
Relevant packages:
next: 13.0.1-canary.0
eslint-config-next: N/A
react: 18.2.0
react-dom: 18.2.0
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
Using the compiler: { emotion: true }
option is throwing an error about React.createContext
not being a function:
event - compiled client and server successfully in 59 ms (403 modules)
error - (sc_server)/node_modules/@emotion/react/dist/emotion-element-b63ca7c6.cjs.dev.js (20:47) @ eval
error - TypeError: React.createContext is not a function
at eval (webpack-internal:///(sc_server)/./node_modules/@emotion/react/dist/emotion-element-b63ca7c6.cjs.dev.js:19:49)
at (sc_server)/./node_modules/@emotion/react/dist/emotion-element-b63ca7c6.cjs.dev.js (/home/projects/vercel-next-js-mxnxa7/.next/server/app/page.js:501:1)
at __webpack_require__ (/home/projects/vercel-next-js-mxnxa7/.next/server/webpack-runtime.js:33:43)
at eval (webpack-internal:///(sc_server)/./node_modules/@emotion/react/jsx-dev-runtime/dist/emotion-react-jsx-dev-runtime.cjs.dev.js:7:22)
at (sc_server)/./node_modules/@emotion/react/jsx-dev-runtime/dist/emotion-react-jsx-dev-runtime.cjs.dev.js (/home/projects/vercel-next-js-mxnxa7/.next/server/app/page.js:512:1)
at __webpack_require__ (/home/projects/vercel-next-js-mxnxa7/.next/server/webpack-runtime.js:33:43)
at eval (webpack-internal:///(sc_server)/./app/layout.tsx:5:88)
at (sc_server)/./app/layout.tsx (/home/projects/vercel-next-js-mxnxa7/.next/server/app/page.js:403:1)
at __webpack_require__ (/home/projects/vercel-next-js-mxnxa7/.next/server/webpack-runtime.js:33:43)
at Object.layout (webpack-internal:///(sc_server)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fpage&appPaths=%2Fpage&pagePath=private-next-app-dir%2Fpage.tsx&appDir=%2Fhome%2Fprojects%2Fvercel-next-js-mxnxa7%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2Fhome%2Fprojects%2Fvercel-next-js-mxnxa7&isDev=true&tsconfigPath=tsconfig.json!:22:99) {
type: 'TypeError',
page: '/'
}
null
Making the root layout component into a client component does work, but this would be unfortunate:
Workaround:
Disable the SWC Emotion transform plugin and use the /** @jsxImportSource @emotion/react */
directive instead:
Expected Behavior
It works to use the SWC Emotion transform plugin via compiler: { emotion: true }
with a server component as the root layout
Link to reproduction
To Reproduce
- Install
next@13.0.1-canary.0
,@emotion/cache@11.10.5
,@emotion/react@11.10.5
- Enable the
app/
dir and the SWC Emotion Transform plugin usingcompiler: { emotion: true }, experimental: { appDir: true }
innext.config.js
- Add the files as mentioned in @mitchellhamilton’s post here: https://github.com/emotion-js/emotion/issues/2928#issuecomment-1293012737
- Remove the
/** @jsxImportSource @emotion/react */
directive to rely on the SWC Emotion transform plugin to addcss
prop support - Start the server and observe the error
Issue Analytics
- State:
- Created a year ago
- Reactions:44
- Comments:26 (3 by maintainers)
Top Results From Across the Web
Advanced Features: Next.js Compiler
js Compiler, written in Rust using SWC, allows Next.js to transform and minify your JavaScript code for production. This replaces Babel for individual...
Read more >Upgrade Guide - Next.js
Upgrading from 12 to 13. To update to Next.js version 13, run the following command using your preferred package manager ...
Read more >Blog - Next.js 13
Next.js 13 introduces layouts, React Server Components, and streaming in the app directory, as well as Turbopack, an improved image ...
Read more >Blog - Next.js 13.1
Next.js 13.1 introduces improvements to the app directory, built-in module transpilation, stable edge runtime for API Routes, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
In the same way, I experience the same problem with @mui, which uses emotion under the hood.
Yep I’m stuck too. I can`t fully migrate to v13 and adopt it because the project depends on MUI