I'm fairly new to Javascript/NodeJS (first time) and have been having an issue with memory leak errors. I keep getting the following:
Possible EventEmitter memory leak detected. 11 SIGINT listeners added. Use emitter.setMaxListeners() to increase limit
I can't seem to find a way of fixing it. I tried using require('events').EventEmitter.defaultMaxListeners = 0;
which seemed to work on a localhost, but running it on a linux server gives me multiple "child process xxxx still did not exit, sending a SIGTERM" errors and forces me to have to restart the server.
var html = require('pa11y-reporter-html');
var pa11y = require('pa11y');
var fs = require("fs");
//require('events').EventEmitter.defaultMaxListeners = 0;
async function runPa11y(url) {
try {
let results = await pa11y(url);
let htmlResults = html.results(results);
return htmlResults
} catch (err) {
console.log("Error: " + err)
}
}
function listScript() {
const args = process.argv;
const os = require('os');
const siteName = args[2];
pathToSiteDir = os.homedir() + "/" + siteName
try {
fd = fs.openSync(pathToSiteDir + '/audits/results-pally.html', 'w');
} catch (err) {
console.log("Could not open results.html" + err)
} finally {
if (fd !== undefined)
fs.closeSync(fd);
}
var array = fs.readFileSync(pathToSiteDir + "/crawled.txt").toString().split("\n");
array = array.filter(function(entry) { return entry.trim() != ''; });
(function theLoop (i) {
setTimeout(function () {
console.log("url: " + array[i])
let reply = runPa11y(array[i])
process.removeAllListeners('exit')
reply.then(function(result) {
try {
fd = fs.openSync(pathToSiteDir + '/audits/results-pally.html', 'a');
fs.appendFileSync(fd, result + "<br>", 'utf8');
} catch (err) {
console.log("Could not open results.html" + err)
} finally {
if (fd !== undefined)
fs.closeSync(fd);
}
});
--i
if (i >= 0) {
theLoop(i);
console.log("Links left to audit: " + i)
}
}, 300);
})(array.length -1);
}
listScript()
It might be that the loop is repeated too quickly and therefore Node is overwhelmed. Try putting the loop condition inside the promise, like that:
(function theLoop (i) {
setTimeout(function () {
console.log("url: " + array[i])
let reply = runPa11y(array[i])
process.removeAllListeners('exit')
reply.then(function(result) {
try {
fd = fs.openSync(pathToSiteDir + '/audits/results-pally.html', 'a');
fs.appendFileSync(fd, result + "<br>", 'utf8');
} catch (err) {
console.log("Could not open results.html" + err)
} finally {
if (fd !== undefined)
fs.closeSync(fd);
}
--i
if (i >= 0) {
theLoop(i);
console.log("Links left to audit: " + i)
}
});
}, 300);
})(array.length -1);