Saving a local file with Node.js

January 05, 2020 - 1 min read

Find out how to store a JSON file on a user's machine and retrieve it later using Node.js.

Creating the File

const storage = require('node-persist');

await storage.init({ dir: '/Applications/my-app' });
await storage.setItem('name', 'myName');

Running these commands creates a file that looks like this:

  "key": "name",
  "value": "myName"

Retrieving the Data

await storage.init({ dir: '/Applications/my-app' });
const item = await storage.getItem('name');

To access the data, the same init command is called followed my the getItem method, while providing the key used while saving.

Real World Example

async function saveToDisk(dataToSave) {
  await storage.init({ dir: '/Applications/my-app' }).catch(err => console.error(err));
  await storage.setItem('myData', dataToSave);

Here is an example of one way you might create a function using these features. For a similar process used to store a token, view this code snippet for my project slack-location-manager.

Further Reading

Boston, MA 2021