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?

5
  • You can import something exported with module.exports just the same way you do with something exported with export Commented Jan 24, 2023 at 9:43
  • The import does not work in ES6 js if exported as module.exports, which is commonJS style (NodeJS). Commented Jan 24, 2023 at 19:44
  • I do it on my app, and it works fine with the correct babel configuration. Commented Jan 24, 2023 at 19:54
  • Babel configuration is what I am looking for. Can you give some details? Thanks. Commented Jan 24, 2023 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? Commented Jan 25, 2023 at 8:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.