NPM 无法安装类型

NPM fails to install types

我对 npm install 有问题,因为它不会安装 @types。

这个很好用

npm install --save lodash

但是要求类型不会

npm install --save @types/lodash

PS C:\Development\Temp> npm install --save @types/lodash
npm WARN `git config --get remote.origin.url` returned wrong result (git://github.com/types/lodash)
npm WARN `git config --get remote.origin.url` returned wrong result (git@github.com:types/lodash)
npm ERR! git clone git@github.com:types/lodash Cloning into bare repository 'C:\Users\myuser\AppData\Roaming\npm-cache\_git-remotes\git-github-com-types-lodash-9eb5372a'...
npm ERR! git clone git@github.com:types/lodash Host key verification failed.
npm ERR! git clone git@github.com:types/lodash fatal: Could not read from remote repository.
npm ERR! git clone git@github.com:types/lodash
npm ERR! git clone git@github.com:types/lodash Please make sure you have the correct access rights
npm ERR! git clone git@github.com:types/lodash and the repository exists.
npm ERR! addLocal Could not install types/lodash
npm ERR! Error: ENOENT: no such file or directory, stat 'C:\Development\Temp\types\lodash'
npm ERR! If you need help, you may report this *entire* log,
npm ERR! including the npm and node versions, at:
npm ERR!     <http://github.com/npm/npm/issues>

npm ERR! System Windows_NT 10.0.15063
npm ERR! command "C:\Program Files\nodejs\node.exe" "C:\ProgramData\chocolatey\lib\npm\tools\node_modules\npm\bin\npm-cli.js" "install" "--save" "@types/lodash"
npm ERR! cwd C:\Development\Temp
npm ERR! node -v v8.6.0
npm ERR! npm -v 1.4.9
npm ERR! path C:\Development\Temp\types\lodash
npm ERR! syscall stat
npm ERR! code ENOENT
npm ERR! errno -4058
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\Development\Temp\npm-debug.log
npm ERR! not ok code 0
PS C:\Development\Temp>

这让我摸不着头脑。我已经更新了 Chocolatey、NodeJS、NPM 以确保它们拥有最新版本。尝试对空文件夹或现有 TypeScript 项目执行命令 - 认为在 Git 存储库中 运行 可能会让人感到困惑(错误:remote.origin.url)。查看 GitHub URL 毫无意义 git://github.com/types/lodash

这些例子来自 MSDN Blog - The Future of Declaration Files

更新:我已经卸载 Node.js 并尝试重新安装 v6.11.3 LTS 或 v8.6.0。但是@types 命令仍然失败。

更新 2:我发现 Chocolately 屏蔽了 npm 版本。我删除了 Chocolately 文件夹,并根据@Louis 的回答升级了 npm。

将您的 npm 版本升级到版本 4 或 5。我提到 4 是因为我 运行 遇到了 5 的问题,使用 4 仍然可行。我不知道有什么理由要 运行 旧版本。

您正在使用 npm 版本 1.4.9,如日志的这一行所示:

npm ERR! npm -v 1.4.9

问题是 npm 版本 2 之前的版本不支持作用域包。以 @ 开头的包是范围包,因此 @types/lodash 是范围包。您需要 npm 版本 2 或更高版本才能安装它。如果您要使用 1.x 系列 (1.4.29) 中最新的 npm,您会收到更好的错误消息:

npm ERR! Error: This version of npm doesn't support scoped packages (caused by reference to @types/lodash). Update to npm@2+.

版本 1.4.9 甚至不知道范围包是一回事,所以它不能给出一个很好的错误消息。看起来它使用 @ 符号表示包名称是一个地址,并使用 Github 作为默认主机填充缺少的信息。

我通常用来升级npm的升级命令是:

npm install -g npm

您可以通过将 npm 参数替换为给出特定版本号的参数来指定特定版本。例如,npm@4 会安装 4.x 系列中最新的 npm。如果不指定版本号,则获取最新发布的版本。