How to write to files with Node.js
Tags: node, beginners
Published: 2021-01-23 20:32:46

---
title: "How to write to files with Node.js"
published: true
tags: [nodejs, beginners]
canonical_url: https://www.emgoto.com/nodejs-write-file/
cover_image: https://www.emgoto.com/nodejs-write-file-dev.png
---

In this post we’ll be covering how you can use the [fs module](https://nodejs.dev/learn/the-nodejs-fs-module/) in Node.js to write to and modify files with `writeFile()` and `writeFileSync()`. We’ll also take a look at an alternative library we can use called [replace-in-file](https://www.npmjs.com/package/replace-in-file).

This post assumes you have a general knowledge of JavaScript syntax, but doesn’t expect any Node.js-specific knowledge.

> If you want to learn how to write and run a Node.js script, I have covered that in my previous post on [automating file renaming with Node.js](https://www.emgoto.com/nodejs-rename-file).

## Overwrite a file with Node.js
### Using fs writeFile to write to files (asynchronous)
Let's jump straight into things with an fs `writeFile()` example:

```js
const { writeFile } = require('fs');

const content = 'Hello world';
const file = '/Users/emma/src/emgoto.com/file.txt';

const callback = (err) => { /** Handle errors */ };

writeFile(file, content, callback);
```

This will change the contents of the file to contain the string “Hello world”.

The callback function will be executed once the file has been successfully written to, or it errors out.

### Using fs writeFileSync to write to files (synchronous)

If you want an synchronous version, you can use `writeFileSync`:

```js
const { writeFileSync } = require('fs');

const content = 'Hello world';
const file = '/Users/emma/src/emgoto.com/file.txt';

writeFileSync(file, content);
```

## Read from a file, then modify part of it with Node.js
### Using fs readFileSync to read from a file (synchronous)

If you want to modify a file, rather than write over its contents, you'll first need to read it. We can use `readFileSync` here: 

```js
const { readFileSync } = require('fs');

const content = readFileSync(file, 'utf8');
const newContent = content.replace('Hello', 'Goodbye');

writeFile(file, newContent, () => {});
```

After we read the file, we will have a copy of the file's contents. We can then use JavaScript's `replace` function to modify it, before updating the file with the new contents. 

### Using fs readFile to read from a file (asynchronous)
The asynchronous version of `readFileSync` is `readFile`:

```js
const { readFile, writeFile } = require('fs');

const callback = (err, data) => {
	const newContent = data.replace('Hello', 'Goodbye');
    writeFile(file, newContent);
}

readFile(file, 'utf8', callback);
```

Here, when we successfully get the contents of the file (or it errors out), the callback function will be called. Then we can use `writeFile` to modify the contents of the file.

## How to effectively use JavaScript’s replace function
In the above example, you’ll notice we were passing strings into JavaScript’s replace function:

```js
content.replace('Hello', 'Goodbye');
```

This only replaces the first instance of "Hello" with "Goodbye". If you want to replace more than one instance, you’ll can make use of Regex:

```js
content.replace(/Hello/g, 'Goodbye');
```

If you're rusty on your Regex (or haven’t used it before):
* Regex patterns live inside of `//`
* The `g` on the end signifies that it is “global”, which means it will find all occurrences.
* `/Hello/g` will find all instances of the string “Hello” 

> Regex can do many more cool things, but I won’t be diving into that in this post! When writing your Regex patterns, I recommend testing it with a tool like [RegExr](https://regexr.com/).

As well as strings and Regex, we can also pass in functions!

```js
const addSpacesBetweenLetters = (string) => 
    string.split('').join(' ');

content.replace(/Hello/g, addSpacesBetweenLetters);
```

This would convert all instances of “Hello” to “H e l l o”.

## Using replace-in-file to write to files
Instead of fs readFile and writeFile, there’s also a handy library we can use called [replace-in-file](https://www.npmjs.com/package/replace-in-file).

To replace all instances of "Hello" with "Goodbye", you would do the following:

```js
const replace = require('replace-in-file');

const options = {
    files: fileName,
    from: /Hello/g,
    to: 'Goodbye',
};

replace(options);
```

You'll notice that we no longer have to open the file and get its contents - `replace-in-file` will handle that for you.

The `from` and `to` variables accept strings, Regex patterns and functions.

## fs writeFile vs replace-in-file
For simple use cases, fs writeFile will get the job done. There are a couple of extra features that the `replace-in-file` library has which make it pretty useful.

It can replace content in multiple files at once:

```js
const replace = require('replace-in-file');

const options = {
    files: [fileName, anotherFileName],
    from: /Hello/g,
    to: 'Goodbye',
};

replace(options);
```

It can also replace things in bulk:

```js
const options = {
    files: [fileName, anotherFileName],
    from: [/Hello/g, /Foo/g],
    to: ['Goodbye', 'Bar'],
}
```

When you're using arrays with `to` and `from`:

* The `from` in the 0th position will convert to the `to` in the 0th position
* The `from` in the 1st position will convert to the `to` in the 1st position

And so on!