How to check if a file exist in nuxt

2.3k views Asked by At

I'm on Nuxt 2.15.4 and i wanna check in my store codes if a file exist in nuxt directory by fs-extra package.

it is simple in modules because i can get files path by this code:

const path = require('path')
const fse = require('fs-extra');
const FilePath = path.join(this.options.rootDir, './static/myfile.json')
const fse = require('fs-extra');
fse.pathExists(FilePath, (err, exists) => {
    console.log(err) // => null
    console.log(exists) // => true
})

but in vuex store i dont have access to this.options.rootDir and this code is always return false:

export const actions = {
  async nuxtServerInit({dispatch, commit}) {
    if(process.server){
      const fse = require('fs-extra');
      fse.pathExists('~/static/myfile.json', (err, exists) => {
        console.log(err) // => null
        console.log(exists) // => false
      })
    }
  }
}

how can i get files fullpath or check if it exits??

#UPDATE

It looks like I had a little mistake in my file path so used ./static/myfile.json and the check is done!!

but got another problem!! I have another json file, when I'm trying to use Object.assign(mainfile, myfile) it won't work!!

here is a sample:

  async nuxtServerInit({dispatch, commit}) {
    let mainfile = require('../assets/mainfile.json')
    // if i use assign here it works and merge them together
    // let myfile = require('../assets/myfile.json')
    // Object.assign(mainfile, myfile)
    if(process.server){
      const fse = require('fs-extra');
      fse.pathExists('./static/myfile.json', (err, exists) => {
        if(exists){
          Object.assign(mainfile, myfile)
          commit('SET_FILE', mainfile); // this send the unmerged file to mutation
          console.log(mainfile); // but get the merged json here
        }
      })
      console.log(mainfile); // it is unmerged
    }
    console.log(mainfile); // it is unmerged
  }

2

There are 2 answers

0
Mojtaba Barari On BEST ANSWER

Ok, thanks to @kissu I found out the problem. As kissu mentioned in his answer's comment, commit is synchronous; I tried await action but didn't get the result; so I used pathExistsSync instead and done!!

  async nuxtServerInit({dispatch, commit}) {
    let myfile = {}
    let mainfile = require('../assets/mainfile.json')
    if(process.server){
      const fse = require('fs-extra');
      if(fse.pathExistsSync('./static/myfile.json')){
          myfile = require('../assets/myfile.json')
          Object.assign(mainfile, myfile)
      }
    }
    await dispatch('setMyFile', mainfile)
  }

#Update

require('../assets/mainfile.json') still throw error if file doesn't exist even with if(fse.pathExistsSync('./static/myfile.json')) statement so:

  async nuxtServerInit({dispatch, commit}) {
    let myfile = {}
    let mainfile = require('../assets/mainfile.json')
    if(process.server){
      const fse = require('fs-extra');
      if(fse.pathExistsSync('./static/myfile.json')){
          myfile = readJsonSync('./static/myfile.json')
          Object.assign(mainfile, myfile)
      }
    }
    await dispatch('setMyFile', mainfile)
  }
4
kissu On

For your updated question, be sure that exists is truthy, that you're entering in the loop and that mainfile is in the format that you're expecting.
Then, you could do

mainfile = {...mainfile, ...myfile} // rather than Object.assign