1

I'm using electron and I have the following in my main process

import * as Git from "nodegit";
console.log("NodeGit version is:", Git.version);
console.log("The HARD reset type has the id", Git.Reset.TYPE.HARD);

When I yarn start as usual, I get the following output

NodeGit version is: 0.27.0
App threw an error during load
TypeError: Cannot read property 'TYPE' of undefined
    at Object../src/main.ts ([redacted-project-dir]\.webpack\main\index.js:17543:57) 
    at __webpack_require__ ([redacted-project-dir]\.webpack\main\index.js:21:30)     
    at [redacted-project-dir]\.webpack\main\index.js:85:18
    at Object.<anonymous> ([redacted-project-dir]\.webpack\main\index.js:88:10)      
    at Module._compile (internal/modules/cjs/loader.js:968:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:986:10)       
    at Module.load (internal/modules/cjs/loader.js:816:32)
    at Module._load (internal/modules/cjs/loader.js:728:14)
    at Module._load (electron/js2c/asar.js:717:26)
    at Function.Module._load (electron/js2c/asar.js:717:26)

This can be reproduced in a fresh project created with

yarn create electron-app my-electron-webpack-typescript --template=typescript-webpack

Note that I get identical results when I do these in my renderer process using ipc.

I found a discussion here with a similar problem where the solution seemed to be to set the build target to ES5 instead of ES6 as the Reset module has a function called default which might cause an ES6 code not being able to import it. I'm not sure if this could even be the case.

But I don't know how could I specify the compile target to ES5. (I'm using webpack and I also seem to be using babel.)

Artur Kovacs
  • 141
  • 5

1 Answers1

0

I believe you should check what type of modules your packing with. The default module system that webpack + typescript uses can change the behavior of require such that you cannot mutate an export. NodeGit mutates its main export in all of its files, and so lib/enums.js cannot assign TYPE to Git.Reset. I believe in a project of mine I had to switch over to babel (which supports typescript now) + CommonJS to avoid this issue.

Tyler
  • 1
  • Thanks! I tried adding a webpack rule in my `webpack.main.config.js` for every .ts file not in my node_modules folder and applying a typescript loader and a babel-loader with a "@babel/preset-env" preset that has "modules": "commonjs" set. I also have "externals: { nodegit: "commonjs nodegit", }" set for my main process config. I still get the same error thrown at start. :( – Artur Kovacs Sep 14 '20 at 12:03