question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

window is undefined

See original GitHub issue

I created an entire new project with a server.js containing a single line:

var chartjs = require('chart.js');

and a package.json with a dependency on chart.js. Running node server.js produces the following error:

alex@alex-pc ~/repos/temp2 $ node server.js 
/home/alex/repos/temp2/node_modules/chart.js/Chart.js:668
            return window.requestAnimationFrame ||
                   ^
ReferenceError: window is not defined
    at /home/alex/repos/temp2/node_modules/chart.js/Chart.js:668:11
    at Object.<anonymous> (/home/alex/repos/temp2/node_modules/chart.js/Chart.js:676:5)
    at Object.<anonymous> (/home/alex/repos/temp2/node_modules/chart.js/Chart.js:2021:4)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/home/alex/repos/temp2/server.js:1:77)

Clearly the problem is mine, because if this was a universal problem then nobody would be able to use this package at all. Anybody got any ideas?

Node v0.12.7 npm v2.11.3

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
santigo171commented, Jul 22, 2021

Hi! I’m using chart.js in a Node.js project, not a browser. My idea is save a chart in an png. I use node-canvas dependency to do it. And I get the same error than you.

I solved it by using:

const Chart = require("chart.js"); // Just importing module
Chart.defaults.animation = false; // Solution

Hope this could be useful for someone.

1reaction
philiiiiiippcommented, Aug 30, 2016

If you use webpack like me, then in your webpack.config.js for your server so that

require('chart.js') gets transpiled to something else and not imported serverside.

Of course you have to make sure that chart.js is then only executed on the client side (if you use react then componentDidMount() would be a candidate)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript: 'window' is not defined - Stack Overflow
The window object represents an open window in a browser. Since you are not running your code within a browser, but via Windows...
Read more >
How to solve "window is not defined" errors in React and Next.js
js world, window is not defined, window is only available in browsers. There are three ways to solve that: 1. First solution: typeof....
Read more >
ReferenceError: window is not defined in JavaScript
The "ReferenceError: window is not defined" error occurs for multiple reasons: ... The window represents a window containing a DOM document and is...
Read more >
How To Solve ReferenceError window is not defined in ...
Fixing a window is not defined error can be quite simple. In most cases, all you will need to do is wrap your...
Read more >
[Solved] ReferenceError : window is not defined - ItsJavaScript
The ReferenceError : window is not defined error mainly occurs if you are using the window object in Node.js, React.js, Next.js.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found