【问题标题】:ElectronJS: Renderer can't resolve module require("./index.js") over HTTPElectronJS:渲染器无法通过 HTTP 解析模块 require("./index.js")
【发布时间】:2021-01-10 22:40:03
【问题描述】:

我的文件夹结构是这样设置的:

assets/
  logo.png
  other_web_assets.png
electron/
  node_modules
  main.js
  package-lock.json
  package.json
src/
  index.js
  otherJavascriptFiles.js
index.html
style.css

我将 ElectronJS 与网站分开的原因是该网站旨在通过 HTTP 提供服务(但它不应该在没有 Electron 的情况下工作,我只需要用于 Firebase 身份验证的 HTTP 协议)。目前我正在通过端口 5500 上的 LiveServer VSCode 扩展为 index.html 提供服务。

在我的index.html 中,我使用require("./src/index.js") 加载index.js,在index.js 文件中,我使用require 与其他javascript 文件和其他节点模块进行通信。

但是,require("./src/index.js")index.html 中不起作用,我总是在开发工具控制台中收到错误Uncaught Error: Cannot find module './src/index'。 我尝试使用<script src="./src/index.js"></script> 导入index.js,但这会导致相同的错误,我的index.js 尝试使用require 导入不同的javascript 文件。

但是,我可以要求正常的节点模块(例如 require("request"))而不会出现任何错误。

有没有办法解决这个问题?

【问题讨论】:

    标签: javascript html node.js http electron


    【解决方案1】:

    从我使用的 js 文件中导入函数

    import {functionName} from 'path to js file where function is defined'
    

    我对图片等使用Require,但对js文件不使用。

    我建议你使用导入。另外,使用 export 一词定义您需要导入其他文件的函数

    【讨论】:

    • 我刚刚试了一下,现在开发工具中有一个错误提示“Uncaught SyntaxError: Cannot use import statement outside a module”
    • 谢谢,成功了!我只需要将导出从“exports.variable = variable”更改为“export default { variable }”
    猜你喜欢
    • 2017-01-15
    • 2019-03-10
    • 2017-07-08
    • 2023-02-16
    • 2021-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多