# Renaming a file in JavaScript Node

**URL:** https://discourse.nodered.org/t/renaming-a-file-in-javascript-node/10995
**Category:** General
**Created:** [10 May 2019 11:40 UTC](https://discourse.nodered.org/t/renaming-a-file-in-javascript-node/10995 "2019-05-10T11:40:58Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![xoani](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/xoani/32/7844_2.png) [@xoani](https://discourse.nodered.org/u/xoani)
#### Post date: [10 May 2019 11:40 UTC](https://discourse.nodered.org/t/renaming-a-file-in-javascript-node/10995/1 "2019-05-10T11:40:58Z")

</div>

I have this code in my Python3 Function Node

```auto
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?

---

<div class="post-metadata">

### Author: ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)
#### Post date: [10 May 2019 12:57 UTC](https://discourse.nodered.org/t/renaming-a-file-in-javascript-node/10995/2 "2019-05-10T12:57:42Z")

</div>

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

---

<div class="post-metadata">

### Author: ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)
#### Post date: [10 May 2019 16:57 UTC](https://discourse.nodered.org/t/renaming-a-file-in-javascript-node/10995/3 "2019-05-10T16:57:50Z")

</div>

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

`fs:require('fs')`

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

```auto
var fs = global.get('fs');
fs.rename('myfile', 'myrenamedfile', function (err) {
    if (err) {
        node.warn(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](https://nodered.org/docs/writing-functions#loading-additional-modules) section of the documentation. Note that comments in the `settings.js` file suggest the deprecated usage, `context.global` instead of `global.get`. That probably still works.
