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.

ReferenceError: window is not defined

See original GitHub issue

Trying to use amplitude-js on node 8.8.1

I got this error when I run: const amplitude = require('amplitude-js');

ReferenceError: window is not defined
    at /root/app/node_modules/amplitude-js/amplitude.js:2427:8
    at commonjsGlobal (/root/app/node_modules/amplitude-js/amplitude.js:2:82)
    at Object.<anonymous> (/root/app/node_modules/amplitude-js/amplitude.js:5:2)
    at Module._compile (module.js:612:30)
    at Object.Module._extensions..js (module.js:623:10)
    at Module.load (module.js:531:32)
    at tryModuleLoad (module.js:494:12)
    at Function.Module._load (module.js:486:3)
    at Module.require (module.js:556:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/root/app/src/helpers/amplitude.js:3:19)
    at Module._compile (module.js:612:30)
    at Object.Module._extensions..js (module.js:623:10)
    at Module.load (module.js:531:32)
    at tryModuleLoad (module.js:494:12)
    at Function.Module._load (module.js:486:3)
    at Module.require (module.js:556:17)
    at require (internal/module.js:11:18)
...

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:12
  • Comments:19 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
mieszko4commented, Jun 19, 2018

I have server side rendering and I simply require only when not in node. In node I mock it. isServer is my custom global variable

import config from "config";

const Amplitude = !global.isServer ? require("amplitude-js") : null;

const instance = Amplitude ? Amplitude.getInstance() : {
  init: () => {},
  logEvent: () => {},
  setUserId: () => {},
  setUserProperties: () => {}
};

instance.init(config.amplitude.apiKey);

export default instance;
4reactions
wilkerluciocommented, Apr 23, 2020

I have a case that I don’t need amplitude to work, but I need to require it, can you make a way that it just doesn’t do anything on the node case, instead of blowing up?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to solve "window is not defined" errors in React and Next.js
How to solve "window is not defined" errors in React and Next.js · 1. First solution: typeof · 2. Second solution: the useEffect...
Read more >
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 don't...
Read more >
referenceerror: window is not defined, how to solve
Here's how to fix the “referenceerror: window is not defined” error that you might have in Node.js or with a tool like Next.js....
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