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

how to fix "window is not defined" when use webpack build ? #162

Closed
levi0913 opened this issue Nov 25, 2016 · 7 comments
Closed

how to fix "window is not defined" when use webpack build ? #162

levi0913 opened this issue Nov 25, 2016 · 7 comments

Comments

@levi0913
Copy link

tks

@oychao
Copy link

oychao commented Nov 29, 2016

Same question here.

@AriTheElk
Copy link

I'm having the same issue when trying to test via jest.

ReferenceError: window is not defined

  at node_modules/react-ace/node_modules/brace/index.js:18673:26
  at _acequire (node_modules/react-ace/node_modules/brace/index.js:88:37)
  at Object.acequire (node_modules/react-ace/node_modules/brace/index.js:93:26)
  at node_modules/react-ace/node_modules/brace/index.js:18671:21
  at Object.<anonymous> (node_modules/react-ace/node_modules/brace/index.js:18678:15)
  at Object.<anonymous> (node_modules/react-ace/lib/ace.js:11:14)

@ardok
Copy link

ardok commented Feb 6, 2017

thlorenz/brace#40 (comment) ==> if anyone wanna try

@securingsincity
Copy link
Owner

I want to make sure i understand. Are you all rendering on the server? Or just a traditional webpack build?

@jnoleau
Copy link

jnoleau commented Mar 8, 2017

You can easily use a stub

const Editor = (props) => {
  if (typeof window !== 'undefined') {
    const Ace = require('react-ace').default;
    require('brace/mode/javascript');
    require('brace/theme/github');

    return <Ace {...props}/>
  }

  return null;
}

//use it
<Editor mode="javascript" theme="github" value="const foo = 42;" />

And if you want isomorphic code (to remove the warning because here on server side we have null and on client side an AceEditor) you could wrap it like this :

class IsomorphicEditor extends React.Component {
  state = {mounted: false};

  componentDidMount() {
    this.setState({mounted: true});
  }

  render = () => (this.state.mounted ? <Editor {...this.props} /> : null);
}

@maxyzli
Copy link
Contributor

maxyzli commented Apr 8, 2017

@jnoleau It works. But I wonder if there is a better way to solve this problem?

@ljwagerfield
Copy link

This works but be warned: do not use variables in your require(...) statements, as this will trigger WebPack to load entire subdirectories of modules. E.g.

OK (loads one module):

require('brace/theme/github');

BAD (loads all theme modules):

const theme = 'github';
require('brace/theme/' + theme);

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

No branches or pull requests

8 participants