javascript - 如何使用 node-youtube-dl 驱动程序?

标签 javascript node.js youtube youtube-dl

我使用 youtube-dl node driver在 youtube 上获取有关视频的信息,但我遇到了 ReferenceError - require is not defined (First line) 的问题。我在笔记本电脑上安装了 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);
});

最佳答案

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

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

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

node myFile.js

关于javascript - 如何使用 node-youtube-dl 驱动程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31309462/

相关文章:

youtube - 如何在 9 月 12 日之后获取稍后观看的播放列表详细信息?

php - 如何从 Youtube channel 获取所有视频 ID

youtube - YouTube mix 的正确 URL 格式,用于抓取视频的特定 URL

javascript - JavaScript 中的(值不等于值)是什么意思?

javascript - PHP/JS : Auto click button in iframe when its opened

javascript - 删除 Canvas 元素的图像

node.js - puppeteer 师 : take webpage screenshot with zoom (scale)

javascript - 无法专注于 TextInput React Native - Amazon Fire Stick

node.js - 现在使用 zeit 设置长期运行的微服务的正确方法是什么?

node.js - OpenWhisk 返回整个响应对象而不仅仅是数据