不能在 Electron 中包含 js 文件
Cannot include js file in Electron
在我的 HTML 文件中,我包含了如下脚本:
<script src="js/index.js"></script>
在我的脚本中,我尝试通过编写 const Configuration = require("./configuration");
添加一个 configuration.js
文件。 configuration.js
与 index.js
在同一文件夹中。但在控制台上它说:
Uncaught Error: Cannot find module './configuration'
configuration.js 和 index.js 文件都在 /app/js/
文件夹中。
有什么解决方案?我可以包含 Node.js 模块,例如 Lodash。
如果你想了解当你需要一个模块时发生了什么,你可以阅读 the docs。
乍一看,您的代码片段应该可以工作。要在 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 语句将模块组合在一起。
在我的 HTML 文件中,我包含了如下脚本:
<script src="js/index.js"></script>
在我的脚本中,我尝试通过编写 const Configuration = require("./configuration");
添加一个 configuration.js
文件。 configuration.js
与 index.js
在同一文件夹中。但在控制台上它说:
Uncaught Error: Cannot find module './configuration'
configuration.js 和 index.js 文件都在 /app/js/
文件夹中。
有什么解决方案?我可以包含 Node.js 模块,例如 Lodash。
如果你想了解当你需要一个模块时发生了什么,你可以阅读 the docs。
乍一看,您的代码片段应该可以工作。要在 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 语句将模块组合在一起。