0

I installed nodegit 0.26.5 via npm and import the package in the renderer part of my Electron application. During compilation I receive this error below:

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'

In my node_modules/nodegit/build directory, I only have a Release directory. Does anyone have an idea what I miss here?

I created a repo, which I forked from a boilerplate template. I only added nodegit and @types/nodegit as a dependency and imported it in 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

to reproduce the issue. Can anyone reproduce the error on their machine? What am I missing here? Thank you so much for any help or hint!!

Daniel Stephens
  • 2,371
  • 8
  • 34
  • 86

1 Answers1

1

I'm using nodegit 0.27 and the error message for me was slightly different, it was

nodegit.js:1088 Uncaught Error: Cannot find module '../package'

Note that I'm using webpack together with electron forge and in my package.json I have the following. (I replaced the irrelevant parts with ...)

{
    "name": ...
    "version": ...
    "description": ...
    "config": {
        "forge": {
            "packagerConfig": {},
            "makers": [
                ...
            ],
            "plugins": [
                [
                    "@electron-forge/plugin-webpack",
                    {
                        "mainConfig": ...
                        "renderer": {
                            "config": "./webpack.renderer.config.js",
                            "entryPoints": [
                                ...
                            ]
                        }
                    }
                ]
            ]
        }
    },
    ...
}

And in my webpack.renderer.config.js I needed to add an externals field to my exports so that they look something like this

module.exports = {
    ...
    externals: {
        nodegit: 'commonjs nodegit'
    },
};

Then in my main.ts, the main process I have

(global as any).mynodegit = require('nodegit');

And then I use IPC to access nodegit within my renderer process. Note that the @ts-ignore is required to suppress error messages from typescript.

import { remote } from "electron";
// @ts-ignore
import * as Git from "@types/nodegit";
// @ts-ignore
const Git = remote.getGlobal('mynodegit');

Here is the boilerplate that helped me with this https://github.com/newblord/electron-react-webpack-nodegit-boilerplate

Artur Kovacs
  • 141
  • 5
  • Since then I realized that this only works in development mode (i.e. `electron-forge start`) but it does not work in release mode (`electron-forge make`) At least for me it doesn't so I would be curious to know if you could get it to work in release mode. And if so, how? – Artur Kovacs Nov 20 '20 at 06:52
  • I got it working now in production mode. Two points on this. 1st, being aware that node's ABI is incompatible/different than the node version of Electron (even if the node version is the same). 2nd, for any future readers, don't use `pnpm` when using `nodegit` and Electron. The symlinks cause that only have of the files are properly packed – Daniel Stephens Nov 20 '20 at 17:04