node.js - 在Electron中运行nodegit失败

标签 node.js electron nodegit

我通过nodegit 0.26.5安装了npm并将软件包导入了我的Electron应用程序的渲染器部分。在编译期间,我在下面收到此错误:

WARNING in ./node_modules/nodegit/dist/nodegit.js
Module not found: Error: Can't resolve '../build/Release/nodegit.node' in '/Users/steve/Documents/git/git_reader/node_modules/nodegit/dist'

ERROR in ./node_modules/nodegit/dist/nodegit.js
Module not found: Error: Can't resolve '../build/Debug/nodegit.node' in '/Users/steve/Documents/git/git_reader/node_modules/nodegit/dist'
ERROR in ./node_modules/nodegit/dist/nodegit.js
Module not found: Error: Can't resolve '../package' in '/Users/steve/Documents/git/git_reader/node_modules/nodegit/dist'
在我的node_modules/nodegit/build目录中,只有一个Release目录。有人知道我在这里想念的吗?
我创建了一个存储库,它是从样板模板中 fork 的。我只添加了nodegit@types/nodegit作为依赖项,并将其导入details.component.ts
https://github.com/Githubber2021/electron-nodegit-error
git clone https://github.com/Githubber2021/electron-nodegit-error.git
npm install
npm run electron:local
重现该问题。任何人都可以在他们的机器上重现错误吗?我在这里想念什么?非常感谢您的帮助或提示!!

最佳答案

我正在使用nodegit 0.27,而对我来说,错误消息略有不同,这是

nodegit.js:1088 Uncaught Error: Cannot find module '../package'
请注意,我将webpack与 Electron 锻造一起使用,在package.json中,我具有以下内容。 (我将不相关的部分替换为...)
{
    "name": ...
    "version": ...
    "description": ...
    "config": {
        "forge": {
            "packagerConfig": {},
            "makers": [
                ...
            ],
            "plugins": [
                [
                    "@electron-forge/plugin-webpack",
                    {
                        "mainConfig": ...
                        "renderer": {
                            "config": "./webpack.renderer.config.js",
                            "entryPoints": [
                                ...
                            ]
                        }
                    }
                ]
            ]
        }
    },
    ...
}
在我的webpack.renderer.config.js中,我需要在导出文件中添加一个externals字段,以便它们看起来像这样
module.exports = {
    ...
    externals: {
        nodegit: 'commonjs nodegit'
    },
};
然后在我的main.ts中,我拥有的主要过程
(global as any).mynodegit = require('nodegit');
然后,我使用IPC在渲染器进程中访问nodegit。请注意,@ts-ignore是必需的,以禁止来自 typescript 的错误消息。
import { remote } from "electron";
// @ts-ignore
import * as Git from "@types/nodegit";
// @ts-ignore
const Git = remote.getGlobal('mynodegit');
这是帮助我完成此https://github.com/newblord/electron-react-webpack-nodegit-boilerplate的样板

关于node.js - 在Electron中运行nodegit失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62975911/

相关文章:

javascript - node.js async.each 回调,我怎么知道它什么时候完成?

javascript - Node.js Express 服务器 - 全局变量问题

javascript - 来自同一 require(js 文件)的多个导出类

javascript - Electron webview 正在阻止 http 图像

javascript - angular-cli.json 在添加 style.css 时创建错误

node.js - 使用 NodeGit 读取 Git 配置变量

angularjs - Liferay7.0 portlet build with angular and typescript

javascript - 要求 Electron 中的视点卡住 Electron 实例

javascript - 如何使用 nodegit 重置暂存和未暂存的更改

javascript - Nodegit 克隆不起作用?