"window is not defined" error with React / Next.js
See original GitHub issuePlease follow the general troubleshooting steps first:
- You are using the latest version
- You are testing using the non-minified version of html2canvas and checked any potential issues reported in the console
Bug reports:
I’m attempting to use HTML2Canvas in a React app that’s built atop Next.js. It seems to be having trouble with the global window
variable, which is odd since I’m only importing it in the client side of the app. Here’s the code:
// Module Imports
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import html2canvas from 'html2canvas';
// Save function
handleSave = async () => {
const { chart } = this.state;
const { updateChartFunction } = this.props;
// capture screenshot
const input = document.getElementById('chartScreenshot');
html2canvas(input, {
useCORS: true,
}).then(canvas => {
const imgData = canvas.toDataURL('image/png');
console.log(imgData);
});
await updateChartFunction({ variables: { ...chart } });
};
Here’s the error:
ReferenceError: window is not defined
at \repo\client\node_modules\html2canvas\dist\html2canvas.js:6873:29
at \repo\client\node_modules\html2canvas\dist\html2canvas.js:7:85
at Object.<anonymous> (\repo\client\node_modules\html2canvas\dist\html2canvas.js:10:2)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.html2canvas (\repo\client\.next\server\static\development\pages\editchart.js:3149:18)
at __webpack_require__ (\repo\client\.next\server\static\development\pages\editchart.js:23:31)
at Module../components/EditChart.js (\repo\client\.next\server\static\development\pages\editchart.js:457:70)
at __webpack_require__ (\repo\client\.next\server\static\development\pages\editchart.js:23:31)
at Module../pages/editchart.js (\repo\client\.next\server\static\development\pages\editchart.js:2760:79)
at __webpack_require__ (\repo\client\.next\server\static\development\pages\editchart.js:23:31)
And here’s the offending line in /dist/html2canvas.js
:
CacheStorage.setContext(window);
I’m absolutely open to the idea that I’m doing something incorrectly or that “this just doesn’t work with Next.js” but I’m hoping there’s an obvious solution someone knows of!
Specifications:
- html2canvas version tested with: 1.0.0-rc.4
- Browser & version: Chrome Version 76.0.3809.132 (Official Build) (64-bit)
- Operating system: Windows 10
Issue Analytics
- State:
- Created 4 years ago
- Reactions:2
- Comments:11 (1 by maintainers)
Top Results From Across the Web
How to solve Next.js window is not defined
ReferenceError : window is not defined is a pretty common error you may run into when using Next.js for the first time but...
Read more >Window is not defined in Next.js React app - Stack Overflow
The error occurs because window is not yet available, while component is still mounting. You can access window object after component is mounted....
Read more >How to Fix "window is not defined" in Next.js - Upmostly
This issue has to do with Server-Side Rendering in Next.js. Next.js will by default try to use SSR for your site. This means...
Read more >How to fix window is not defined error in Next.js
As you can see the error occurs in the terminal itself. That means it is a compilation error. Next.js is compiled/built on Node.js...
Read more >How to solve "window is not defined" errors in React and Next.js
1. First solution: typeof · 2. Second solution: the useEffect hook · 3. Third solution: dynamic loading.
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 FreeTop 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
Top GitHub Comments
Should be fixed with #2039
this solution works for me on latest nextjs and html2canvas