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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。