react-editor-js icon indicating copy to clipboard operation
react-editor-js copied to clipboard

require is not defined createReactEditorJS

Open faltawy opened this issue 2 years ago • 4 comments

  • @editorjs/editorjs version: x.y.z
  • react-editor-js version: x.y.z
  • vite

Bug discription

after building Uncaught ReferenceError: require is not defined createReactEditorJS

faltawy avatar Aug 17 '22 21:08 faltawy

Hey mate, not the maintainer by any means of the imagination but you can fix this by using @originjs/vite-plugin-commonjs.

In your vite.config.(t/j)s you should have something like this:

import { defineConfig } from 'vite';
import { viteCommonjs, esbuildCommonjs } from '@originjs/vite-plugin-commonjs';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    viteCommonjs(),
  ],
  optimizeDeps:{
    esbuildOptions:{
      plugins:[
        esbuildCommonjs(['react-editor-js', '@react-editor-js/client', '@react-editor-js/server']) 
      ]
    }
  }
})

And your imports should work as something like this

// @ts-ignore: Workaround for CJS
import { createReactEditorJS } from 'react-editor-js/dist/react-editor-js.cjs';

I just got faced with the same problem and just fixed it rn.

Hope it helps, Luis Bizarro.

WildEgo avatar Aug 29 '22 13:08 WildEgo

Hey mate, not the maintainer by any means of the imagination but you can fix this by using @originjs/vite-plugin-commonjs.

In your vite.config.(t/j)s you should have something like this:

import { defineConfig } from 'vite';
import { viteCommonjs, esbuildCommonjs } from '@originjs/vite-plugin-commonjs';

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    viteCommonjs(),
  ],
  optimizeDeps:{
    esbuildOptions:{
      plugins:[
        esbuildCommonjs(['react-editor-js', '@react-editor-js/client', '@react-editor-js/server']) 
      ]
    }
  }
})

And your imports should work as something like this

// @ts-ignore: Workaround for CJS
import { createReactEditorJS } from 'react-editor-js/dist/react-editor-js.cjs';

I just got faced with the same problem and just fixed it rn.

Hope it helps, Luis Bizarro.

@WildEgo THANK YOU SO MUCH!!! Works perfect!

les19 avatar Dec 13 '22 13:12 les19

ref: https://github.com/Jungwoo-An/react-editor-js/pull/236

junkboy0315 avatar Sep 29 '23 04:09 junkboy0315

if that does not work, you can just add this to your vite config file

build: {
    commonjsOptions: { transformMixedEsModules: true }, // Change
  },

so something like this:

import { defineConfig } from "vite";
import path from "path";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "./src"),
    },
  },
  build: {
    commonjsOptions: { transformMixedEsModules: true }, // Change
  },
});

CyrusZei avatar Jan 07 '24 14:01 CyrusZei