javascript - 电子:未捕获的错误:找不到模块
问题描述
我正在构建我的第一个电子桌面应用程序,当我在一个文件中使用导入另一个文件时出现“找不到模块”错误。两个文件都在同一个文件夹中,没有拼写错误。.require()
这是主文件index.js
const app = require('electron')
const store = require('./datacontainer') //Here I import the other file
if(store.users.length==0) { // throws exception: store is not defined
...
}
下面是导入的文件datacontainer.js
var exp = module.exports = {};
exp.users = [{user1},{user2},...];
...
但是,当我运行应用程序并查看控制台时,它会抛出以下异常;
Uncaught Error: Cannot find module './datacontainer'
at Module._resolveFilename (module.js:543)
at Function.Module._resolveFilename (C:\mm_apps\report-viewer\node_modules\electron\dist\resources\electron.asar\common\reset-search-paths.js:35)
at Function.Module._load (module.js:473)
at Module.require (module.js:586)
at require (internal/module.js:11)
at index.js:10
我做错了什么或错过了什么?
更新:
在使用的index.html
地方index.js
,如果我像下面这样引用脚本,错误就会消失
<script>
require('./scripts/index')
</script>
但是以这种方式引用时会引发上述错误
<script src="./scripts/index.js"></script>
是什么赋予了?
解决方案
当你这样做
<script src="./scripts/index.js"></script>
它在服务器上查找文件。
就像是:
https://www.example.com/your-web-folder/scripts/index.js
使用Node.js
's require 根据项目的文件夹结构解决它。
推荐阅读
- angular - 如何处理输入类型 =“文本”的 keyup 事件
- angular - 如何在 Angular 5 中获取 URI 的第一部分?
- timepicker - TimePicker - 将 minTime 设置为大于选定的开始时间
- c# - FindElementByXPath 不会在页面外找到元素
- node.js - 您应该在 Kubernetes 中使用 PM2、节点集群还是两者都不使用?
- git - Git:总结一系列提交中的更改
- unity3d - 如何做一个 Unity 拖放单元
- android - Android Studio 3.2 空测试套件
- java - Spring中的Kotlin setter依赖注入
- lua - 在表中动态构建子表