简体   繁体   中英

Cannot import file in preload

for some reason I cannot import js files in preload.js (electron). Error: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:
preload.js:

const {ipcRenderer} = require('electron');
const repo = require("./lib/test.mjs")

window.addEventListener('DOMContentLoaded', () => {

})

test.mjs:

const request = require('request')

class Repository {
    constructor(repoUrl){
        this.repoUrl = repoUrl
        request ({
            url: this.repoUrl,
            json: true
        }, (error, response, body) =>{
            !error && response.statusCode === 200
                ? this.repoSettings = body
                : console.log(error)
        })
    }
}

module.exports = {Repository}

According to my research, this should work. adding "type": "module" to package.json only breaks things further.

Issue fixed by renaming test.mjs to test.js .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM