使用链接的本地包更新全局 NPM 包 [英] Update global NPM packages with linked local package

查看:75
本文介绍了使用链接的本地包更新全局 NPM 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I linked a local package globally using npm link. Unfortunately when I try to update my globally installed packages it now gives an error:

$ 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.

有没有办法解决这个问题?

Is there a way around this?

推荐答案

update - 作为一个更简洁的解决方案,我写了 一个仅打印未链接的全局 npm 包的节点脚本,我将其粘贴在下面.将此脚本作为路径上某处的可执行文件(在此示例中名为 npm-lsg-unlinked),您可以运行 npm-lsg-unlinked |xargs npm update -g 更新所有全局 npm 包而不更新链接的包(这是 OP 错误的原因).

update - As a cleaner solution, I wrote a node script to print only unlinked global npm packages, I'll paste it below. With this script as an executable somewhere on your path (named npm-lsg-unlinked in this example), you run npm-lsg-unlinked | xargs npm update -g to update all global npm packages without updating linked ones (which is the cause of OP's error).

#!/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)
})


我在我的类似问题上写过这个问题a href="https://stackoverflow.com/questions/67691622/update-all-global-npm-packages-except-linked-ones">更新除链接的所有全局 npm 包) 但就我知道,npm 的 CLI 中没有本地方法可以避免这种情况.npm update -g 不带参数尝试更新所有全局包,包括通过 npm link 符号链接到全局 node_modules 文件夹的包,这将如果您正在处理的本地包不在 npm 存储库中,则失败.


I wrote about this on my similar questionUpdate all global npm packages except linked ones) but as far as I know, there's no native way in npm's CLI to avoid this. npm update -g without an argument tries to update all global packages, including the ones symlinked into the global node_modules folder by npm link, which will fail if it's a local package you're working on that's not in the npm repository.

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

I tried to find a work-around using npm ls -g and shell tools to filter its output to only the non-linked packages and this was my first try:

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

这需要一个全局 npm 模块列表,删除无用的第一行,修剪格式(像└──"这样的字符),删除带有;"的行.->"在它们(这些是链接的包)中,从包名中去除版本字符串,然后将全局包名列表通过管道传输到 npm update -g.

That takes a list of global npm modules, deletes the useless first line, trims the formatting (characters like "└──"), deletes lines with " -> " in them (these are the linked packages), strips off the version string from the package name, and then pipes the list of global package names to npm update -g.

一些 sed 正则表达式并不完美,如果 npm 改变了它们的输出语法,这个命令将需要更新.我还考虑通过 jq 过滤器,但鉴于 npm 的 JSON 输出的结构,我无法弄清楚如何编写它.

Some of the sed regular expressions are not perfect and this command will need updating if npm ever changes their output syntax. I also thought about piping npm ls -g --json through a jq filter but I couldn't figure out how to write it given the structure of npm's JSON output.

这篇关于使用链接的本地包更新全局 NPM 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆