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 #36

Closed
vdhoangson opened this issue Sep 10, 2018 · 20 comments
Closed

ReferenceError: window is not defined #36

vdhoangson opened this issue Sep 10, 2018 · 20 comments
Labels
resolution:duplicate This issue is a duplicate of another issue and was merged into it.

Comments

@vdhoangson
Copy link

vdhoangson commented Sep 10, 2018

ReferenceError: window is not defined
    at Object.<anonymous> (/my_path/node_modules/@ckeditor/ckeditor5-react/dist/webpack:/CKEditor/webpack/universalModuleDefinition:10:2)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/son/Nodejs/dustgo/node_modules/babel-register/lib/node.js:152:7)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/Users/son/Nodejs/dustgo/client/modules/Blog/Post/pages/Admin/Edit.js:10:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at loader (/Users/son/Nodejs/dustgo/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/son/Nodejs/dustgo/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

How to fix it please?

@ma2ciek
Copy link
Contributor

ma2ciek commented Sep 10, 2018

Hi, @vdhoangson!

Could you describe some steps to reproduce the issue?

Thanks.

@vdhoangson
Copy link
Author

I have an article creation page

When I click the link to this page, it works fine

It only failed when I refreshed the page

@ma2ciek
Copy link
Contributor

ma2ciek commented Sep 10, 2018

Please, describe the setup, how you load the component, etc.

Without some details, it's impossible to reproduce it.

@vdhoangson
Copy link
Author

aaa.js.zip
my file here

@vdhoangson
Copy link
Author

anyone help me?

@pomek
Copy link
Member

pomek commented Sep 11, 2018

We ask you about steps to reproduce. You attached some file instead. What could we do with it? We need more details (e.g. webpack configuration, node & npm version, etc.).

Would be good if you create a repository with a minimal setup that allows reproducing the issue.

@pomek pomek added the pending:feedback This issue is blocked by necessary feedback. label Sep 11, 2018
@vdhoangson
Copy link
Author

@pomek can you add my skype?
skype: vdhoangson

@pomek
Copy link
Member

pomek commented Sep 11, 2018

I guess you don't want to share with us your project. And I understand it. That's the reason why we're asking about steps and/or a minimal set-up for reproducing the issue.

We're asking because as you said:

It only failed when I refreshed the page

That's weird and it does not sound like an error of CKEditor itself.

Answering your question - because you reported the issue here, we would like to help you here. If we resolve your problem, other developers could also resolve their issues in the future based on our messages.

@vdhoangson
Copy link
Author

I using mern stack http://mern.io/

https://github.com/Hashnode/mern-starter

And webpack config like this. Not edit anything

I was installed ckeditor-react base on document, it work when i click a tag to go to editor page, but when i refresh editor page, it show error

@pomek
Copy link
Member

pomek commented Sep 11, 2018

Before I go deeper into the details of MERN, I would like to ask you about the last thing - which version of React do you use? You can check this in node_modules/ or by typing React.version in the dev-tool console.

@vdhoangson
Copy link
Author

thanks @pomek
now i'm using react v16.4.2

@ma2ciek
Copy link
Contributor

ma2ciek commented Sep 11, 2018

Ok, I've checked it (http://mern.io/documentation.html) and it looks like it's using server-side rendering (which we do not support as our editor directly touches the DOM) You need to use some dynamic require to check if the component is rendered by the browser or by the server.

Related problems:

@pomek
Copy link
Member

pomek commented Sep 19, 2018

The case described in the ticket is about missing window object when the component is rendering on the server (SSR). A very similar ticket is here https://github.com/ckeditor/ckeditor5-react/issues/31 so I'm closing this as a DUP.

@pomek pomek closed this as completed Sep 19, 2018
@pomek pomek added resolution:duplicate This issue is a duplicate of another issue and was merged into it. and removed pending:feedback This issue is blocked by necessary feedback. labels Sep 19, 2018
@evenmed
Copy link

evenmed commented Oct 18, 2019

I ran into this same issue when trying to use Next.js with Server-Side Rendering and CKEditor.

Here's how I fixed it in case it helps anyone:

import React form 'react';

class MyComponent extends React.Component {
    state = {isServer: true};
    
    componentDidMount() {
        // SSR doesn't fire ComponentDidMount, so we import CKEditor inside of it and store it as a component prop
        this.CKEditor = require("@ckeditor/ckeditor5-react");
        this.ClassicEditor = require("@ckeditor/ckeditor5-build-classic");
        this.setState({ isServer: false }); // We just do this to toggle a re-render
    }

    render() {
        return ({this.CKEditor && (<this.CKEditor editor={this.ClassicEditor} />)})
    }
}

@bipindubey-technoark
Copy link

I ran into this same issue when trying to use Next.js with Server-Side Rendering and CKEditor.

Here's how I fixed it in case it helps anyone:

import React form 'react';

class MyComponent extends React.Component {
    state = {isServer: true};
    
    componentDidMount() {
        // SSR doesn't fire ComponentDidMount, so we import CKEditor inside of it and store it as a component prop
        this.CKEditor = require("@ckeditor/ckeditor5-react");
        this.ClassicEditor = require("@ckeditor/ckeditor5-build-classic");
        this.setState({ isServer: false }); // We just do this to toggle a re-render
    }

    render() {
        return ({this.CKEditor && (<this.CKEditor editor={this.ClassicEditor} />)})
    }
}

How to use this in functional component??

@RyanJeon
Copy link

I ran into this same issue when trying to use Next.js with Server-Side Rendering and CKEditor.
Here's how I fixed it in case it helps anyone:

import React form 'react';

class MyComponent extends React.Component {
    state = {isServer: true};
    
    componentDidMount() {
        // SSR doesn't fire ComponentDidMount, so we import CKEditor inside of it and store it as a component prop
        this.CKEditor = require("@ckeditor/ckeditor5-react");
        this.ClassicEditor = require("@ckeditor/ckeditor5-build-classic");
        this.setState({ isServer: false }); // We just do this to toggle a re-render
    }

    render() {
        return ({this.CKEditor && (<this.CKEditor editor={this.ClassicEditor} />)})
    }
}

How to use this in functional component??

This worked for me using hooks

let CKEditor;
let ClassicEditor;

useEffect(() => {
  CKEditor = require("@ckeditor/ckeditor5-react");
  ClassicEditor = require("@ckeditor/ckeditor5-build-classic");
}, []);

@Max-Power-7
Copy link

I have same problem in Next.js
¿Have solution?

@sleemkeen
Copy link

@Bajrang179
Copy link

I am getting the same issue with React version 18 (server side rendering). Can anyone help me with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
resolution:duplicate This issue is a duplicate of another issue and was merged into it.
Projects
None yet
Development

No branches or pull requests

10 participants