Javascript 错误:ENOENT:没有这样的文件或目录,打开“/moviedata.json”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48469666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:08:03  来源:igfitidea点击:

Error: ENOENT: no such file or directory, open '/moviedata.json'

javascriptjson

提问by Daniel ORTIZ

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

我在节点 Js 工作。当我尝试加载文件时:moviedata.json,使用以下几行:

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)

错误:ENOENT:没有这样的文件或目录,在 Object.fs.openSync (fs.js:640:18) 的 Error (native) at Object.fs.readFileSync (fs.js:508) 处打开“./moviedata.json” :33) 在对象。(/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)

错误:ENOENT:没有这样的文件或目录,在 Object.fs.openSync (fs.js:640:18) 的 Error (native) at Object.fs.readFileSync (fs.js:508:33) 处打开“moviedata.json” ) 在对象。(/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.

要在与 js.txt 相同的文件夹中读取它的文件。

enter image description here

在此处输入图片说明

But im don't understand what im doing wrong

但我不明白我做错了什么

回答by chiswicked

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

fs.readFileSync('moviedata.json', 'utf8')moviedata.json在您运行应用程序的目录中MoviesLoadData.js查找,而不是在您的文件所在的目录中查找。

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

假设你跑node aws/MoviesLoadData.js/Users/dortiz/Documents/NodeJS/pruebas/zwfs.readFileSync('moviedata.json', 'utf8')会寻找moviedata.jsonin /Users/dortiz/Documents/NodeJS/pruebas/zw,而不是 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.

如果您要使用我给定的示例运行脚本,则需要在 json 文件的路径之前添加以正确引用它。

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.

我不确定您如何运行代码,因此我的示例可能无法在您的代码库中运行,但希望了解您出错的地方将有助于调试您的代码。