Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReferenceError: window is not defined #1987

Closed
ochanje210 opened this issue Aug 21, 2017 · 18 comments · Fixed by #1990
Closed

ReferenceError: window is not defined #1987

ochanje210 opened this issue Aug 21, 2017 · 18 comments · Fixed by #1990
Labels

Comments

@ochanje210
Copy link

ochanje210 commented Aug 21, 2017

From the latest version: 0.71.5, server-side rendering produces following issue

/var/app/current/node_modules/semantic-ui-react/dist/commonjs/behaviors/Visibility/Visibility.js:260
  context: window,
           ^
ReferenceError: window is not defined
    at Object.<anonymous> (/var/app/current/node_modules/semantic-ui-react/dist/commonjs/behaviors/Visibility/Visibility.js:260:12)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/var/app/current/node_modules/semantic-ui-react/dist/commonjs/behaviors/Visibility/index.js:8:19)
    at Module._compile (module.js:569:30)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: `NODE_ENV=production node build/server`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
@layershifter
Copy link
Member

PRs are welcome. We already have isBrowser function to recognize if SSR is used.

@lottamus
Copy link
Contributor

Looks like the issue is coming from this commit: e350886

Perhaps we should wrap the defaultProps "context": window with an "isBrowser" check

@uicomponent
Copy link

uicomponent commented Aug 21, 2017

+1

ChrisNLott thanks for help!

I commented all changes from this commit (+two related comments in Sticky.js and Visibility.js) and my project start to work.

Hope it can help somebody)

@kien8995
Copy link

Thank @ilinyevg, I try this and it work !

const hasWindow =
typeof window === 'object' && window !== null && window.self === window;

Visibility.defaultProps = {
  context: hasWindow ? window : {},
  continuous: false,
  once: true
};

@arthur-nesterenko
Copy link

I have wrote temporary script for fix this, perhaps for somebody will be useful

const fs = require( 'fs' );

const visibilityFile = 'node_modules/semantic-ui-react/dist/commonjs/behaviors/Visibility/Visibility.js';
const stickyFile = 'node_modules/semantic-ui-react/dist/commonjs/modules/Sticky/Sticky.js';

fs.readFile( visibilityFile, 'utf8', function ( err, data ) {
  if ( err ) {
    return console.error( err );
  }
  const result = data.replace( 'context: window', 'context: typeof window === \'object\' && window !== null && window.self === window ? window : {}' );

  fs.writeFile( visibilityFile, result, 'utf8', function ( err ) {
    if ( err ) return console.error( err );
  } );
} );

fs.readFile( stickyFile, 'utf8', function ( err, data ) {
  if ( err ) {
    return console.error( err );
  }
  const result = data.replace( 'scrollContext: window', 'scrollContext: typeof window === \'object\' && window !== null && window.self === window ? window : {}' );

  fs.writeFile( stickyFile, result, 'utf8', function ( err ) {
    if ( err ) return console.error( err );
  } );
} );

@WoLfulus
Copy link

How's this going? Everything broke here :(

@ochanje210
Copy link
Author

Falling back to 0.71.3 should be fine until this is fixed.

@WoLfulus
Copy link

WoLfulus commented Aug 27, 2017

I'm in a loop xD
Some features I use are broken in .3

I'll try the workarounds

@Maxhodges
Copy link

Maxhodges commented Aug 27, 2017

I'm getting an error related to "window is not defined" when deploying to gatsby.
https://www.gatsbyjs.org/docs/debugging-html-builds/

I see multiple issues about this here and lots of discussion. Is there any consensus regarding a solution? if so should we add something to the docs?

@layershifter
Copy link
Member

Just wait until #1990 will be merged and released in new version.

@prateekrastogi
Copy link

@layershifter Any eta on when project maintainers are planning to do so?

@levithomason
Copy link
Member

I do my best to make a release every weekend. Sometimes I don't cause of life and free software :)

@ddenysov
Copy link

ddenysov commented Sep 5, 2017

I still have this error in 0.72.0

scrollContext: typeof window === 'object' && window !== null && window.self === window ? window : {},

The code above fixed problem

@lookagit
Copy link

node V7.9.0
npm V4.2.0

@levithomason
Copy link
Member

If users are having issues on the latest release, please open a new issue if there is not one already. The fields in the issue template are necessary for us to debug and ship fixes.

@curran
Copy link

curran commented Oct 21, 2017

Experiencing this error with SSR on version 0.75.1.

@moshest
Copy link

moshest commented Nov 15, 2017

I add to webpack on the server side the following plugin:

  plugins: [
    // Define free variables
    // https://webpack.js.org/plugins/define-plugin/
    new webpack.DefinePlugin({
      window: undefined,
    }),
  ],

@Semantic-Org Semantic-Org locked and limited conversation to collaborators Dec 10, 2017
@levithomason
Copy link
Member

We're happy to look at new issues posted against the latest version 0.77.0.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.