不能包括在电子
问题描述:
js文件在我的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/
文件夹中。
什么是解决方案?我可以包含像Lodash这样的Node.js模块。
答
如果你想了解当你需要一个模块时发生了什么,你可以阅读the docs。
在第一次看,你的代码片段应该工作。要在node.js中要求模块,您始终使用文件中的路径。
但是,就你而言,你只是导入纯JS。在这种情况下,脚本将耗尽您的HTML调用。
该逻辑可能会导致很多其他问题,所以我建议您创建自己的模块。 node.js使这非常简单。
var configuration = {a: 1,b: 2};
module.exports = configuration;
更多阅读AOUT说:
- https://nodejs.org/dist/latest-v5.x/docs/api/modules.html
- http://www.sitepoint.com/understanding-module-exports-exports-node-js/
里面你的HTML文件,你可以把你的模块一起通过要求陈述。
change require(“./ js/configuration”);也许完成了。 – AhbapAldirmaz
@AhbapAldirmaz它的工作,实际上。我不明白为什么。如果您可以添加答案,我会将其标记为正确。 **编辑**:哦,我猜路径是相对于html文件的。解决了它。 – nope