简体   繁体   English

显示package.json中的依赖项

[英]Display the dependencies from package.json

Is there a way we can display the version of Express, Jade, Stylus.. that we have installed in our nodejs. 有没有办法可以显示我们在nodejs中安装的Express,Jade,Stylus ..的版本。 Capture the current version and display it in the browser. 捕获当前版本并在浏览器中显示。

Thanks 谢谢

Basically a simple 基本上很简单

$ npm ls

does what you want: It gives you a list of all installed modules, their versions, and their dependencies with the same info recursively. 做你想做的事情:它以递归的方式为你提供所有已安装模块,它们的版本以及它们的依赖关系的列表。

As you have asked for a solution that works in your browser: The easiest solution will probably be to run that command as a child process from Node.js using the child_process module , and pipe the child's stdout property to the response stream of an http server. 正如你所要的那种在浏览器有效的解决方案:最简单的解决方案将可能使用到运行该命令从Node.js的一个子进程child_process模块 ,以及孩子的标准输出属性到HTTP服务器的响应流。

Then you get the output of npm ls inside your browser. 然后在浏览器中获得npm ls的输出。

The basic frame looks like this: 基本框架如下所示:

var spawn = require('child_process').spawn,
    http = require('http');

http.createServer(function (req, res) {
  var npm = spawn('npm', [ 'ls' ]);
  npm.stdout.pipe(res);
}).listen(3000);

Of course you can make it nicer, more comfortable, and so on :-) 当然你可以让它更好,更舒适,等等:-)

Update from comments: 来自评论的更新:

var npm = spawn('npm', [ 'ls', '--json' ]);

Although Golo Roden's reply describes a valid way, I think opening a separate process just to read a version is too much. 尽管Golo Roden的回复描述了一种有效的方式,但我认为打开一个单独的过程只是为了阅读一个版本太多了。

You are specifically asking about the version in package.json, so why don't you just read that file and parse it? 你是专门询问package.json中的版本,那你为什么不读这个文件并解析呢? I successfully managed to use the fs module for this. 我成功地设法使用fs模块。 Here is my app: 这是我的应用程序:

var application_root = __dirname,
    express = require('express'),
    path = require('path'),
    fs = require('fs');

var app = express();

app.configure(function () {
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(path.join(application_root, "public")));
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.get('/api', function (req, res) {
    fs.readFile(application_root + '/package.json', function(err, fd) {
        console.log(err);
        console.log(fd);
        res.send('File data: ' + fd);
    });
});

app.listen(4242);

This will display the contents of the package.json file in your root folder, when you request the /api URL. 当您请求/ api URL时,这将在根文件夹中显示package.json文件的内容。 If you only want the version of a specific dependency you can always query the properties of the fd object in the callback and display that. 如果您只想要特定依赖项的版本,则始终可以在回调中查询fd对象的属性并显示该属性。

Of course the disadvantage here is that this will always read the file rather than use the 'real' version from npm, but since your node application reads the file anyway, I don't see why not do it. 当然这里的缺点是它总是会读取文件而不是使用npm中的“真实”版本,但是由于你的节点应用程序无论如何都要读取文件,我不明白为什么不这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM