如何更改npm ll的输出格式?

时间:2019-03-04 13:07:25

标签: node.js shell npm

我需要为我的客户提供我们项目的所有依赖项(库名称+ github / npm url)。我只需在每个项目中使用npm ll就可以轻松地做到这一点。但这给了我树状结构的输出。有没有办法将此输出更改为表?

我已经阅读了npm ll命令的文档,但没有发现任何东西。

2 个答案:

答案 0 :(得分:2)

不确定它是否能完全回答问题,但是您始终可以将输出修改为更易于管理的内容,例如脚本,例如:

#!/bin/bash
# list packages and remove tree structure prefix + "deduped" suffix (keep only module@version)
data=($(npm ls | sed 's/^[┬├│─└ ]*//g' | sed 's/deduped$//g'))
for module in ${data[*]}
do
  # split on @ only when it separates module and version
  # (not the @ prefix on @username/module@version)
  split=($(echo $module | sed -r 's/^(.+)@/\1 /'))
  # an example display
  printf "%-30s%10s https://www.npmjs.org/packages/%s\n" ${split[0]} ${split[1]} ${split[0]}
done

答案 1 :(得分:2)

如果其他所有方法均失败,则可以使用jq提取所需内容:

npm ll --json | jq -r 'recurse(.dependencies[]) | [.name, .version, .repository.url?] | @csv'

示例输出:

"express","4.16.4","git+https://github.com/expressjs/express.git"
"accepts","1.3.5","git+https://github.com/jshttp/accepts.git"
"mime-types","2.1.22","git+https://github.com/jshttp/mime-types.git"
"mime-db","1.38.0","git+https://github.com/jshttp/mime-db.git"
"negotiator","0.6.1","git+https://github.com/jshttp/negotiator.git"