Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

239
Views
Webpack rendering: "window is not defined"

I use MERN – https://github.com/Hashnode/mern-starter (react, redux, webpack, nodejs, express) And component react-sound – https://github.com/leoasis/react-sound

When I include component

import Sound from 'react-sound';

and try start the server, I have "window is not defined" error from webpack server rendering.

But if I comment this line and start the server, all will be fine. After that I can uncomment this line and component will be work, because when the server is running, changes don't triggered server rendering (only front rendering).

If I try

if (typeof window !== 'undefined') {
    const Sound = require('react-sound');
}

And in render

render() {
    return (
        <div>
            <Sound playStatus={Sound.status.STOPPED} url="" />
        </div>
    );
}

I have ReferenceError: Sound is not defined error on front rendering (after webpack).

UPDATE:

If I try (var, not const)

if (typeof window !== 'undefined') {
    var Sound = require('react-sound');
}

I have TypeError: Cannot read property 'status' of undefined error on front rendering.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Seems that you can't use react-sound outside of a browser environment.

The solution can be as follows:

let SoundEl;
if (typeof window !== 'undefined') {
  import Sound from 'react-sound';
  SoundEl = <Sound playStatus={Sound.status.STOPPED} url="" />
} else {
  SoundEl = <div></div>
}

...

render() {
  return (
    <div>
      {SoundEl}
    </div>
  )
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!