0

I have an Electron app integrated with Flask server. The app has both HTML loaded by Flask server (flask-page.html ES6) and Electron renderer (render-page.html NodeJS).

I want to share a class defined in a file for Flask and Renderer to avoid duplicated code, as class is exactly the same, but with different export/import.

Now, it's defined in 2 files as followings:

sharedclass-nodejs.js

    class sharedClass {
      sameMethod() {

      }
    }

    module.exports = {sharedClass}

sharedclass-es6.js

    export class sharedClass {
      sameMethod() {

      }
    }

load-flask.js

    import {sharedClass} from "sharedclass-es6.js"

flask-page.html (Flask server)

    <script type="module" src="sharedclass-es6.js"></script>
    <script type="module" src="load-flask.js"></script>

load-render.js

    const {sharedClass} = require("sharedclass-nodejs.js")

render-page.html (Electron render UI)

    <script src="sharedclass-nodejs.js"></script>

Is it possible to define 1 copy of "class sharedClass" in 1 file, such as NodeJS style export, and use it in ES6?

user3792705
  • 577
  • 3
  • 18
  • You can import something exported with `module.exports` just the same way you do with something exported with `export` – Arkellys Jan 24 '23 at 09:43
  • The import does not work in ES6 js if exported as module.exports, which is commonJS style (NodeJS). – user3792705 Jan 24 '23 at 19:44
  • I do it on my app, and it works fine with the correct babel configuration. – Arkellys Jan 24 '23 at 19:54
  • Babel configuration is what I am looking for. Can you give some details? Thanks. – user3792705 Jan 24 '23 at 20:03
  • Does this answer your question? [How can I use Node.js CommonJS modules in ES6 import/export modules using babel or webpack?](https://stackoverflow.com/questions/69472430/how-can-i-use-node-js-commonjs-modules-in-es6-import-export-modules-using-babel) – Arkellys Jan 25 '23 at 08:27

0 Answers0