Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot find file in Node even though it has correct path

Tags:

node.js

fs

After executing this code:

const filename = "../../.dburl"
const url = fs.readFileSync(filename, 'utf-8')

I recieve the following error:

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

What I know so far:

1) The filepath is correct.

2) My application has permission to read the file.

3) .dburl is not read even when stored in the same directory as the application.

Any help is much appreciated ... Thanks!

like image 659
srpalo Avatar asked Jan 11 '17 22:01

srpalo


People also ask

Can not find module node path?

To solve the "Cannot find module path or its corresponding type declarations" error, install the types for node by running the command npm i -D @types/node . You can then import path with the following line of code import * as path from 'path' .

How do I find the path of a node js file?

We can get the path of the present script in node. js by using __dirname and __filename module scope variables. __dirname: It returns the directory name of the current module in which the current script is located. __filename: It returns the file name of the current module.

How do I resolve Cannot find module error using node js?

To fix the Cannot find module error, simply install the missing modules using npm . This will install the project's dependencies into your project so that you can use them. Sometimes, this might still not resolve it for you. In this case, you'll want to just delete your node_modules folder and lock file ( package-lock.

What is __ filename in node?

The __filename represents the filename of the code being executed. This is the resolved absolute path of this code file. For a main program, this is not necessarily the same filename used in the command line. The value inside a module is the path to that module file.


2 Answers

You can use the module-level variable '__dirname' to get the directory that contains the current script. Then you can use path.resolve to use relative paths.

console.log('Path of file in parent dir:', require('path').resolve(__dirname, '../app.js'));
like image 126
bsyk Avatar answered Sep 20 '22 18:09

bsyk


READING FILE ON SAME LEVEL

enter image description here

 //the server object listens on port 8080
    const PORT = 8080;
    var http = require("http");
    var fs = require("fs");
    var path = require("path");

    //create a server object:
    http
      .createServer((req, res) => {
        console.log("READING FILE: ", path.resolve(__dirname, "input.txt"));

        fs.readFile(path.resolve(__dirname, "input.txt"), (err, data) => {
          //error handling
          if (err) return console.error(err);

          //return file content
          console.log("FILE CONTENT: " + data.toString());
          res.write(data.toString());
          res.end();
          console.log("READ COMPLETED");
        });
      })
      .listen(PORT);

READING FILE ON OTHER LEVEL:

enter image description here

//the server object listens on port 8080
const PORT = 8080;
var http = require("http");
var fs = require("fs");
var path = require("path");

//create a server object:
http
  .createServer((req, res) => {
    console.log("READING FILE: ", path.resolve(__dirname, "./mock/input.txt"));

    fs.readFile(path.resolve(__dirname, "./mock/input.txt"), (err, data) => {
      //error handling
      if (err) return console.error(err);

      //return file content
      console.log("FILE CONTENT: " + data.toString());
      res.write(data.toString());
      res.end();
      console.log("READ COMPLETED");
    });
  })
  .listen(PORT);
like image 24
Hitesh Sahu Avatar answered Sep 18 '22 18:09

Hitesh Sahu