Trying to use Dom-Parser with Discord.js. Couldn't find help from any where else.
Error on line 15 fs.readFile
I also had a lot problems getting fs working. First it wasn't defined then it could not be runned before initalization, just got that fixed (I hope)
// Discord stuff.
const Discord = require('discord.js');
const client = new Discord.Client();
const config = require('./config.json');
const token = config.token;
// DomParser
var DomParser = require('dom-parser');
var parser = new DomParser();
var data = fs.readFileSync(filepathHidden);
// Other requirements
var fs = require('fs');
// when getting online.
client.once('ready', () => {
console.log('WAHAHAHA IM ALIVE!');
}),
fs.readFile('https://url.com)', 'utf8', function(err, html){
if (!err){
var dom = parser.parseFromString(html);
console.log(dom.getElementsByClassName('new_solution_box_title').innerHTML);
}
})
client.login(token);
var
is hoisted.
So since you have var fs
, there is a variable called fs
in the function / module / global scope where you have that statement.
It starts out undefined.
When you say fs = require('fs')
you assign the file system module to it. At that point it stops being undefined.
On the previous line when you try to read it with fs.readFileSync
, you haven't yet defined it so you get an error.
Order matters.