简体   繁体   English

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

[英]Electron: Uncaught Error: Cannot find module

I'm building my first electron desktop app and I'm getting a "cannot find module" error when I use require() in one file to import the other. 我正在构建我的第一个电子桌面应用程序,当我在一个文件中使用require()导入另一个文件时,出现“找不到模块”错误。 Both files are in the same folder and there's no misspelling. 两个文件都在同一个文件夹中,没有拼写错误。 .

Here's the main file index.js 这是主文件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
    ...
}

and below is the imported file datacontainer.js 下面是导入的文件datacontainer.js

var exp = module.exports = {};

exp.users = [{user1},{user2},...];

...

However, when I run the app and look at the console, it throws the following exeption; 但是,当我运行该应用程序并查看控制台时,它抛出以下异常:

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

What am I doing wrong or missing? 我在做什么错还是想念?

UPDATES: 更新:

In the index.html where the index.js is used, the error goes away if I reference the script like the following 在使用index.jsindex.html中,如果我引用如下脚本,则错误消失了

<script>
   require('./scripts/index')
</script>

but throws above error when referenced this way 但是以这种方式引用时抛出上述错误

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

what gives? 是什么赋予了?

When you do 当你做

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

it looks for the file on the server. 它在服务器上查找文件。

Something like: 就像是:

https://www.example.com/your-web-folder/scripts/index.js

Using Node.js 's require resolves it according to the folder structure of your project. 使用Node.js的require可以根据项目的文件夹结构来解决它。

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

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