node.jselectronnodegit

Runing nodegit in Electron fails


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!!


Solution

  • 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