Error when running js on mac via node.js. How to fix? - node.js

I have created a .js file in a text editor and tried to run it via node js:
var pizzaDoge = require('fs')
pizzaDoge.writeFile("/index.html", "<h1>doge is fat</h1>", function(error) {
if (error) {
return console.log(error)
} else {
return console.log("you fed the doge!")
}
})
when I try to run the program using node using "node index.html" in the command line software this pops out
{ Error: EACCES: permission denied, open '/index.html'
errno: -13,
code: 'EACCES',
syscall: 'open',
path: '/index.html' }
just in case the error no. matters to which computer I am using I use Mac. Thanks for the help.

On Mac, the root folder(/) requires root permission to access
Please try to use ./index.html instead of /index.html for creating files under your current folder

Related

How to read from a file where the filepath uses backward slashes?

In node.js, if I try to read from a file with backward slashes in the link (using fs module), I get this
Error: EISDIR: illegal operation on a directory, open 'C:\main\temp\config
1\folder\plugin\jquery-3.1.1.min.js'
at Error (native)
errno: -4068,
code: 'EISDIR',
syscall: 'open',
path: 'C:\\main\\temp\\config1\\folder\\plugin\\jquery-3.1.1.min.js' }
node.js code:
fs.readFile('C:\main\temp\config1\folder\plugin\jquery-3.1.1.min.js', function (err, data) {
});
Does anyone know how to fix it?
Thanks
Windows paths are supported by nodejs. You need to escape the backslashes:
fs.readFile('C:\\main\\temp\\config1\\folder\\plugin\\jquery-3.1.1.min.js', function (err, data) {
});

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).

Getting "at Error (native)" instead of actual node.js stacktrace despite using err.stack

Using the following example code in io.js 3.2.0 64bit on Windows 10 and calling the following code with node example.js
'use strict';
const fs = require('fs');
fs.readdir('I_DONT_EXIST', function (/**Error*/ err, /**string[]*/ files) {
if (err) {
console.log(err.stack);
}
});
I get
{ [Error: ENOENT: no such file or directory, scandir '...\I_DONT_EXIST']
errno: -4058,
code: 'ENOENT',
syscall: 'scandir',
path: '...\\I_DONT_EXIST' }
Error: ENOENT: no such file or directory, scandir '...\I_DONT_EXIST'
at Error (native)
So I get at Error (native) instead of the actual error trace even though I ask for err.stack.
Shouldn't this be the actual stack trace?
EDIT:
Here is a tiny piece of code that demonstrates my last (3rd) comment to the answer below.
'use strict';
const fs = require('fs');
fs.readdir('I_DONT_EXIST', function (/**Error*/ err, /**string[]*/ files) {
if (err) {
console.log('\n== 1) Original error');
console.log(JSON.stringify(err, Reflect.ownKeys(err), 4));
console.log('\n== 2) Original error "stack" property');
console.log(err.stack);
const e = new Error(err);
// Copy parameters I'm interested in from the original object
e.code = err.code;
console.log('\n\n== 3) New error');
console.log(JSON.stringify(e, Reflect.ownKeys(e), 4));
console.log('\n== 4) New error "stack" property');
console.log(e.stack);
console.log('\n\n== 5) Throw the error');
throw e;
}
});
The output I get, showing that I don't even get a file location of where the error finally occurs when I examine the original error object but get one in the new one, is:
== 1) Original error
{
"stack": "Error: ENOENT: no such file or directory, scandir 'C:\\Users\\xxx\\I_DONT_EXIST'",
"message": "ENOENT: no such file or directory, scandir 'C:\\Users\\xxx\\I_DONT_EXIST'",
"errno": -4058,
"code": "ENOENT",
"syscall": "scandir",
"path": "C:\\Users\\xxx\\I_DONT_EXIST"
}
== 2) Original error "stack" property
Error: ENOENT: no such file or directory, scandir 'C:\Users\xxx\I_DONT_EXIST'
== 3) New error
{
"stack": "Error: Error: ENOENT: no such file or directory, scandir 'C:\\Users\\xxx\\I_DONT_EXIST'\n at C:\\Users\\xxx\\test.js:11:19",
"message": "Error: ENOENT: no such file or directory, scandir 'C:\\Users\\xxx\\I_DONT_EXIST'",
"code": "ENOENT"
}
== 4) New error "stack" property
Error: Error: ENOENT: no such file or directory, scandir 'C:\Users\xxx\I_DONT_EXIST'
at C:\Users\xxx\test.js:11:19
== 5) Throw the error
C:\Users\xxx\test.js:20
throw e;
^
Error: Error: ENOENT: no such file or directory, scandir 'C:\Users\xxx\I_DONT_EXIST'
at C:\Users\xxx\test.js:11:19
Typically for async methods there won't be much of a (useful) stack trace available. There are modules like longjohn that can help provide more of a stack trace for such methods, but you wouldn't want to use that in production because of the overhead it incurs.

node.js fs rename ENOENT

I am trying to write a handler for file uploads in node.js using express framework. Following is the raw skeleton of it.
exports.handleUpload = function(req,res){
var temp_path = req.files.doc.path,
target_path = './uploads/' + req.files.doc.name ;
fs.rename(temp_path, target_path, function(err){
if(err){
console.log(err);
}
fs.unlink(temp_path, function(){
if(err){
console.log(err)
}
})
//Do stuff
})
}
However I get an error in the execution of renmae function occassionally(not always), especially with uploads of large files.
This is what the console caches from the error code
{ [Error: ENOENT, rename '/tmp/16368-19661hu.pptx'] errno: 34, code: 'ENOENT', path: '/tmp/16368-19661hu.pptx' }
From : https://github.com/joyent/node/blob/master/deps/uv/include/uv.h
XX(ENOENT, "no such file or directory")
The uploads/ directory does exist and permissions isn't an issue. Had it been so, it would have been failing each time, but it does not.
You're using the /tmp directory. The OS might be deleting the files since it can do so for the /tmp directory, hence the "randomness" of the issue. Use fs.exists before doing your other operations.

Trying to use fs.chmod with node.js

I have this device that will be connected to a computer, and the system i'm developing depends a lot on that device. However, since it will be installed in different places then they might use one usb port or another, and in my case, everytime i plug in the device i have to do a chmod 777 for it to work.
So, i made this file with possible ports the device could be connected to, and it goes like
/dev/ttyUSB0,/dev/ttyUSB1,/dev/ttyUSB2
I read it with this function
var puertoLect = function(callback){
//Busca en el archivo ports la lista de puertos probables donde se encuentra el modem receptor
fs.readFile('ports.txt', 'UTF-8', function (err, data) {
if (err){
console.log(err);
}else{
callback({data:data});
}
});
}
And then I use this function to split it into an array and do a chmod for each value of the array.
var puertoEnable = function(){
puertoLect(function(puertos){
var singlePort = String(puertos.data).split(',');
for(var i=0;i<singlePort.length;i++){
fs.chmod(singlePort[i], '0777', function (err){
if(err){
console.log(singlePort[i]+': Not Enabled');
console.log(err);
}else{
console.log(singlePort[i]+': Enabled');
}
});
}
});
}
What i'm trying to do with this latest function is test if it works. So that if it finds the exact port then it will enabled it and let me know it did, and if it doesn't, just tell me it didn't. However it ends up returning this:
undefined: No se pudo Habilitar
{ [Error: EPERM, chmod '/dev/ttyS24'] errno: 50, code: 'EPERM', path: '/dev/ttyS24' }
undefined: No se pudo Habilitar
{ [Error: ENOENT, chmod '/dev/ttyUSB0'] errno: 34, code: 'ENOENT', path: '/dev/ttyUSB0' }
undefined: No se pudo Habilitar
{ [Error: ENOENT, chmod '/dev/ttyS25
'] errno: 34, code: 'ENOENT', path: '/dev/ttyS25\n' }
I don't know what EPERM or ENOENT mean, however, when I tried to do this, the device was not connected (on purpose), because the files ttyS25 and ttyS24 already exist in "/dev". And ttyUSB0 will only appear when i connect the device.
So, I have to try and find a way to check the path to see if the file i'm trying to chmod exists. If it does, then do the chmod and return a variable with the path so i can use it in my serialPort configuration.
If anyone could help i'd appreciate it.

Resources