0

I am pretty new in Vue.js and I faced with the issue, that I can't update/write some data in my local JSON file.

Let's say that I have data.json enter image description here

And I would like to add a new entry to this file.

I use the basic code :

  let d = JSON.stringify({ x: 5, y: 6 });
        const fs = require('fs');
        try { 
             fs.writeFileSync('data.json', d, 'utf-8'); 
            } 
         catch (e) 
            { 
            alert(e);
            }

As results I have an error message:

"TypeError: fs.writeFileSync is not a function"

How do I resolve this?

1
  • fs is a Node.js module, are you sure you can use it in Vue? Commented Oct 15, 2020 at 14:07

1 Answer 1

2

Vue.js is a front-end framework. It runs in the user's browser, so it doesn't have direct access to files on the server. The fs module you're trying to use exists only in node.js, which runs on the server. That's why it appears to be undefined when you try to access it from your front-end vue code.

If you want to change files based on user actions, you need to set up a server for vue to send requests to, and have the server modify the files. Although, if you're trying to write changes to a JSON file, you probably want a database instead. Maybe take a look at MongoDB?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.