nodegit: Module not found: Error: Can't resolve '../build/Debug/nodegit.node'

I am getting these errors when using this library

The Debug folder isn’t even there. Anyone have any idea?

I am using Node 6.10.2 and npm 3.10.10:

ERROR in ./~/nodegit/dist/nodegit.js
Module not found: Error: Can't resolve '../package' in 'C:\Users\Dolan\Documents\Git-Proton\node_modules\nodegit\dist'
 @ ./~/nodegit/dist/nodegit.js 979:18-39
 @ ./src/app/common/git/git.service.ts
 @ ./src/app/common/git/git.module.ts
 @ ./src/app/app.module.ts
 @ ./src/main.ts
 @ multi ./src/main.ts

ERROR in ./~/nodegit/dist/nodegit.js
Module not found: Error: Can't resolve '../build/Debug/nodegit.node' in 'C:\Users\Dolan\Documents\Git-Proton\node_modules\nodegit\dist'
 @ ./~/nodegit/dist/nodegit.js 18:11-49
 @ ./src/app/common/git/git.service.ts
 @ ./src/app/common/git/git.module.ts
 @ ./src/app/app.module.ts
 @ ./src/main.ts
 @ multi ./src/main.ts

image

About this issue

  • Original URL
  • State: open
  • Created 7 years ago
  • Reactions: 7
  • Comments: 25

Commits related to this issue

Most upvoted comments

Having the same issue in Mac OSX 10.12.6, using Electron. Log:

┏ Renderer Process -----------

  Hash: 5e2f705eccaf53da0ab1
  Version: webpack 2.7.0
  Time: 8372ms
        Asset     Size  Chunks  Chunk Names
  renderer.js  3.09 MB       0  renderer
   styles.css   142 kB       0  renderer
  
  WARNING in ./~/promisify-node/index.js
  107:14-27 Critical dependency: the request of a dependency is an expression
  
  ERROR in ./~/nodegit/dist/nodegit.js
  Module not found: Error: Can't resolve '../build/Debug/nodegit.node' in '/Users/christian/Desktop/projects/FreshProject/node_modules/nodegit/dist'
   @ ./~/nodegit/dist/nodegit.js 18:11-49
   @ ./~/babel-loader/lib!./~/vue-loader/lib/selector.js?type=script&index=0!./src/renderer/components/Index.vue
   @ ./src/renderer/components/Index.vue
   @ ./src/renderer/router/index.js
   @ ./src/renderer/main.js
   @ multi ./.electron-vue/dev-client ./src/renderer/main.js
  Child html-webpack-plugin for "index.html":
           Asset     Size  Chunks  Chunk Names
      index.html  1.45 MB       0  

┗ ----------------------------

I also encountered this error message when using webpack to pack up (not during run-time though).
I figured out that commenting out the line importing debug version of nodegit.node helps webpack bundle successfully.
But I wonder what is the purpose of loading debug version of nodegit.node and if it is the right thing to ignore it in webpack.

Project Configuration

Here is part of my package.json:

  "dependencies": {
    "nodegit": "^0.26.5",
  },
  "devDependencies": {
    "electron": "^8.2.5",
    "node-loader": "^1.0.2",
    "webpack": "^4.46.0",
  },

Not sure if it matters, my node version is 12.20 and I installed nodegit with prebuilt binaries targeting windows x64.
You may see that I installed electron, webpack, and a package named node-loader. The usage of the last package will be explained later.

And here is my webpack configuration for the main process of electron:

const main = {
  entry: path.resolve(__dirname, './main.js'),
  output: {
    filename: 'main.js',
    path: distPath,
  },
  target: 'electron-main',
  node: {
    __dirname: false,
  },
  module: {
    rules: [
      {
        test: /\.node$/,
        loader: 'node-loader',
      },
    ],
  },
};

I require('nodegit'); in main.js to force webpack to bundle it to test if it can be bundled successfully for main process of electron.

Problem Description

Before the node-loader package is installed, webpack fails to pack release version of nodegit.node with the following error message:

WARNING in ./node_modules/nodegit/build/Release/nodegit.node 1:2
Module parse failed: Unexpected character '�' (1:2)
    You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
    (Source code omitted for this binary file)
     @ ./node_modules/nodegit/dist/nodegit.js 12:11-51
     @ ./main.js

Because line 12 of nodegit.js is actually catched, this exception further leads to another exception when trying to load debug version of nodegit.node:

ERROR in ./node_modules/nodegit/dist/nodegit.js
    Module not found: Error: Can't resolve '../build/Debug/nodegit.node' in 'C:\my\project\path\node_modules\nodegit\dist'
     @ ./node_modules/nodegit/dist/nodegit.js 19:11-49
     @ ./main.js

Here the node-loader comes to rescue.
Since webpack told me to search for loaders to solve the error for release version of nodegit.node, I installed node-loader.
It eliminates the first error message but the second one continues to show.
Since there is no debug version, currently what I could do is commenting out rawApi = require("../build/Debug/nodegit.node");.
After doing so, webpack could finally bundle nodegit and it finally works. (launched the bundled JS with electron)

Hope this help everyone trying to use nodegit together with electron and webpack.

Edit 1

As it would be better to instruct webpack to ignore unused require instead of modifying source of nodegit, I spent some time on figuring it out. One solution is that one may use IgnorePlugin to do so. Adding the line plugins: [ new IgnorePlugin(/build\/Debug\/nodegit.node/i) ] to my webpack config solves the problem

Edit 2

Eventually I have to add nodegit into externals in my webpack configuration to build in production mode. I can build my project with webpack successfully now so please one take its webpack.config.js as a working example. I hope it helps.

I got this to work with Electron by attaching it to the window from the preload.js instead of requiring it from a rendering thread:

preload.js:

window.NodeGit = require("nodegit");

electron.js:

let mainWindow = new BrowserWindow({
    ..
    webPreferences: {
        nodeIntegration: false,
        preload: __dirname + '/preload.js',
    },
    ....
});

From the rest of the application I then access it from window.NodeGit.

I’ve only recently picked up Electron, so I’m not really well known yet with process specific issues. I’ll take a look when I’m home and see if I can somehow solve the issue or provide some more information.