【问题标题】:Cannot import file in preload无法在预加载中导入文件
【发布时间】:2021-05-12 17:50:36
【问题描述】:

由于某种原因,我无法在 preload.js(电子)中导入 js 文件。 错误: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}

根据我的研究,这应该可行。将 "type": "module" 添加到 package.json 只会进一步破坏事情。

【问题讨论】:

  • 在 test.mjs 中,您不正确地加载了 'request' 模块。应该是import request from 'request';。同样,您错误地导出了Repository。应该是export default Repository;
  • @AluanHaddad 修复它,不幸的是删除最后一行会产生同样的错误

标签: javascript node.js import electron


【解决方案1】:

通过将 test.mjs 重命名为 test.js 解决了问题。

【讨论】:

    猜你喜欢
    • 2016-06-26
    • 2016-07-28
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 2019-03-18
    相关资源
    最近更新 更多