node.js 列出我已链接 npm 的节点模块的简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24933955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Easy way to list node modules I have npm linked?
提问by callum
I am looking for a command that will list the names of global modules that I have npm link'd to local copies, also listing the local path.
我正在寻找一个命令,该命令将列出我已npm link复制到本地副本的全局模块的名称,同时列出本地路径。
In fact, a list of allglobally installed modules would be even better, with the npm link'd ones flaggedsomehow.
事实上,所有全局安装的模块的列表会更好,以某种方式标记npm link'd 的模块。
采纳答案by mscdex
Did you try just listing the node_modulesdirectory contents (e.g. ls -l node_modules | grep ^l)? They're normal symlinks.
您是否尝试只列出node_modules目录内容(例如ls -l node_modules | grep ^l)?它们是正常的符号链接。
If you really need to find allsymlinks, you could try something like find / -type d -name "node_modules" 2>/dev/null | xargs -I{} find {} -type l -maxdepth 1 | xargs ls -l.
如果你真的需要找到所有的符号链接,你可以尝试像find / -type d -name "node_modules" 2>/dev/null | xargs -I{} find {} -type l -maxdepth 1 | xargs ls -l.
回答by Andrew
To list all globally linked modules, this works (documentation https://docs.npmjs.com/cli/ls):
要列出所有全局链接的模块,这是有效的(文档https://docs.npmjs.com/cli/ls):
npm ls -g --depth=0 --link=true
I had to update the version of npm on my machine first, though:
不过,我必须先在我的机器上更新 npm 的版本:
npm install npm@latest -g
回答by Konstantin Kalbazov
A better alternative to parsing lsis to use findlike this:
解析的更好替代方法ls是使用find如下:
find . -type l
You can use -maxdepth 1to only process the first directory level:
您可以使用-maxdepth 1仅处理第一个目录级别:
find . -maxdepth 1 -type l
You can use -lsfor additional info.
您可以使用-ls其他信息。
For instance, for finding node modules that are npm linked:
例如,要查找 npm 链接的节点模块:
find node_modules -maxdepth 1 -type l -ls
Here's an articlewhy parsing lsis not the best idea
这是一篇文章为什么解析ls不是最好的主意
回答by RockyRoad
If you want a nice colored output from npm list, you may like:
如果你想要一个漂亮的彩色输出npm list,你可能会喜欢:
\ls -F node_modules | sed -n 's/@$//p' | xargs npm ls -g --depth 0
which gives in my current playground dir:
在我当前的操场目录中给出:
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
+-- [email protected]
`-- [email protected]
It makes a few assumptions but it should work in most cases, or be easy to adapt with the explanations below.
它做了一些假设,但它应该在大多数情况下工作,或者很容易适应下面的解释。
- use
\lsto bypass possible aliases on yourlscommand - the
-Foption adds an '@' indicator for links - the
sedcommand selects those links and removes the indicator - the
xargspart passes previous output as arguments tonpm ... npmis invoked withlistorlsto list modules with versions- replace with
llto get details about each listed module.
- replace with
-gfor the global modules and--depth 0for a shallow listing (optional)--long false(default with 'list').
- 用于
\ls绕过ls命令中可能的别名 - 该
-F选项为链接添加了一个“@”指示符 - 该
sed命令选择这些链接并删除指示器 - 该
xargs部分将先前的输出作为参数传递给npm ... npm被调用list或ls列出带有版本的模块- 替换为
ll以获取有关每个列出的模块的详细信息。
- 替换为
-g对于全局模块和--depth 0对于浅列表(可选)--long false(默认为“列表”)。
Issue:for some reason npmgives extraneous entries for me at the moment (non colored). They would be those I had "npm unlink"ed.
问题:出于某种原因,npm目前为我提供了无关的条目(非彩色)。他们将是我有“ npm unlink” ed 的那些。
For "a list of all globally installed modules" in current npm path, you just do
对于当前 npm 路径中的“所有全局安装模块的列表”,您只需执行
npm list -g
For further needs you may want to have a look at
对于进一步的需求,您可能需要查看
npm help folders
You cannot follow symlinks backwards unless you scan your whole filesystem and (then that's not a npm specific question).
除非您扫描整个文件系统并且(那么这不是 npm 特定问题),否则您不能向后跟踪符号链接。
For quickly finding files and directories by name, I use locatewhich works on an index rebuilt usually once a day.
为了按名称快速查找文件和目录,我locate通常每天使用which 对重建的索引起作用。
locate '*/node_modules'
and start working from there (you may want to refine the search with --regexpoption.
并从那里开始工作(您可能希望使用--regexp选项来优化搜索。
回答by Stoffe
I found this question after I also wrote my own tool, here it is for completeness: npm-list-linked.
我在编写自己的工具后发现了这个问题,这里是为了完整性:npm-list-linked。
It will recursively follow all linked packages down in the hierarchy as well, at my work we sometimes may have npm link2-3 levels deep and this way you can see exactly which are local and which ones are not, avoids surprises.
它也会递归地跟踪层次结构中的所有链接包,在我的工作中,我们有时可能有npm link2-3 层深,这样您就可以准确地看到哪些是本地的,哪些不是,避免出现意外。
$ npm-list-linked
Linked packages in /home/user/projects/some-project/
@prefix/package 0.2.7
other-package 0.1.2
回答by ryanve
I made a Node.js module that uses fsto check for symlinks made by npm linkor otherwise.
我制作了一个 Node.js 模块,用于fs检查由npm link或以其他方式制作的符号链接。
https://www.npmjs.com/package/symlinked
https://www.npmjs.com/package/symlinked
var symlinked = require("symlinked")
console.log(symlinked.names())
回答by danmakenoise
回答by BaronVonKaneHoffen
find `npm root -g` -maxdepth 2 -type l
to show global links, including namespaced packages.
显示全局链接,包括命名空间包。
@andrew's answer works some of the time:
@andrew 的回答有时有效:
npm ls -g --depth=0 --link=true
but blew up on peer dep errors for me on some occasions. Hope that helps someone!
但在某些情况下,对我来说,peer dep 错误会让我大吃一惊。希望对某人有所帮助!

