Renaming a file in JavaScript Node

I have this code in my Python3 Function Node

os.rename('cont.json', '%s.json'% formated_string)
msg['payload']='%s.json'% formated_string
return msg

It renames the file cont.json as formated_string.json (formated_string is the current date). Then it returns the name of the newly renamed file.

I need to do this in the JavaScript Function Node but I can't seem to find out how to write the same simple thing in JavaScript. Help?

You can write to a file with the file node, which accepts msg.filename, which you can generate.

@xoani, if you absolutely have to do this inside a function node using JavaScript, it is possible with a bit of effort. First, add


to the functionGlobalContext object in your settings.js file. Then, in your function node, something like this should do the trick:

var fs = global.get('fs');
fs.rename('myfile', 'myrenamedfile', function (err) {
    if (err) {
    } else {
        node.warn('File renamed');

The file names should be the complete path names, and you will need to be more serious about error handling.

This is described in the Writing Functions section of the documentation. Note that comments in the settings.js file suggest the deprecated usage, instead of global.get. That probably still works.