question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't create an instance of SimpleGit in Typescript

See original GitHub issue

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 ?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
steveukxcommented, Feb 7, 2022

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

1reaction
steveukxcommented, Mar 6, 2020

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"
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to jest.mock simple-git/promise - Stack Overflow
Here is the unit test example: index.ts : import git from 'simple-git/promise'; function main() ...
Read more >
simple-git - npm
Simple Git. NPM version. A lightweight interface for running git commands in any node.js application. Version 3 - Out Now.
Read more >
Building a MEAN REST API and host in the Heroku cloud ...
Run the following command to create a new Heroku app: ... Create a new tsconfig.json file which will define the TypeScript project settings....
Read more >
Typescript dependency injection: setting up InversifyJS IoC for ...
When executed, the main file is 'new-up-ing' (manually creating a new instance of a class) the service class, calling it's getAllNames ...
Read more >
Typescript with Jasmine: easy project setup | by Turhan Oz
In this article, we will go straight to the point for creating a ... "description": "example of typescript project with unit tests ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found