0

I'm using Next.js and when I try to

import M from 'materialize-css';

I get this error:

window is undefined

2 Answers 2

1

You can only import the materialize-css on client side by using dynamic.

import dynamic from 'next/dynamic';
const M = dynamic(() => import('materialize-css'), {
    ssr: false,
});
Sign up to request clarification or add additional context in comments.

Comments

1

I added a useEffect and added this line:

     if(typeof window !== 'undefined'){
       const M = require('materialize-css');
       ...
     }
      

instead of the import statement

Comments

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.