styled-components: ReferenceError: window is not defined - NextJs 10.1.2
Environment
System:
- OS: Linux 4.19 Ubuntu 18.04.5 LTS (Bionic Beaver)
- CPU: (8) x64 Intel® Core™ i7-9700K CPU @ 3.60GHz
- Memory: 21.27 GB / 25.01 GB
- Container: Yes
- Shell: 5.4.2 - /usr/bin/zsh
Binaries:
- Node: 14.16.0 - ~/.nvm/versions/node/v14.16.0/bin/node
- Yarn: 1.22.5 - /usr/bin/yarn
- npm: 6.14.11 - ~/.nvm/versions/node/v14.16.0/bin/npm
npmPackages:
- babel-plugin-styled-components: ^1.12.0 => 1.12.0
- styled-components: ^5.2.2 => 5.2.2
Steps to reproduce
- Using NextJS 10.1.2 with the future flag for Webpack 5 enabled. EDIT: same error occurs even with this flag removed.
module.exports = {
future: {
webpack5: true,
strictPostcssConfiguration: true,
}
};
- Running
npm devornpm buildresults in the below error:
ReferenceError: window is not defined
at k (/home/wade/Code/Clients/rhythm-marketing/node_modules/styled-components/dist/styled-components.cjs.js:1:7161)
at e.getStyleElement (/home/wade/Code/Clients/rhythm-marketing/node_modules/styled-components/dist/styled-components.cjs.js:1:22622)
at Function.getInitialProps (/home/wade/Code/Clients/rhythm-marketing/.next/server/pages/_document.js:1148:49)
at async loadGetInitialProps (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/lib/utils.js:5:91)
at async renderToHTML (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/render.js:54:1110)
at async /home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:112:97
at async /home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:105:142
at async DevServer.renderToHTMLWithComponents (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:137:387)
at async DevServer.renderErrorToHTML (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:139:505)
at async DevServer.renderErrorToHTML (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/server/next-dev-server.js:35:1380)
at async DevServer.renderToHTML (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:138:1281)
at async DevServer.renderToHTML (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/server/next-dev-server.js:35:578)
at async DevServer.render (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:75:236)
at async Object.fn (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:59:580)
at async Router.execute (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/router.js:25:67)
at async DevServer.run (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:69:1042)
ReferenceError: window is not defined
at k (/home/wade/Code/Clients/rhythm-marketing/node_modules/styled-components/dist/styled-components.cjs.js:1:7161)
at e.getStyleElement (/home/wade/Code/Clients/rhythm-marketing/node_modules/styled-components/dist/styled-components.cjs.js:1:22622)
at Function.getInitialProps (/home/wade/Code/Clients/rhythm-marketing/.next/server/pages/_document.js:1148:49)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (node:internal/process/task_queues:94:5)
at async loadGetInitialProps (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/lib/utils.js:5:91)
at async renderToHTML (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/render.js:54:1110)
at async /home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:112:97
at async __wrapper (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/lib/coalesced-function.js:1:330)
at async DevServer.renderToHTMLWithComponents (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:137:387)
at async DevServer.renderToHTML (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:138:522)
at async DevServer.renderToHTML (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/server/next-dev-server.js:35:578)
at async DevServer.render (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:75:236)
at async Object.fn (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:59:580)
at async Router.execute (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/router.js:25:67)
at async DevServer.run (/home/wade/Code/Clients/rhythm-marketing/node_modules/next/dist/next-server/server/next-server.js:69:1042)
Expected Behavior
This error does not occur with styled-components: ^5.2.1
Actual Behavior
The above error occurs and I am unable to build my project.
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 77
- Comments: 21 (1 by maintainers)
Commits related to this issue
- rollabck styled-components to fix https://github.com/styled-components/styled-components/issues/3444 — committed to VulcanJS/vulcan-next by eric-burel 3 years ago
Same issue here, Next 10.0.9 without the Webpack 5 flag. Appears to be caused by #3415.
Fix is out in 5.2.3
v5.2.1 with next 10.1.2 works for me. Why did a minor release break things?
The error does occur when using
styled-components: ^5.2.1(which is atm being resolved to 5.2.2 in when using yarn without a lockfile)npm uninstall styled-componentsnpm install styled-components@5.2.0Did the trick for me.I had the same problem when generating the build in NextJS in version 10.0.7 with styled-components in version “^ 5.2.1”. The solution was to remove styled-components and reinstalled, fixing it in version 5.2.0.
yarn remove styled-componentsyarn add styled-components@5.2.0Server side rendering in general is busted on 5.2.2. We have rolled back to 5.2.1 and the issue is resolved.
If it helps, this was the stack trace:
Hi, I’m using this nextjs styled-components example. I also confirm the bug with
styled-components@5.2.2and installingstyled-components@5.2.1doesn’t have the bug.This was also happening on Gatsby build on Netlify, fixed by rolling back to version 5.2.1
This was the stack trace, hope this helps!
failed Building static HTML for pages - 9.641serror "window" is not available during server side rendering.> 1 | import{typeOf as e,isElement as t,isValidElementType as n}from"react-is";import r,{useState asI can’t see how I should do this, as
styled-componentsis a dependency ofredoc-cli. I raised an issue over there as well.Having the same issue here.