用链接的本地包更新全局NPM包



我使用npm link全局链接了一个本地包。不幸的是,当我尝试更新全局安装的软件包时,它现在给出了一个错误:

$ npm update -g
npm ERR! code E404
npm ERR! 404 Not Found - GET https://registry.npmjs.org/vue_type_checker - Not found
npm ERR! 404 
npm ERR! 404  'vue_type_checker@*' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

有办法解决这个问题吗?

update作为一个更简洁的解决方案,我写了一个node脚本来只打印未链接的全局npm包,我将它粘贴在下面。将此脚本作为可执行文件放在路径的某个位置(本例中名为npm-lsg-unlinked),运行npm-lsg-unlinked | xargs npm update -g来更新所有全局npm包,而不更新链接包(这是OP错误的原因)。

#!/usr/bin/env node
const { exec } = require("child_process")
exec('npm ls --global --json', (err, stdout, stderr) => {
if (err) throw err
const deps = JSON.parse(stdout).dependencies
// dependencies hash looks like:
// "linked-pkg": { "version": "1.0.0", "resolved": "file:..." },
// "global-pkg": { "version": "1.0.0" }, ...
// so we skip any package that has a "resolved" property
const output = Object.keys(deps).reduce((total, key) => {
return deps[key].resolved ? total : `${total}${key}n`
}, '')
process.stdout.write(output)
process.stderr.write(stderr)
})

我写过类似的问题更新所有全局npm包(除了链接的包),但据我所知,在npm的CLI中没有原生的方法来避免这种情况。没有参数的npm update -g试图更新所有全局包,包括那些通过npm link符号链接到全局node_modules文件夹的包,如果它是你正在处理的不在npm存储库中的本地包,则会失败。

我试图找到一个使用npm ls -g和shell工具来过滤其输出到非链接包的解决方案,这是我的第一次尝试:

npm ls -g | tail -n+2 | tr -d '├─└ ' | 
sed -e '/ -> /d' -e 's|@[0-9.]*||' | xargs npm update -g

获取全局npm模块的列表,删除无用的第一行,修改格式(像"→";在其中(这些是链接的包),从包名中剥离版本字符串,然后将全局包名列表管道到npm update -g

一些sed正则表达式不完美,如果npm改变了它们的输出语法,这个命令将需要更新。我也想过管道npm ls -g --json通过jq过滤器,但我不知道如何写给npm的JSON输出的结构。

最新更新