nhn / tui.image-editor

🍞🎨 Full-featured photo image editor using canvas. It is really easy, and it comes with great filters.
http://ui.toast.com/tui-image-editor
MIT License
6.84k stars 1.26k forks source link

error - ReferenceError: self is not defined in node_modules\tui-image-editor\dist\tui-image-editor.js:15 #837

Open aymeric75 opened 1 year ago

aymeric75 commented 1 year ago

Describe the bug

This line : import ImageEditor from '@toast-ui/react-image-editor';

creates this error:

error - ReferenceError: self is not defined
    at Object.<anonymous> (C:\Users\...\nextjsgenerator\node_modules\tui-image-editor\dist\tui-image-editor.js:15:4)

In my NextJs / Typescript project. It's a new fresh project, with no other code added

To Reproduce

Content of my package.json file


{
  "name": "nextjsgenerator",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@next/font": "13.1.5",
    "@toast-ui/react-image-editor": "^3.15.2",
    "@types/node": "18.11.18",
    "@types/react": "18.0.27",
    "@types/react-dom": "18.0.10",
    "eslint": "8.32.0",
    "eslint-config-next": "13.1.5",
    "next": "13.1.5",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "typescript": "4.9.4"
  }
}

Content of my tsconfig.json file :

{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "baseUrl": ".",
    "paths": {
      "@/*": ["./*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}

To be noted, since I am using NextJS which advises to use React 18. I had to npm i @toast-ui/react-image-editor --force ...

Regards

lja1018 commented 1 year ago

@aymeric75 Thank you for the information. I'll check.

lengthtail commented 1 year ago

By using Next.js dynamic import you can get around this problem.

// create editor component as README.md says,  then...
import dynamic from "next/dynamic";

const ImageEditor = dynamic(() => import("@/components/editor"), {
  ssr: false,
  loading: () => <p>Loading...</p>,
});

export default function Page() {
  return (
      <ImageEditor />
  )
}
SalahAdDin commented 1 year ago

By using Next.js dynamic import you can get around this problem.

// create editor component as README.md says,  then...
import dynamic from "next/dynamic";

const ImageEditor = dynamic(() => import("@/components/editor"), {
  ssr: false,
  loading: () => <p>Loading...</p>,
});

export default function Page() {
  return (
      <ImageEditor />
  )
}

we are having this issue with Jest testing a React app, how can we solve it?