Node.js Writing into YAML file
Asked Answered
E

2

10

I'm using Node.js and I'm having trouble figuring out how could I read a YAML file, replace a value in it, and write the updated value to the YAML file.

I'm currently using the module "yamljs" which allows me to load the YAML file and I've managed to edit the value in the loaded Object.

The only part I need help with, is how to write to the YAML file. Cause for some reason, I can't find the solution for that anywhere, and I'm not even sure if I could use the module for that.

The module does have some command line tools, but I'm not too sure how to use those either.

Earthshine answered 2/10, 2018 at 21:12 Comment(6)
According to the documentation of yamljs this is not possible. You might have a look at other libraries. js-yaml looks it is doing both (but the repos doesnt seems maintained, yet there are a lot of DLs on NPM).Woaded
1) It is YAML, not YML, and the file extension/suffix for those files should be .yaml unless your filesystem does not support that. 2) Most YAML parsers/loaders drop meaninful information such as comments, ids doing such round tripping (load-modify-save), the only exeption that I know of is my Python based ruamel.yaml.Platinumblond
I'll give the js-yaml a shot, thanks :3Earthshine
@Earthshine Were you able to write to yaml using js-yaml?Reference
Yes I was, I just don't know how to close this lmao.Earthshine
Nvm I answered it myself :D Marking it as the correct answer and closing this.Earthshine
E
14

The module "js-yaml" worked for my case. https://github.com/nodeca/js-yaml

Here's the code I used:

const yaml = require('js-yaml');
...

let doc = yaml.safeLoad(fs.readFileSync('./Settings.yml', 'utf8'));
doc.General.Greeting = newGreet;
fs.writeFile('./Settings.yml', yaml.safeDump(doc), (err) => {
    if (err) {
        console.log(err);
    }
});
Earthshine answered 5/3, 2019 at 16:26 Comment(2)
what do you have into Settings.yml? And what into newGreet?Finnegan
Why do you ask? newGreet is a local variable defined by the function, and my Settings.yml's structure had it like in my example for it. Obviously you cannot copypaste it, but it shows the basic idea on how to use it.Earthshine
R
0

You can use yaml.dump() function with some optional configurations:

try {
    const file = path.resolve(__dirname, `./config/`, CONFIG.yml)

    const fileContents = fs.readFileSync(file, 'utf8');
    const baseConfig = yaml.load(fileContents);

    // change the baseConfig file to whatever you want...

    const newYaml = yaml.dump(baseConfig, {
        lineWidth: -1, // Ensure no line
        noRefs: true, // Avoid unnecessary references in the output
        condenseFlow: true // Prevent multi-line block scalars
    });

    fs.writeFileSync(configFile, newYaml, 'utf8');
    console.log(`Configuration updated successfully: ${configFile}`);
} catch (e) {
    console.error(`Error loading or updating config file ${configFile}:`, e);
}
Rauch answered 24/7 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.