0

I have used nuxt.js as web apps framework and UI framework is Vuetify.js. I thought checking file existence of my web apps to use "fs.existsSync" of node.js, but it didn't work. I tried to test like below.

    <template>
 <div>
  <v-btn @click="test">fileCheck
  </v-btn>
 </div>
</template>
<script lang="ts">
import {Component, Vue} from 'nuxt-property-decorator'
const fs = require('fs')

@Component({})
export default class extends Vue {
 test() {
  const url = './static/test.png'
    if (fs.existsSync(url)) {
      alert('exist!')
    } else {
      alert('Not exist!')
    }
 }
}
</script>

this code is really simple, I just click the botton and show the alert to notice file exist or not. But never happen and error occurred like.

client.js?06a0:97 TypeError: fs.existsSync is not a function

Does any give me advise, please?

drunkdolphin
  • 765
  • 1
  • 16
  • 46
  • `fs.*` are server side (nodejs) functions - they don't work in a browser – Bravo Aug 05 '21 at 02:29
  • Thanks for the comments. My app is SPA. that's why it can't use fs,*. In browser, is there any solution to check file existence? – drunkdolphin Aug 05 '21 at 02:35
  • `My app is SPA. that's why it can't use fs,*. In browser` if you say so - no website can use `fs.*` ... SPA, PWA, regular websites, nothing, so, it's nothing to do with "SPA" - so, where do you want to check for file existence? on your server? do it in server code. On the client? not possible 100% – Bravo Aug 05 '21 at 02:40
  • I want to do check in client side. In my questions background, my customer request the logo data change depend on the customer, so after my project deploy to the AWS S3 bucket, and then "static/test.png" is going to be changed depend on the customer. – drunkdolphin Aug 05 '21 at 02:55
  • Some customer will not have "test.png" file, so possible to occur reference error – drunkdolphin Aug 05 '21 at 02:58
  • Before error, I want to check file exists. and I thought it was better way to check. – drunkdolphin Aug 05 '21 at 02:59
  • I may have mentioned *On the client - not possible 100%* - so, the answer is, it's not possible, 100% ... I mean, which folder would you look in anyway! – Bravo Aug 05 '21 at 03:25
  • Thanks for advice. I try to find other way. – drunkdolphin Aug 05 '21 at 05:23
  • Something like this is the closest you can do on the client: https://stackoverflow.com/a/68027966/8816585 – kissu Aug 05 '21 at 08:22
  • Thanks for the advise Mr. kissu!! I really really appreciate it all the time. I would like to use as reference. – drunkdolphin Aug 05 '21 at 12:08

0 Answers0