NodeGit-无法读取未定义的属性“ TYPE”

问题描述

我正在使用电子,主要过程中有以下内容

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);

当我像往常一样开始纱线运动时,会得到以下输出

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)

可以在使用创建的新项目中复制

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

请注意,当我在使用ipc的渲染器过程中进行处理时,会得到相同的结果。

我发现了一个讨论here,存在类似问题,该解决方案似乎是将构建目标设置为ES5而不是ES6,因为Reset模块具有一个称为default功能,可能会导致ES6代码无法导入。我不确定情况是否如此。

但是我不知道如何指定ES5的编译目标。 (我正在使用webpack,而且我似乎也在使用babel。)

解决方法

我相信您应该检查包装所使用的模块类型。 webpack +打字稿使用的默认模块系统可以更改require的行为,以使您无法更改导出。 NodeGit会在其所有文件中更改其主要导出内容,因此lib/enums.js无法将TYPE分配给Git.Reset。我相信在我的一个项目中,我不得不切换到babel(现在支持打字稿)+ CommonJS来避免此问题。