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!
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' .
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.
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.
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.
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'));
//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);
//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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With