Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants