-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReferenceError: window is not defined #138
Comments
Are you running server-side-rendering, i.e. are you running this in node? |
I've had the same error using this package on node v6.10.3 building a static site with webpack. After a little digging the error was caused by some browser compatibility code added by this package while checking to see if
Because of this I've removed the package from the project and am using the |
I have server side rendering and I simply 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; |
is there any plan to fix that ? |
I'd like to use this package server-side, and others are no doubt doing server-side rendering. Having a dependency on Can we subvert the local storage setup if |
I am also facing the Same issue I am using Nodejs with express over ReactJs. And I am trying to use the recent react-amplitude package launched last month |
@barathkb - in my project i'm using react and redux. i created a redux middleware for dealing with amplitude. could u only use (import) amplitude on the client side ? |
@burrack Thanks for your suggestion but the things is that I want it both in my client side and the server side since I am using NextJs and Express every initial page load using URL will be pre-rendered on the server side. |
I'm having this problem still with my node express server. Some events cannot be logged on the client. Any chance this will be fixed? |
Would be awesome to fix! |
Has anyone found a fix to this? |
? |
Currently we just don’t support running in a Node environment for the JS SDK. Will consider to add the support. Thanks for providing feedbacks. |
This is sever side SDK request basically. Please refer to #164. Closing this now. |
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? |
I need to bundle amplitude-js with an npm package and finding it impossible to setup the package in a safe way so that it doesn't throw errors when consumed by an app using SSR. |
One way would be dynamic import. Wait to load the package until code is running in a client env (window is defined) let amplitude;
if (window !== undefined) {
import('amplitude-js').then(ampPackage => {
amplitude = ampPackage;
});
} The comment above has a similar solution that might be helpful as-well that only imports on the client and mocks critical API's on the server. Without knowing more about your specific constraints it's hard to provide an exact recommendation. This package is supported for client side environments only. |
On this answer, you can use
|
new amplitude ts support with nextjs https://github.com/amplitude/Amplitude-TypeScript/blob/main/examples/browser/next-app/pages/index.tsx |
Trying to use amplitude-js on node 8.8.1
I got this error when I run:
const amplitude = require('amplitude-js');
The text was updated successfully, but these errors were encountered: