Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GatsbyJS - build show error window is not defined (AOS)

I'm new with GatsbyJS and ReactJS and i have a problem when I build my project. The problem is whenever I build the project with AOS inside it, it show error window is not defined. It is totally fine when i run in develop mode.

enter image description here

And this is my code:

enter image description here

I read that componentDidMount() should fix the problem. Unfortunately, it does not work for me.

Any help? Thank you..

like image 317
Tommy Saputra Avatar asked Mar 07 '23 05:03

Tommy Saputra


1 Answers

I changed my code to:

import React from "react";
import Link from "gatsby-link";

import whiteLogo from "../img/tm_logo_white.png";
import myImg from "../img/me.jpg";

class IndexPage extends React.Component {

    componentDidMount() {
        const isBrowser = typeof window !== "undefined";
        const AOS = isBrowser ? require("aos") : undefined;

        this.aos = AOS;
        this.aos.init();
    }

    componentDidUpdate() {
        this.aos.refresh();
    }
}

Deleted import AOS from "aos" at the top, and everything works fine for me.

like image 191
Tommy Saputra Avatar answered Mar 19 '23 18:03

Tommy Saputra