git-js: Can't create an instance of SimpleGit in Typescript

Hi, As I’m trying to get simple-git working with typescript, I try to clone a repository if my baseDir doesn’t exists

If dir exists and trying to fetch and pull, everything’s alright

Here my code :

import { existsSync } from 'fs-extra'
import simplegit from 'simple-git/promise'

export class GitService {
  private git: simplegit.SimpleGit
  private readonly path: string
  private readonly repo: string

  constructor () {
    this.path = process.env.GIT_PATH
    this.repo = process.env.GIT_REPO

    if (!existsSync(this.path)) {
      simplegit().silent(true).clone(this.repo, this.path).catch()
    }

    this.git = simplegit(this.path).silent(true)
  }

  public async getLastVersion () {
    await this.git.fetch()
    return this.git.pull('origin', 'master')
  }

  public getPath (): string {
    return this.path
  }
}

While trying to get instance of simplegit to make my clone, I get this error :

projectPath\node_modules\simple-git\promise.js:58
   return Object.keys(git.constructor.prototype).reduce(function (api, fn) {
                          ^
TypeError: Cannot read property 'constructor' of undefined
    at Object.module.exports [as default] (projectPath\node_modules\simple-git\promise.js:58:27)
    at new GitService (projectPath\src\services\GitService.ts:17:25)
    at new Bot (projectPath\src\Bot.ts:30:23)
    at Object.<anonymous> (projectPath\src\index.ts:22:13)
    at Module._compile (module.js:652:30)
    at Module.m._compile (projectPath\node_modules\ts-node\src\index.ts:439:23)
    at Module._extensions..js (module.js:663:10)
    at Object.require.extensions.(anonymous function) [as .ts] (projectPath\node_modules\ts-node\src\index.ts:442:12)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)

Any idea how I can resolve this problem ?

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 15 (3 by maintainers)

Most upvoted comments

As this has been added to an issue relating to a different major release of the library, please can you create a new issue and include the version of TypeScript are you using? Thanks

You can import the constructor and types from simple-git/promise using:

import gitP, { SimpleGit } from 'simple-git/promise';

export const run = async () => {
    const git: SimpleGit = gitP();
    console.log('This is', (await git.checkIsRepo()) ? '' : ' not', ' a git repo.');
};

Which works in my current test script based on:

"dependencies": {
  "@types/node": "^13.7.7",
  "simple-git": "^1.131.0",
  "typescript": "^3.8.3"
}

Okay that actually happend when you tried to additionally install @types/simple-git despite gettting an error. What fixed it was a npm un simple-git --save and then reinstall just the simple-git but weird…

error TS2503: Cannot find namespace 'NodeJS'.
node_modules/simple-git/promise.d.ts

This is the way I made it work

import simplegit, {SimpleGit} from 'simple-git/promise'

export class GitService {
  private git: SimpleGit

  constructor() {
    this.git = simplegit()
  }
}

Of course, you need to have in your tsconfig.json

{
    "esModuleInterop"             : true,
    "allowSyntheticDefaultImports": true,
}