プログラミング

(エラー) ‘git’ is not recognized as an internal or external command

プログラミングをしていて、パッケージのインストール時などにエラー”‘git’ is not recognized as an internal or external command,”が起こる場合があります。

例えば以下は、yo(yeoman)をインストールしようとする際にエラーが起こる例です。

> yo.cmd
? ==========================================================================
We're constantly looking for ways to make yo better!
May we anonymously report usage statistics to improve the tool over time?
More info: https://github.com/yeoman/insight & http://yeoman.io
========================================================================== No
? 'Allo ice! What would you like to do? Code

Make sure you are in the directory you want to scaffold into.
This generator can also be run with: yo code


     _-----_     ╭──────────────────────────╮
    |       |    │   Welcome to the Visual  │
    |--(o)--|    │   Studio Code Extension  │
   `---------´   │        generator!        │
    ( _´U`_ )    ╰──────────────────────────╯
    /___A___\   /
     |  ~  |
   __'.___.'__
 ´   `  |° ´ Y `

? What type of extension do you want to create? New Extension (TypeScript)
? What's the name of your extension? tmp
? What's the identifier of your extension? tmp
? What's the description of your extension?
? Initialize a git repository? Yes
? Bundle the source code with webpack? No
? Which package manager to use? npm

Writing in C:\pg\vscode\tmp...
   create .vscode\extensions.json
   create .vscode\launch.json
   create .vscode\settings.json
   create .vscode\tasks.json
   create src\test\runTest.ts
   create src\test\suite\extension.test.ts
   create src\test\suite\index.ts
   create .vscodeignore
   create .gitignore
   create README.md
   create CHANGELOG.md
   create vsc-extension-quickstart.md
   create tsconfig.json
   create src\extension.ts
   create package.json
   create .eslintrc.json


I'm all done. Running npm install for you to install the required dependencies. If this fails, try running the command yourself.



added 228 packages, and audited 229 packages in 22s

39 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

Your extension tmp has been created!

To start editing with Visual Studio Code, use the following commands:

     cd tmp
     code-insiders .

Open vsc-extension-quickstart.md inside the new extension for further instructions
on how to modify, test and publish your extension.

For more information, also visit http://code.visualstudio.com and follow us @code.


'git' is not recognized as an internal or external command,
operable program or batch file.

     _-----_     ╭───────────────────────╮
    |       |    │      Bye from us!     │
    |--(o)--|    │       Chat soon.      │
   `---------´   │      Yeoman team      │
    ( _´U`_ )    │    http://yeoman.io   │
    /___A___\   /╰───────────────────────╯
     |  ~  |
   __'.___.'__
 ´   `  |° ´ Y `

node:events:342
      throw er; // Unhandled 'error' event
      ^

Error: spawn git ENOENT
    at notFoundError (C:\Users\tmp\AppData\Roaming\npm\node_modules\generator-code\node_modules\cross-spawn\lib\enoent.js:6:26)
    at verifyENOENT (C:\Users\tmp\AppData\Roaming\npm\node_modules\generator-code\node_modules\cross-spawn\lib\enoent.js:40:16)
    at ChildProcess.cp.emit (C:\Users\tmp\AppData\Roaming\npm\node_modules\generator-code\node_modules\cross-spawn\lib\enoent.js:27:25)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
Emitted 'error' event on ChildProcess instance at:
    at ChildProcess.cp.emit (C:\Users\tmp\AppData\Roaming\npm\node_modules\generator-code\node_modules\cross-spawn\lib\enoent.js:30:37)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12) {
  code: 'ENOENT',
  errno: 'ENOENT',
  syscall: 'spawn git',
  path: 'git',
  spawnargs: [ 'init', '--quiet' ]
}

エラーの文章が示す通り、このエラーは”git”のインストールにより通常の場合解決することができます。

gitを実行ファイルとしてダウンロードし通常通りインストールするか、chocolateyを使用してgitをインストールします。

choco install git -y

インストール後、必ず忘れずにシェル(cmd.exeやpowershell)を終了、再起動します。これにより、”git”とコマンドを打って表示がされることを確認の上、エラーが起こった同様のコマンドで再度エラーの起こらないことを確認します。