I am reading a package.json file of a project, and trying to identify if the next framework dependency exists under my dependencies
key. And everything is alright, the console.log('We made it')
that you can see works, when I invoke it with functionIamStuckWith;
What I want to get out of this is a boolean value that is either true or false, to use it in a condition and do things conditionally. That is why I return true or false.
I tried and I couldn't make it, I created a private (also tried public) isNextJsFramework: boolean;
, then initialized it in the constructor and in my promise function I tried to set the field
like this.isNextJsFramework = true
. What I got out of it tho, is my Visual Studio Code warning me that 'isNextjsFramework' is declared but its value is never read.
.
Do u have any idea how can I fix the syntax? Or why this doesn't work?
class Parser {
private options: ParserOptions;
private isNextJsFramework: boolean;
// Also tried to initialize it here
// private isNextJsFramework = false;
constructor(options: ParserOptions, isNextJsFramework: boolean) {
this.isNextJsFramework = isNextJsFramework;
this.options = options;
}
public parse() {
return new Promise((resolve) => {
const functionIAmStuckWith = this.getPackageJson().then(async (path: string[]) => {
const packageJsonFile = await JSON.parse(
fs.readFileSync(path[0], 'utf8')
);
if (packageJsonFile) {
if (packageJsonFile.dependencies) {
Object.keys(
packageJsonFile.dependencies).forEach((dependency) => {
if (dependency.match(/^next$/)) {
// I get the error when I use it here.
// If I just type this.isNextJsFramework without assigning a value, it does not complain
console.log("We made it");
this.isNextJsFramework = true;
}
});
}
}
});
functionIamStuckWith;
......... More here and at some point it is resolved