I am trying to parse a YAML file. I was able to parse the file properly but the comments in the YAML file are not getting read. Is there any way to do it? Attaching the parser code and config.json. Also attaching the screenshot of the file and output for reference.

var fs= require('fs');
var path= require('path');
var yaml = require('js-yaml')

var fname= "config.json"
var jPath= path.join(__dirname,"..","ConfigGen","Config",fname);
var jsString= fs.readFileSync(jPath, 'utf8')

// Get path for files from Config file
var tType= "cto"                        //Get this from input
var pth= JSON.parse(jsString)[tType]    //perform error handling

var cType = "jbod"                      //Get this from input
//Use that path 
fs.readdir(pth, function(err,files) {
    files.forEach(function(file){
        fName= cType+"_"+tType+"_uut.yaml-example";
        if(file==fName){
            var flContent= fs.readFileSync(path.join(pth,file),"utf8")
            // return path.join from here and use the next part in a separate function
            var data= yaml.safeLoad(flContent)[0][0]
            console.log(data)
            for (var index in data){
                var prefix = index
                for (idx in data[index]){
                    //console.log(prefix, idx ,data[prefix][idx])
                }
            }
            
        }
    })
})

Reiterating flyx's comment, according to the YAML spec on comments:

Comments are a presentation detail and must not be used to convey content information.

So assuming you're not going to be able to correlate the comments to any adjacent fields, you can just read the whole file as a string and match against any characters after a #

You can read the file and parse with this regex like this:

var { promises: fs } = require('fs');

(async() => {
    let path = "./config.yaml"
    let file = await fs.readFile(path, "utf8")
    let matches = file.matchAll(/#.*/g)
    let comments = [...matches].map(m => m[0])
    console.log(comments)
})()

If you have a yaml file that looks like this:

# block comment
env: dev
prop: value # inline comment

It will log the following:

[ '# block comment', '# inline comment' ]