Installing npm packages directly from GitHub gives ENOENT error

I’ve been trying to install npm packages from GitHub, but I’m encountering an ENOENT error every time. While installing from the npm registry works without any issues, directly using GitHub seems to fail.

For instance, when I attempt to run npm install https://github.com/expressjs/express, it results in an error regarding a missing package.json file. On the other hand, using npm install express behaves as expected and operates smoothly.

According to the error details:

npm http GET https://github.com/expressjs/express.git
npm http 200 https://github.com/expressjs/express.git
npm ERR! not a package /home/user/temp/npm-12345/9876543210987-0.123/tmp.tgz
npm ERR! Error: ENOENT, open '/home/user/temp/npm-12345/9876543210987-0.123/package/package.json'
npm ERR! System Linux 4.15.0-72-generic
npm ERR! command "/usr/bin/node" "/usr/bin/npm" "install" "https://github.com/expressjs/express.git"
npm ERR! node -v v14.16.0
npm ERR! npm -v 6.14.11
npm ERR! code ENOENT
npm ERR! errno 34

What mistake could I be making here? Is there a specific way I should be installing from GitHub?

I encountered a similar issue recently and found that the problem often stems from npm attempting to download the repository as a tarball instead of cloning it properly. The GitHub URL you’re using isn’t being recognized as a git repository by npm. Instead of using the direct GitHub URL, try the format npm install github:expressjs/express which is specifically designed for GitHub repositories. This shorthand tells npm to handle the GitHub repository correctly. Another thing to check is whether you have git installed and accessible from your command line, as npm needs git to clone repositories. If git isn’t properly configured, npm falls back to trying to download archives which can cause the ENOENT error you’re seeing. You can verify git is working by running git --version before attempting the install.

had this exact same problem last week! turns out npm sometimes has issues with the .git extension when downloading from github. try removing the .git part and use just npm install https://github.com/expressjs/express or better yet use the ssh format if you have keys setup. also check your npm cache might be corrupted - npm cache clean --force fixed it for me

This error typically occurs when npm tries to install from the GitHub URL but cannot locate a valid package.json in the repository’s root directory. The issue is often related to how npm handles the GitHub repository structure during installation. Try using the git+https protocol instead: npm install git+https://github.com/expressjs/express.git. This explicitly tells npm to treat it as a git repository. Another approach is to specify a specific branch or tag if the main branch doesn’t contain the proper package structure: npm install git+https://github.com/expressjs/express.git#master. Sometimes the repository might have the package.json in a subdirectory rather than the root, which causes npm to fail during extraction. You can also verify this by checking the repository structure on GitHub first to ensure package.json exists in the expected location.