如何以编程方式获取 npm 安装模块的详细信息? [英] How to get details of npm installed modules programatically?

查看:60
本文介绍了如何以编程方式获取 npm 安装模块的详细信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法或库可以告诉从 require 的模块在哪里解析,尤其是它可能包含哪些二进制文件?

Is there way or library that could tell where from require'd module was resolved and especially what binaries it possibly contains?

例如,当我需要('coffee-script')时,(AFAIK)无法告诉它的安装目录以及它具有哪些命令行二进制文件.

For example, when I require('coffee-script') there is (AFAIK) no way to tell its installation directory and what command line binaries it has.

我理想中需要的是 require 和 package.json 解析器之间的某种混合,例如就像遵循假设的npminfo"库一样.

What I ideally need is some kind of mix between require and package.json parser, e.g. like following hypotethical 'npminfo' library.

var npminfo = require('npminfo')

// get info about module
var pkginfo = npminfo.resolve('coffee-script')
pkginfo.version => '1.1.0'
pkginfo.path  => '/home/teemu/node_modules/coffee-script'
pkginfo.bins =>  { coffee: '/home/teemu/node_modules/coffee-script/bin/coffee', cake: '/home/teemu/node_modules/coffee-script/bin/cake'}

// generic info
npminfo.binpath => '/home/teemu/.node_modules/bin' 

我确实尝试使用 require.paths 并且只是遍历目录,但由于某种原因,它不包含我的模块实际安装的路径.不知何故,require 仍然可以找到它们?

I did try to use require.paths and just walk through directories, but for some reason it does not contain path where my modules are actually installed. Somehow require still finds them though?

~ $ node
> require.paths
[ '/Users/teemuikonen/.node_modules',
  '/Users/teemuikonen/.node_libraries',
  '/usr/local/lib/node' ]
>

~ $ ls /usr/local/lib/node
wafadmin

~ $ ls .node_modules/
ls: .node_modules: No such file or directory

~ $ ls node_modules/
cli  cradle coffee-script ...   

谢谢

推荐答案

使用require.resolve('module')获取路径

use require.resolve('module') to get the path

require 在每个级别查找名为 node_modules 的文件夹.这未显示在 require.paths() 中,但我不确定为什么.

require looks for a folder called node_modules at each level. This isn't displayed in require.paths(), but I'm not sure why.

更新:这将记录模块文件夹中的文件

Update: this will log the files in the modules folder

var fs = require('fs');
var path = require('path');
var path1 = require.resolve('module');
path1 = path.dirname(path1);
fs.readdir(path1, function(err, files){
  console.log(err);
  console.log(files);
})

这篇关于如何以编程方式获取 npm 安装模块的详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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