打印所有已安装 node.js 模块的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13981938/
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
Print a list of all installed node.js modules
提问by Anderson Green
In a node.js script that I'm working on, I want to print all node.js modules (installed using npm) to the command line. How can I do this?
在我正在处理的 node.js 脚本中,我想将所有 node.js 模块(使用 npm 安装)打印到命令行。我怎样才能做到这一点?
console.log(__filename);
//now I want to print all installed modules to the command line. How can I do this?
采纳答案by Andrey Sidorov
Use npm ls(there is even json output)
使用npm ls(甚至还有 json 输出)
From the script:
从脚本:
test.js:
测试.js:
function npmls(cb) {
require('child_process').exec('npm ls --json', function(err, stdout, stderr) {
if (err) return cb(err)
cb(null, JSON.parse(stdout));
});
}
npmls(console.log);
run:
跑:
> node test.js
null { name: 'x11', version: '0.0.11' }
回答by aniston
If you are only interested in the packages installed globally without the full TREE then:
如果您只对没有完整 TREE 的全局安装的软件包感兴趣,那么:
npm -g ls --depth=0
npm -g ls --depth=0
or locally (omit -g) :
或本地(省略 -g):
npm ls --depth=0
npm ls --depth=0
回答by hfarazm
list of all globally installed third party modules, write in console:
所有全局安装的第三方模块的列表,在控制台中写入:
npm -g ls
回答by Muhammad F. Musad
in any os
在任何操作系统中
npm -g list
and thats it
就是这样
回答by d4nyll
Generally, there are two ways to list out installed packages - through the Command Line Interface (CLI) or in your application using the API.
通常,有两种方法可以列出已安装的软件包 - 通过命令行界面 ( CLI) 或在您的应用程序中使用API。
Both commands will print to stdoutall the versions of packages that are installed, as well as their dependencies, in a tree-structure.
这两个命令都将打印到stdout树结构中安装的所有软件包版本及其依赖项。
CLI
命令行界面
npm list
Use the -g(global) flag to list out all globally-installed packages. Use the --depth=0flag to list out only the top packages and not their dependencies.
使用-g(global) 标志列出所有全局安装的包。使用该--depth=0标志仅列出顶级包而不是它们的依赖项。
API
应用程序接口
In your case, you want to run this within your script, so you'd need to use the API. From the docs:
在你的情况下,你想在你的脚本中运行它,所以你需要使用 API。从文档:
npm.commands.ls(args, [silent,] callback)
In addition to printing to stdout, the data will also be passed into the callback.
除了打印到stdout,数据也会传入回调。
回答by neojp
Why not grab them from dependenciesin package.json?
为什么不从抓住他们dependencies的package.json?
Of course, this will only give you the ones you actually saved, but you should be doing that anyway.
当然,这只会为您提供您实际保存的那些,但无论如何您都应该这样做。
console.log(Object.keys(require('./package.json').dependencies));
回答by A T
for package in `sudo npm -g ls --depth=0 --parseable`; do
printf "${package##*/}\n";
done

