gpt4 book ai didi

javascript - 不能在 Electron 中要求来自渲染器的自定义模块

转载 作者:数据小太阳 更新时间:2023-10-29 05:31:56 28 4
gpt4 key购买 nike

我正在尝试制作一些我可以导入以用于我的项目的类,但我在导入我正在制作的模块时遇到了一些问题。

我的文件结构如下所示:

├╴main.js
└╴src/
├╴html/
│ └╴index.html
├╴css/
│ └╴index.css
└╴js/
├╴index.js
└╴participant.js

所有 index.* 文件都相互关联,因此具有相同的名称。

有问题的麻烦制造者是 index.js,我的 index.html 渲染器和 participant.js

这是我得到的代码:

// index.js
const {Participant} = require("./participant");

const addNodeBtn = document.getElementById('add-node');

addNodeBtn.addEventListener('click', () => {
// this is really just filler code to see if everything works
let p = new Participant("Jason");
alert(`His name was ${p.name}`);
});

// participant.js
"use strict";

var Participant = class {
constructor(name) {
this.name = name;
}
}

module.exports.Participant = Participant;

无论出于何种原因,我不断收到错误消息“找不到模块 ./participant”。

这是我尝试过的替代方案:

require("participant");
require("./participant.js");
module.exports = Participant;
module.exports.Participant = class { ... }

都无济于事,都给我同样的错误。我什至将 index.js 重命名为其他内容,认为它与 require 机制冲突。仍然没有变化。

对解决这个问题有什么帮助吗?

更新 它似乎在我的 main.js 文件中工作。

最佳答案

好吧,结果是 index.js 一旦通过脚本标记包含在内,就会将其当前位置解析为包含 HTML 代码的位置。因此,要要求 participant.js,鉴于您的文件夹结构,您将必须使用 require('../js/participant.js') .这只会影响使用 <script> 的 HTML 页面中包含的脚本。标签。所有其他 require的将按预期工作。

关于javascript - 不能在 Electron 中要求来自渲染器的自定义模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41307822/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com