fs 读不到路径下文件 express

https://stackoverflow.com/questions/48469666/error-enoent-no-such-file-or-directory-open-moviedata-json

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.

你可能感兴趣的:(fs 读不到路径下文件 express)