1

I get the following error when i do "npm run build" :

WebpackError: ReferenceError: window is not defined

this is a part of code that causes an error:

    params.slidesPerView =  Math.floor(window.innerWidth/400);
    // params.slidesPerGroup = Math.floor(window.innerWidth/400);
    console.log("Calc Width");
    console.log(window.innerWidth/400);
    this.lastWidth = params.slidesPerView;

1 Answer 1

2

During your development, you can access to window in client. When you run build, gatsby renders these components on the server where window is not defined.

Solution is the use window in componentDidMount or check window before you want use it.

Sign up to request clarification or add additional context in comments.

3 Comments

"componentDidMount" How to use it ?
Read number 1 in above link in gatsby documention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.