Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: code: 'ERR_REQUIRE_ESM' #900

Open
maifeeulasad opened this issue Jun 11, 2021 · 5 comments

Comments

@maifeeulasad
Copy link

The full error looks like this, on running ``:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/maifee/Desktop/WhatApp/main.js
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1085:13) {
  code: 'ERR_REQUIRE_ESM'
}

My node version is : v14.17.0

Here is my index.js :

require = require("esm")(module)
module.exports = require("./main.js")

Here is the main.js :

import { app as application } from 'electron';
import { createWindow } from './src/window';
import { createTray } from './src/tray';

function init() {
    const app = application

    app.on('ready', ()=>{
        createWindow().then((window)=>{
            createTray(window,app)
        })
    });
    app.on('window-all-closed', () => app.quit());
}

init()

The scripts I'm also using are:

  "scripts": {    
"start": "electron -r babel-register .",    
"build": "electron-builder"  
},

Although it starts and builds, but I get error when trying to run those built files.

@borkdude
Copy link

borkdude commented Sep 3, 2021

I'm having a similar problem with electron.

@maifeeulasad
Copy link
Author

@borkdude, I couldn't still solve this problem. So I just went with the traditional approach, like this: https://github.com/maifeeulasad/WhatApp/blob/main/index.js

And everything is working fine now. Although my plan to use a 100% es6 based electron project is still a dream.

@borkdude
Copy link

borkdude commented Sep 3, 2021

@maifeeulasad I developed a library to interpret ClojureScript code from files or strings:

https://github.com/borkdude/nbb/blob/main/test-scripts/api-test/test.mjs

The library is offered as an ES Module.

I would like to use this library from Electron, but so far I didn't succeed.

@maifeeulasad
Copy link
Author

@borkdude Here is a really interesting article: https://blog.bitsrc.io/javascript-require-vs-import-47827a361b77

And here is a of answer, which you may also find helpful: https://stackoverflow.com/a/63942979/10305444

@borkdude
Copy link

borkdude commented Sep 5, 2021

@maifeeulasad I don't see an example of how to use an ES module from an Electron app, but perhaps I'm missing something.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

No branches or pull requests

2 participants