You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

react-editor-js报错:Cannot call a class as a function 问题排查求助

Troubleshooting react-editor-js Issues in Ejected Electron CRA Setup

Hey Jake, sorry to hear you've been stuck on this for 2 hours—since you can't reproduce the problem in vanilla React sandboxes, the issue is almost certainly tied to your modified Electron + ejected CRA environment. Let's break down the most likely culprits and how to debug them:

1. Webpack Target & Module Resolution Conflicts

Since you changed the webpack target to electronRenderer, the bundler's module handling differs from the default browser target. Here's what to check:

  • Node.js Module Fallbacks: Some Editor.js plugins might rely on Node.js core modules (like path or fs) that aren't resolved correctly in the electronRenderer target. Open your ejected webpack config (usually config/webpack.config.js) and verify the resolve.fallback section includes fallbacks for any missing modules. For example:
    resolve: {
      fallback: {
        "path": require.resolve("path-browserify"),
        "fs": false // Or use a polyfill if needed
      }
    }
    
  • Transpilation of Editor.js Plugins: Ensure your babel-loader is processing Editor.js plugin files. Add the plugin package names to the include array in your webpack rules for JavaScript/JSX files:
    module: {
      rules: [
        {
          test: /\.(js|jsx)$/,
          include: [
            path.resolve(__dirname, "src"),
            /react-editor-js/,
            /@editorjs\/.*/ // Include all official Editor.js plugins
          ],
          loader: "babel-loader"
        }
      ]
    }
    

2. Dependency Version Mismatches

Even if you installed all plugins, version conflicts between react-editor-js, @editorjs/editorjs, and your Electron dependencies could break things:

  • Verify compatibility between react-editor-js and @editorjs/editorjs: Check the react-editor-js package docs to confirm it supports your installed version of @editorjs/editorjs (e.g., v2.x of react-editor-js typically requires v2.26+ of Editor.js).
  • Check for duplicate dependencies: Run yarn why @editorjs/editorjs to see if multiple versions of the core Editor.js library are being installed. If so, use resolutions in your package.json to force a single version:
    "resolutions": {
      "@editorjs/editorjs": "2.28.2" // Use your desired version
    }
    

3. Electron Context Isolation & Node Integration

Electron's security features might block Editor.js from accessing necessary global objects:

  • Temporarily disable context isolation (for debugging only—don't use in production) in your main process BrowserWindow config to see if that fixes the issue:
    const mainWindow = new BrowserWindow({
      webPreferences: {
        contextIsolation: false,
        nodeIntegration: true
      }
    });
    
  • If this resolves the problem, you'll need to use a preload script to safely expose required APIs to the renderer process instead of disabling context isolation long-term.

4. Data Parsing & Tool Configuration Issues

Double-check your data and tool setup, as environment differences might expose edge cases not present in sandboxes:

  • Validate your parsed data: Add console.log(JSON.parse(text)) before passing it to <EditorJS /> to ensure the data structure matches what Editor.js expects (e.g., each block has a valid type matching a registered tool, and no missing required fields).
  • Simplify your toolset: Test with only a basic tool like Paragraph first to rule out a problematic plugin. If it works, re-add plugins one by one to identify the culprit.

5. Ejected CRA Optimization Settings

Ejected CRA's default optimizations might be tree-shaking Editor.js plugins incorrectly:

  • Disable tree-shaking temporarily in your webpack config to test:
    optimization: {
      usedExports: false
    }
    
  • If that fixes it, mark Editor.js plugins as having side effects in your package.json to prevent them from being eliminated:
    "sideEffects": [
      "node_modules/@editorjs/**/*.js",
      "node_modules/react-editor-js/**/*.js"
    ]
    

Quick Debugging Tips

  • Open Electron's developer tools (Ctrl+Shift+I) and check the Console tab for errors like missing modules, undefined tools, or global access issues—these will give you direct clues.
  • Log your EditorJSTools object to confirm all plugins are properly imported and initialized (no undefined entries).

内容的提问来源于stack exchange,提问作者Jake

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 12:42:30