0

What I need:

Read a local JSON file inside NuxtJS as the page loads. So I can parse it into a prop within <option> tag.

What I have:

Lowdb (installed as dependency — to read the JSON file) inside a component, with this code:

computed: {
        resultFetchCamera: function() {
            const low = require('lowdb')
            const FileSync = require('lowdb/adapters/FileSync')
            const adapter = new FileSync('db.json');
            const db = low(adapter);
            let value = db.get('Size').map('Name').value();
            return value;
        }
    }
}

I got an error This dependency was not found: * fs in ./node_modules/lowdb/adapters/FileSync.js. Fixed with this solution. Which leads me to another error: TypeError: fs.existsSync is not a function. This solution helps out a bit but it also leads to other errors: TypeError: window.require is not a function and TypeError: FileSync is not a constructor. So, I undo the last solution and get back with the fs.existsSync error.

The question:

  1. How to fix the fs.existsSync error (in a NuxtJS environment)?
  2. Did I implement Lowdb to NuxtJS correctly?
Reinaldy
  • 205
  • 2
  • 5
  • 14

0 Answers0