我通过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/