NPM无法安装types

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

这工作正常

npm install --save lodash 

然而,要求types不

 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博客 – 宣言文件的未来这些例子

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

更新2:我已经意识到巧克力是掩盖npm版本。 我删除了巧克力文件夹,并根据@Louis答案升级了npm。

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

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

 npm ERR! npm -v 1.4.9 

问题是版本2之前的npm版本不支持范围包 。 以@开始的包是范围包,因此@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甚至不知道范围包是一件事情,所以它不能给出一个很好的错误信息。 它看起来像@符号作为表示包名是一个地址,并填写缺less的信息与Github作为默认主机。

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

 npm install -g npm 

您可以通过将npm参数replace为给定特定版本号来指定特定版本。 例如, npm@4会在4.x系列中安装最新的npm 。 如果你没有指定一个版本号,你会得到最新的发布版本。