[英]Uncaught Error: ENOENT, no such file or directory
const csv = require('csv-parser') var fs = require('browserify-fs'); const net1 = []; const net2 = []; let mood = 'Comedy'; let type = 'Movie'; let rating = 'PG-13'; let list = []; fs.createReadStream(__dirname +'netflix_titles.csv').pipe(csv({})).on('data', (data) => net1.push(data)).on('end', () => { for (let i = 0; i < net1.length; i++) { if (net1[i]['type'] === type) { if (net1[i]['rating'] === rating) { list.push(net1[i]['title']) } } } }); fs.createReadStream(__dirname + 'netflix_list.csv').pipe(csv({})).on('data', (data) => net2.push(data)).on('end', () => { for (let i = 0; i < net2.length; i++) { if (list.includes(net2[i]['title']) && net2[i]['genres'].includes(mood)) { console.log(net2[i]['title']) } } });
With this code, I keep getting the ENOENT error for 'netflix_titles.csv' and 'netflix_list.csv' even though they are in the root directory.使用此代码,我不断收到“netflix_titles.csv”和“netflix_list.csv”的 ENOENT 错误,即使它们位于根目录中。 I also bundled this script into a bundle.js with browserify.
我还使用 browserify 将此脚本捆绑到 bundle.js 中。 Both the bundle.js and this script.js are in the root directory.
bundle.js 和这个 script.js 都在根目录中。
The error code ENOENT (which means Error: No ENTry/ENTity ) means that the requested file is not available.错误代码ENOENT (表示Error: No ENTry/ENTity )表示请求的文件不可用。 In this case, this means that either the file located on
__dirname + 'netflix_titles.csv'
or the __dirname + 'netflix_list.csv'
cannot be found.在这种情况下,这意味着找不到位于
__dirname + 'netflix_titles.csv'
或__dirname + 'netflix_list.csv'
上的文件。
That said, the __dirname
variable in Node.js outputs the absolute path where the script is located.也就是说,Node.js 中的
__dirname
变量输出脚本所在的绝对路径。 However, the value of that variable is guaranteed not to include a trailing slash ( /
).但是,保证该变量的值不包含尾部斜杠 (
/
)。
So assume that you're running this script from C:\Users\Administrator\Projects\nodejs-example\
, and the __dirname
for that is C:\\Users\\Administrator\\Projects\\nodejs-example
.因此,假设您从
C:\Users\Administrator\Projects\nodejs-example\
运行此脚本,并且__dirname
为C:\\Users\\Administrator\\Projects\\nodejs-example
。 Combining that with __dirname + 'netflix_titles.csv'
, the result will be C:\\Users\\Administrator\\Projects\\nodejs-examplenetflix_titles.csv
and NOT C:\\Users\\Administrator\\Projects\\nodejs-example\\netflix_titles.csv
. Combining that with
__dirname + 'netflix_titles.csv'
, the result will be C:\\Users\\Administrator\\Projects\\nodejs-examplenetflix_titles.csv
and NOT C:\\Users\\Administrator\\Projects\\nodejs-example\\netflix_titles.csv
。
So to solve this problem, add a leading slash ( /
) so it will become __dirname + '/netflix_titles.csv'
and __dirname + '/netflix_list.csv'
, respectively.因此,为了解决这个问题,添加一个前导斜杠 (
/
),使其分别变为__dirname + '/netflix_titles.csv'
和__dirname + '/netflix_list.csv'
。 Windows will tolerate which slash you'll be using (either /
, or \\
(escaped \
character)), but using the forward slash /
is more recommended so your program can be run on non-Windows devices, too. Windows 将允许您使用哪个斜杠(
/
或\\
(转义的\
字符)),但更推荐使用正斜杠/
,这样您的程序也可以在非 Windows 设备上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.