next.jsmaterialize

window is undefined in next js when using materialize css


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

import M from 'materialize-css';

I get this error:

window is undefined


Solution

  • I added a useEffect and added this line:

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

    instead of the import statement