繁体   English   中英

电子:未捕获错误:找不到模块

[英]Electron: Uncaught Error: Cannot find module

我正在构建我的第一个电子桌面应用程序,当我在一个文件中使用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.jsindex.html中,如果我引用如下脚本,则错误消失了

<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的require可以根据项目的文件夹结构来解决它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM