Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 920
Shafayat Alam Avatar asked Aug 10 '16 15:08

Shafayat Alam


People also ask

How do I fix error Enoent No such file or directory?

To resolve the ENOENT warning message, you need to add a package. json file in the directory where you run the npm install command. And then run your npm install command again. This time, the warning message should not appear.

What does error code Enoent mean?

It's an abbreviation of Error NO ENTry (or Error NO ENTity), and can actually be used for more than files/directories. It's abbreviated because C compilers at the dawn of time didn't support more than 8 characters in symbols. Follow this answer to receive notifications. edited Jun 4, 2019 at 14:30. community wiki.

Why is there no such file or directory?

The error "FileNotFoundError: [Errno 2] No such file or directory" is telling you that there is no file of that name in the working directory. So, try using the exact, or absolute path. In the above code, all of the information needed to locate the file is contained in the path string - absolute path.


1 Answers

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');
like image 78
Wake Avatar answered Sep 22 '22 06:09

Wake