fs.readFileSync throwing errno 4058 - node.js

When I try to access the json file iike this:
...
var configurationFile = fileSystem.readFileSync(
"../configurationFile.json"
);
var apiSecretKey = JSON.parse(configurationFile.toString()).secret;
var url = JSON.parse(configurationFile.toString()).db_url;
var dbName = JSON.parse(configurationFile.toString()).db_name;
...
i get this error
Error: ENOENT: no such file or directory, open '../configurationFile.json'
at Object.<anonymous> (C:\Users\Samuel\Documents\GitHub\project-1\Back-End\API\Services\loginservice.js:7:36)
...
at Object.<anonymous> (C:\Users\Samuel\Documents\GitHub\project-1\Back-End\index.js:11:26) {
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path: '../configurationFile.json'
this is the dir structure
API
...Services
......loginservice.js
...configurationFile.js
index.js
How do i get around this besides this wrapping the config in a module. Is this a windows specific issue?

Related

Why I am getting error as operation not permitted for copying files from one folder to another folder using nodejs?

I am trying to copy all the files from a folder to destination folder. I have also imported the fs and path package inside the code. Also I have removed the read-only option tick from properties of the foler and its files respectively. But still getting the below last error. How could I resolve this error?
This is my code:
function sync(source, destination) {
var files = [];
//var targetFolder = path.join( destination, path.basename( source ) );
var targetFolder = destination;
if ( !fs.existsSync( targetFolder ) ) {
fs.mkdirSync( targetFolder );
}
// Copy
if ( fs.lstatSync( source ).isDirectory() ) {
files = fs.readdirSync( source );
files.forEach( function ( file ) {
var curSource = path.join( source, file );
if ( fs.lstatSync( curSource ).isDirectory() ) {
fs.copyFolderRecursiveSync( curSource, targetFolder );
} else {
fs.copyFileSync( curSource, targetFolder );
}
});
}
};
This is how I passing the source and the destination parameters:
sync('D:/New folder', 'D:/New folder (2)');
But I am getting this error:
calling
internal/fs/utils.js:307
throw err;
^
Error: EPERM: operation not permitted, copyfile 'D:\New folder\New Text Document (1).txt' -> 'D:/New folder (2)'
at Object.copyFileSync (fs.js:1991:3)
at C:\Users\em\Desktop\influence\memecache.js:171:23
at Array.forEach (<anonymous>)
at sync (C:\Users\em\Desktop\influence\memecache.js:165:18)
at Object.<anonymous> (C:\Users\em\Desktop\influence\memecache.js:151:3)
at Module._compile (internal/modules/cjs/loader.js:1063:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
at Module.load (internal/modules/cjs/loader.js:928:32)
at Function.Module._load (internal/modules/cjs/loader.js:769:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) {
errno: -4048,
syscall: 'copyfile',
code: 'EPERM',
path: 'D:\\New folder\\New Text Document (1).txt',
dest: 'D:/New folder (2)'
}
fs.copyFileSync( curSource, targetFolder + file );

How to send a midi file as a Message Attachment

I made my bot generate a random midi file, I just need to know how to send it onto discord
var write = new MidiWriter.Writer([track]);
message.channel.send(new Discord.Attachment(write.dataUri()))
.catch(console.error);
The error it gives me is:
{ [Error: ENOENT: no such file or directory, stat 'C:\Users\bossay\Desktop\Botz\CropBot\data:audio\midi;base64,TVRoZAAAAAYAAAABAIBNVHJrAAAASgCQA0CEAIADQACQAEAAgABAAJACQIQAgAJAAJADQACAA0AAkANAhACAA0AAkABAggCAAEAAkABAhACAAEAAkAJAggCAAkAA\y8A']
errno: -4058,
code: 'ENOENT',
syscall: 'stat',
path:
'C:\\Users\\bossay\\Desktop\\Botz\\CropBot\\data:audio\\midi;base64,TVRoZAAAAAYAAAABAIBNVHJrAAAASgCQA0CEAIADQACQAEAAgABAAJACQIQAgAJAAJADQACAA0AAkANAhACAA0AAkABAggCAAEAAkABAhACAAEAAkAJAggCAAkAA\\y8A' }
I am going to assume you are using the midi-writer-js npm module here.
The Discord.Attachment does not accept a data uri, you want to pass a buffer instead.
(Also to not send a file.jpg you need to set a file name)
To get such a buffer you can pass the Uint8Array retrieved via Writer#buildFile to Buffer.from.
Putting all of it together could look like this.
var write = new MidiWriter.Writer([track]);
var buffer = Buffer.from(write.buildFile())
message.channel.send(new Discord.Attachment(buffer, 'file.midi'))
.catch(console.error);

Error: { [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }

Fotolog is closing recently and I wanted to backup all of mine photos on there. Looking for something useful, I found this project: https://github.com/firstdoit/fotolog-backup
I installed npm and coffee with the instructions on the readme
but when I try to run:
coffee fotolog-build-index.coffee ticinowriting2
I receive this error:
doc#doc-mtn:~/fotolog-backup$ coffee fotolog-build-index.coffee ticinowriting2
{ [Error: getaddrinfo ENOTFOUND] code: 'ENOTFOUND', errno: 'ENOTFOUND', syscall: 'getaddrinfo' }
The code in "fotolog-build-index" is:
request = require 'request'
$ = require 'cheerio'
fs = require 'fs'
imageURLs = []
user = process.argv[2]
throw new Error("Usage: coffee fotolog-build-index.coffee <username>") unless user
buildIndexFromPage = (page) ->
request "http://www.fotolog.com.br/#{user}/mosaic/#{page}", (err, resp, html) ->
return console.error(err) if err
console.log "finished page #{page}... adding images"
images = $.load(html)("a.wall_img_container img")
images.map (i,img) ->
imageURLs.push $(img).attr("src").replace('_t','_f')
if images.length < 30
console.log imageURLs
console.log "got #{imageURLs.length} images"
fs.writeFileSync('index.json', JSON.stringify(imageURLs))
else
buildIndexFromPage(page + 30)
buildIndexFromPage(0)
Sorry for my bad english, im from switzerland and i know quite nothing about this kind of code
That error means the DNS resolver could not resolve the hostname (www.fotolog.com.br) to an IP address. I can verify this is indeed the case (dig gets NXDOMAIN). So unless you have the IP address there's not much you can do.
You could try just www.fotolog.com since that does seem to resolve yet, however the message on that site indicates data should have been downloaded before February 20, 2016.

NodeJS: readdir() returns "undefined" instead of the list of files?

I'm trying to check how many files does have a directory using NodeJS's File System.
var fs =require('fs');
function listaArchivos(directorio){
fs.readdir(directorio, function(err, archivos){
if(!err) {
console.log(archivos);
} else {console.log(err)}
})
}
var directorio = 'home/Rosamunda/Desktop/coderhouse/fs/';
listaArchivos(directorio);
I receive this error:
{ [Error: ENOENT, readdir 'home/Rosamunda/Desktop/coderhouse/fs/']
errno: 34,
code: 'ENOENT',
path: 'home/Rosamunda/Desktop/coderhouse/fs/' }
I've tried to search for that ENOENT error, and what I do understand is that the error appears when the path is incorrect, but the path does exist. If I try to print archivos, it returns "undefined".
ENOENT means the path doesn't exist. It looks like you may be missing the / at the beginning of the path (to make it an absolute path).

Why does the errno in node-webkit differ from Node.js?

Run the same code in node-webkit and Node.js
var fs = require('fs')
// var fs = process.mainModule.exports.fs // node-webkit
fs.readFile('/xxxx/xx','utf-8', function (e) {
console.log(e)
})
In Node.js, the errno is 34
{ [Error: ENOENT, open '/xxxxx/xx'] errno: 34, code: 'ENOENT', path: '/xxxxx/xx' }
In node-webkit, it's -2
{"errno":-2,"code":"ENOENT","path":"/xxxx/xx"}
Why the errnos are different?
Is there a errno cheat sheet for node-webkit like this one for Node.js?
I'm having the same error and its due to using a relative path. Try to reading fro xx directly, not from /xxxx/xx
Hope it solves it

Resources