Reputation: 45
Im trying to use writefile, but for some reason it does it outside of ./events folder..
im trying to use fs like,
fs.writeFile("./level.json", JSON.stringify(storage), (err) => {
console.log(err)
})
xp.js is trying to write the level.json in the same folder.
Upvotes: 0
Views: 281
Reputation: 366
To be more consistent across different architectures I suggest you to use path module. So in your case:
fs.writeFile(require('path').resolve('events', 'level.json'), JSON.stringify(storage), (err) => {
console.log(err)
});
Upvotes: 0
Reputation: 1927
This happens because ./
references the current working directory, so wherever you ran the script from. If you'd like the path to reference the same folder where the currently running js file is, use __dirname
instead like so:
fs.writeFile(`${__dirname}/level.json`, JSON.stringify(storage), (err) => {
console.log(err)
})
Upvotes: 2