Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  2] [ 2]  / answers: 1 / hits: 34930  / 6 Years ago, fri, january 26, 2018, 12:00:00

im working in node Js.
When im trying to load a file: moviedata.json, with this lines:



var allMovies = JSON.parse(fs.readFileSync('moviedata.json', 'utf8'));


Shows:




Error: ENOENT: no such file or directory, open './moviedata.json'
at Error (native)
at Object.fs.openSync (fs.js:640:18)
at Object.fs.readFileSync (fs.js:508:33)
at Object. (/Users/dortiz/Documents/NodeJS/pruebas/zw/aws/MoviesLoadData.js:13:31)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)



Error: ENOENT: no such file or directory, open 'moviedata.json'
at Error (native)
at Object.fs.openSync (fs.js:640:18)
at Object.fs.readFileSync (fs.js:508:33)
at Object. (/Users/dortiz/Documents/NodeJS/pruebas/zw/aws/MoviesLoadData.js:13:31)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)




The file to read its in same folder that js.



enter



But im don't understand what im doing wrong


More From » json

 Answers
8

fs.readFileSync('moviedata.json', 'utf8') will look for moviedata.json in the directory from where you ran your application, not in the directory where your MoviesLoadData.js file is located.



Suppose you ran node aws/MoviesLoadData.js from /Users/dortiz/Documents/NodeJS/pruebas/zw, fs.readFileSync('moviedata.json', 'utf8') would look for moviedata.json in /Users/dortiz/Documents/NodeJS/pruebas/zw, not in /Users/dortiz/Documents/NodeJS/pruebas/zw/aws



If you were to run your script with my given example, you'd need to prepend the path to the json file to correctly reference it.



fs.readFileSync(__dirname + '/moviedata.json', 'utf8')


I'm not sure how you run your code, so my example may not work in your codebase, but hopefully understanding where you went wrong will help debug your code.


[#55343] Wednesday, January 24, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leslijessalyng

Total Points: 650
Total Questions: 85
Total Answers: 109

Location: Croatia
Member since Mon, Sep 6, 2021
3 Years ago
leslijessalyng questions
Fri, Feb 21, 20, 00:00, 4 Years ago
Tue, Jul 30, 19, 00:00, 5 Years ago
Fri, Jul 5, 19, 00:00, 5 Years ago
Wed, Mar 13, 19, 00:00, 5 Years ago
;