My goal is to load ogg files in node.js I've tried ReadFile but hits me up with this error: Could not read file. [Error: ENOENT: no such file or directory, open 'Main\game\songs\test\Voices.ogg'] and the file does exist.
Code:
fs.readdir('./game/songs', function(err, files){
if (err) {
console.log("Could not list the directory.", err)
return
}
files.forEach(function(file, index){
fs.readFile('./game/songs/' file "/Inst.ogg", function(err, data){
if (err) {
console.log("Could not read file.", err)
return
}
})
fs.readFile('./game/songs/' file "/Voices.ogg", function(err, data){
if (err) {
console.log("Could not read file.", err)
return
}
})
})
})
CodePudding user response:
You should pass the absolute path of the file, use the built-in path
module's join()
function:
const path = require('path');
const fs = require('fs');
fs.readFile(path.join(__dirname, 'YOUR_PATH_HERE'), function(err, data) {});