使用npm安装私有GitHub仓库依赖报错,yarn可正常安装
解决npm安装私有GitHub依赖时的ENOENT错误
问题背景
在package.json中添加私有GitHub仓库作为依赖后,执行npm install出现以下错误,但yarn install可正常完成:
依赖配置:
"repo-name": "git+https://github_pat_***:x-oauth-basic@github.com/company/repo-name.git"
报错日志:
npm ERR! code ENOENT npm ERR! syscall open npm ERR! path /Users/username/.npm/_cacache/tmp/git-cloneXObG5S/package.json npm ERR! errno -2 npm ERR! enoent ENOENT: no such file or directory, open '/Users/username/.npm/_cacache/tmp/git-cloneXObG5S/package.json' npm ERR! enoent This is related to npm not being able to find a file. npm ERR! enoent npm ERR! A complete log of this run can be found in: /Users/username/.npm/_logs/2024-02-22T15_22_06_508Z-debug-0.log
解决方案
1. 清理npm缓存
缓存损坏可能导致临时克隆目录的文件不完整,执行以下命令清理缓存后重新安装:
npm cache clean --force npm install
2. 验证PAT权限
确保你的GitHub个人访问令牌(PAT)拥有repo权限(私有仓库读取权限)。即使yarn能成功安装,npm在克隆后的文件校验环节可能需要更明确的权限。可以重新生成带repo权限的PAT替换原链接中的token。
3. 指定仓库的分支/标签/Commit哈希
在依赖链接末尾明确指定分支、标签或Commit哈希,避免npm默认克隆的内容不符合预期:
"repo-name": "git+https://github_pat_***:x-oauth-basic@github.com/company/repo-name.git#main"
4. 升级npm版本
低版本npm处理Git依赖可能存在逻辑bug,升级到最新稳定版后重试:
npm install -g npm npm install
5. 手动克隆仓库验证
替换链接中的token后,手动执行克隆命令,检查仓库根目录是否存在package.json:
git clone https://github_pat_***:x-oauth-basic@github.com/company/repo-name.git
如果克隆后没有package.json,说明是私有仓库本身的结构问题,需要确认仓库是否正确配置了npm包结构。
内容的提问来源于stack exchange,提问作者Alperen Akyuz
相关产品推荐
相关产品推荐

