首页 > 解决方案 > 电子:未捕获的错误:找不到模块

问题描述

我正在构建我的第一个电子桌面应用程序,当我在一个文件中使用导入另一个文件时出现“找不到模块”错误。两个文件都在同一个文件夹中,没有拼写错误。.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>

是什么赋予了?

标签: javascriptnode.jselectron

解决方案


当你这样做

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

它在服务器上查找文件。

就像是:

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

使用Node.js's require 根据项目的文件夹结构解决它。


推荐阅读