I use youtube-dl node driver to get information about videos on youtube, but I have trouble with ReferenceError - require is not defined (First line). I installed node-youtube-dl module on my laptop and the code is just taken from example in description. Is there anything else that I'm missing to do?
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()
is a keyword in Node.js. It is used to import CommonJS modules like those in the npm registry. But it is not a keyword in your browser, where trying to use it will return a ReferenceError
as you describe.
If you want to import npm modules using require()
in your browser, you can try browserify or webpack. However, it is entirely possible (probable?) that youtube-dl
is not compatible with browserify or webpack.
If you don't need to run it from a browser, you can put the code in a file (say, myFile.js
) and run it from the command line with node:
node myFile.js