不能在电子中包含js文件

Cannot include js file in Electron

本文关键字:包含 js 文件 不能      更新时间:2023-09-26

在我的HTML文件中,我包含一个脚本,如下所示:

<script src="js/index.js"></script>

在我的脚本中,我尝试仅通过编写const Configuration = require("./configuration");来添加configuration.js文件。 configuration.jsindex.js 位于同一文件夹中。但在控制台上它说:

Uncaught Error: Cannot find module './configuration'

配置.js和索引.js文件都位于/app/js/文件夹中。

解决方案

是什么?例如,我可以包含 Node.js 模块,例如 Lodash。

如果您想了解需要模块时会发生什么,可以阅读文档。

乍一看,您的代码片段应该可以工作。若要在 node.js 中要求模块,请始终使用文件中的路径。

但是,在您的情况下,您只是在导入普通 JS。在这种情况下,脚本会耗尽您的 HTML 调用。

这种逻辑可能会导致很多其他问题,所以我建议你创建自己的模块。 node.js使这变得非常容易。

var configuration = {a: 1,b: 2};
module.exports = configuration;

更多阅读:

  • https://nodejs.org/dist/latest-v5.x/docs/api/modules.html
  • http://www.sitepoint.com/understanding-module-exports-exports-node-js/

在 HTML 文件中,您可以通过 require 语句将模块放在一起。

相关文章: