1

我使用youtube-dl 节点驱动程序来获取有关 youtube 上视频的信息,但我遇到了 ReferenceError 问题 - 未定义要求(第一行)。我在笔记本电脑上安装了 node-youtube-dl 模块,代码仅取自描述中的示例。还有什么我想做的吗?

var youtubedl = require('youtube-dl');
var url = 'https://www.youtube.com/watch?v=9Q7Vr3yQYWQ';

ytdl.getInfo(url, function(err, info) {
  if (err) throw err;

  console.log('id:', info.id);
  console.log('title:', info.title);
  console.log('url:', info.url);
  console.log('thumbnail:', info.thumbnail);
  console.log('description:', info.description);
  console.log('filename:', info._filename);
  console.log('duration:', info.duration);
  console.log('format_id:', info.format_id);
});

4

2 回答 2

2

require()是 Node.js 中的关键字。它用于导入像 npm 注册表中的 CommonJS 模块。但它不是您浏览器中的关键字,尝试使用它会返回ReferenceError您描述的 a 。

如果你想require()在浏览器中导入 npm 模块,你可以试试browserifywebpack。但是,完全有可能(可能?)youtube-dl与 browserify 或 webpack 不兼容。

如果您不需要从浏览器运行它,您可以将代码放在一个文件中(例如,myFile.js),然后使用 node 从命令行运行它:

node myFile.js
于 2015-07-09T06:17:02.630 回答
0

在您的示例中:

var youtubedl = 需要('youtube-dl');

但是后来你称之为:

ytdl.getInfo(网址,函数(错误,信息)

只需简单地将您的要求重命名为 ytdl,如下所示:

var ytdl = require('youtube-dl');

于 2016-02-06T18:39:50.397 回答