Error: ENOENT: no such file or directory, though the file exists
Asked Answered
A

2

9

This is my project structure:

enter image description here

This is index.js.

var express = require('express');
var router = express.Router();
var fs = require('fs');
var links = require('../models/Links');

var readline = require('linebyline');
var rl = readline('../data.txt');
router.get('/', function (req, res) {

    rl.on('line', function (line, lineCount, byteCount) {
        var data = line.split(',');
        var id = data[0];
        var url = data[1];           
    })
});

module.exports = router;

What am I doing wrong?

I tried rewriting

var rl = readline('/../data.txt');
var rl = readline(__dirname +'/../data.txt');

Nothing works.

Arraignment answered 10/8, 2016 at 15:39 Comment(8)
ENOENT is usually permissions. Does your application have the permission to access the file?Cleek
also, you should call readline right before rl.on('line')...Pipes
it was doing fine until i moved the code in index file. The code was in app.js file and worked fine.Arraignment
@NickBull yes, everyone has read&write permission to that fileArraignment
@Pipes did that, problem exists :(Arraignment
that was unrelated to the problem. it's just that the way your code looks now you won't be getting any lines (even after fixing the problem)Pipes
@Pipes understood! thanks for the tipArraignment
called readline right before rl.on('line') and changed to var rl = readline('./data.txt'); , now working!Arraignment
T
8

Your readline invocation is still going to be relative to the directory your app is running in (your root, where app.js resides), so I don't think you need the parent directory reference.

It should be just

var rl = readline('./data.txt');

Or if you want to use __dirname

var rl = readline(__dirname + '/data.txt');
Timbal answered 10/8, 2016 at 16:5 Comment(2)
thanks , figured that. i had to call readline right before rl.on('line') tooArraignment
Makes sense, although that's not what was causing the error in question: Error: ENOENT: no such file or directory, though the file existsTimbal
W
0

run this command it fixes this issue in an instant

npm audit fix --force
Watteau answered 18/7 at 5:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.