standard-things / esm

Tomorrow's ECMAScript modules today!
Other
5.26k stars 147 forks source link

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

Open maifeeulasad opened 3 years ago

maifeeulasad commented 3 years ago

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 commented 2 years ago

I'm having a similar problem with electron.

maifeeulasad commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

@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 commented 2 years ago

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