0

"TypeError: fs.existsSync is not a function"

I get this error when i want to generate a csv file and put some data in it.

const objectstocsv = require('objects-to-csv');

export default {
    data () {
        return {
        data : [
            {code: 'CA', name: 'California'},
            {code: 'TX', name: 'Texas'},
            {code: 'NY', name: 'New York'},
            ],
        }
    },
    methods: {

        async exportData(){
            const csv = new objectstocsv(this.data);

            await csv.toDisk('./file.csv');

            console.log('ok')
            },

    }
}

Function is called when I click on a button. I don't get it

  • The question is incomplete. Currently it appears that you try to use Node-only lib in client-side app, which is not possible – Estus Flask Jul 05 '22 at 13:19

1 Answers1

0

The fs module depends on Node.js. It will not run in the browser.

Presumably, you have a webpack configuration that silently replaces it with an object that has a subset of browser-compatible features (which won't include any feature that actually touch the file system).

Either:

  • Move your code that uses the fs module to a web service and access it using Ajax
  • Replace your code that uses the fs module with code that will run in the browser (such as this answer does).

The first step towards either of these would be to replace toDisk with toString.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335