Fs Module error - javascript

I'm using Node v6.7.0 trying to use 'fs' module but there is error as you see below. i Have tried to install it in addition but is not working(even if i add whole path). If i check in the site https://www.npmjs.com/package/fs you can see the message. Any ideas how can use the module?
var filename = process.argv[2];
var version = process.argv[3];
var fs = require('fs');
var prompt = require('C:/Program Files/nodejs/node_modules/prompt');
var p4 = require('C:/Program Files/nodejs/node_modules/p4');
p4.edit(filename, function(err, data) {
if (err) {
console.error(err.message);
}
fs.readFile(filename, 'utf8', function (err, data) {
if (err) {
return console.log(err);
}
var result = data.replace(/string to be replaced/g, version);
fs.writeFile(filename, result, 'utf8', function (err) {
if (err) return console.log(err);
});
});
console.log(data);
prompt.start();
prompt.get('p4 submit -c changelist', function (err, result) {
if(err) {
console.log(err.message);
}
console.log(result);
});
});
fs.js:303
binding.open(pathModule._makeLong(path),
^
TypeError: path must be a string or Buffer
at TypeError (native)
at Object.fs.readFile (fs.js:303:11)
at C:\WorkSpace\http.js:22:9
at C:\Program Files\nodejs\node_modules\p4\p4.js:13:24
at ChildProcess.exithandler (child_process.js:213:5)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:877:16)
at Socket.<anonymous> (internal/child_process.js:334:11)
at emitOne (events.js:96:13)
Process finished with exit code 1

fs is a nodejs core module : here is the fs documentation

I found the answer it should be executed in node command line and var filename = process.argv[2]; must be filled up.

Related

Javascript: Error about opening a folder and renaming the files every x secs

i am very new in JS and Nodejs and i am trying to
make a script that: every 1 sec it will read the files of the folder and after doing a specific task with the rows of every file will rename the files. The code bellow is my implementation:
const path = require('path');
const fs = require('fs');
const csv = require('csv-parser');
const { parse } = require("csv-parse");
const directoryPath = path.join(__dirname, 'Documents');
console.log(directoryPath);
function FTP(){
console.log("------------- SCAN THE DIRECTORY --------------------------")
fs.readdir(directoryPath, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(function (file) {
console.log(`${file}`)
fs.createReadStream(`${directoryPath}/${file}`)
.pipe(parse({ delimiter: `\t`, from_line: 2 }))
.on("data", function (row) {
let arrayToString = JSON.stringify(Object.assign({}, row)); // convert array to string
let stringToJsonObject_row = JSON.parse(arrayToString);
// DO A JOB ....
})
.on("end", function () {
console.log("finished");
})
.on("error", function (error) {
console.log(error.message);
})
});
})
fs.readdir(directoryPath, function (err, files) {
if (err) {
return console.log('Unable to scan directory: ' + err);
}
files.forEach(function (file) {
fs.rename(`${directoryPath}/${file}`,`${directoryPath}/${file}0`, () => {
console.log(`${file} Has been renamed`)});
});
});
}
setInterval(FTP, 1000);
THIS IS THE ERROR I GET:
/home/nikos/Desktop/TL2/FTP_server/Documents
------------- SCAN THE DIRECTORY --------------------------
sample_new
sample_new1
sample_new Has been renamed
sample_new1 Has been renamed
finished
finished
------------- SCAN THE DIRECTORY --------------------------
sample_new0
sample_new10
sample_new0 Has been renamed
sample_new10 Has been renamed
finished
finished
------------- SCAN THE DIRECTORY --------------------------
sample_new00
sample_new100
sample_new00 Has been renamed
sample_new100 Has been renamed
finished
finished
------------- SCAN THE DIRECTORY --------------------------
sample_new000
sample_new1000
sample_new000 Has been renamed
sample_new1000 Has been renamed
finished
finished
------------- SCAN THE DIRECTORY --------------------------
sample_new0000
sample_new10000
sample_new0000 Has been renamed
sample_new10000 Has been renamed
finished
finished
------------- SCAN THE DIRECTORY --------------------------
sample_new00000
sample_new100000
sample_new00000 Has been renamed
sample_new100000 Has been renamed
node:events:505
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open '/home/nikos/Desktop/TL2/FTP_server/Documents/sample_new00000'
Emitted 'error' event on ReadStream instance at:
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: '/home/nikos/Desktop/TL2/FTP_server/Documents/sample_new00000'
}
[Done] exited with code=1 in 6.107 seconds
As it seems for a few iteration, it works but then i take the above error, can anyone explain me why?
Thank you!
As i can understand after reading about asynchronous programming, i think that due to nodejs is non-blocking, it does not wait for the IO, it goes to the bellow lines of code and when the file is going to be processed, it is already renamed by the renaming commands. Do you think this is a correct explanation?

'sed' Command doesn't work with java script

I want to exectue following Shell Command with the Execute Option form Javascript:
"sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf"
Then i try this command in the shell console it works perfektly. But after starting it with the .js programm nothing is happen. I just want to copy the first 5 lines in template and add them to the config file.
You can do with exec from child_process
const exec = require('child_process').exec
exec("sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf", (err, stdout, stderr) => {
if (err) {
return console.error(err)
}
console.log(`stdout: ${stdout}`)
console.log(`stderr: ${stderr}`)
})
You can use the below code to execute any shell command and replace the o/p to a new file(create a new file) everytime you run it. And, you should use the line-reader module to get the line from the files.
npm install --save line-reader
const fs = require('fs');
const lineReader = require('line-reader');
const { exec } = require('child_process');
const cmd = "sed -i 4r<(sed '1,5!d' /etc/wpa_supplicant/wpa_template.template) /etc/wpa_supplicant/wpa_supplicant.conf"
exec(cmd, (err, stdout, stderr) => {
if (err) {
//some err occurred
console.error(err)
} else {
// the *entire* stdout and stderr (buffered)
fs.writeFile('file path', stdout, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('file saved');
readFile1();
});
}
});
const readFile1 = () => lineReader.eachLine('file path', function(line) {
//make logic to take number of lines you want to take.
console.log(line);
});

Run python script using node js error no 22

I'm using node.js to run a python script through the web browser. below is the ScriptRunner.js code.
var PythonShell = require('python-shell');
var path = require('path');
exports.runScript = function() {
var options = {
scriptPath: 'D:\L4Project\working code\finalproject\routes'
};
PythonShell.run('sensitive2.py',options, function (err, results) {
if (err) throw err;
console.log('results: %j', results);
});
}
when I run the script in browser it returns error message like below.
D:\L4Project\working code\finalproject\routes\scriptRunner.js:10
if (err) throw err;
^
outes\sensitive2.py': [Errno 22] Invalid argumentg codeinalproject at PythonShell.parseError (D:\L4Project\working code\finalproject\node_modules\python-shell\index.js:191:17)
at terminateIfNeeded (D:\L4Project\working code\finalproject\node_modules\python-shell\index.js:98:28)
at ChildProcess.<anonymous> (D:\L4Project\working code\finalproject\node_modules\python-shell\index.js:89:9)
at emitTwo (events.js:126:13)
at ChildProcess.emit (events.js:214:7)
at Process.ChildProcess._handle.onexit (internal/child_process.js:198:12)
I have googles lot and nothing worked. Can Somebody help me to solve the problem?

How to get files list using fileSystem?

I am trying to get list of files in specified directory but its throwing error any idea what is implemented wrong in below code.
service.js
var fs = require('fs');
var path = require('path');
var async = require('async');
var filePath = path.resolve('./logs/dit/');
function checkSearchObj () {
fs.readdir(filePath,function (err, files) {
files.forEach(function(file) {
console.log(file);
});
})
}
checkSearchObj();
error
TypeError: Cannot read property 'forEach' of undefined
at C:\Users\sh529u\WebstormProjects\ulog\app\serverf
s:8:15
at FSReqWrap.oncomplete (fs.js:82:15)
Change this:
function checkSearchObj () {
fs.readdir(filePath,function (err, files) {
files.forEach(function(file) {
console.log(file);
});
})
}
to this:
function checkSearchObj () {
fs.readdir(filePath,function (err, files) {
if (err) {
console.log('Error:', err);
return;
}
files.forEach(function(file) {
console.log(file);
});
})
}
to see what's the error.
You need to check for errors instead of assuming that there was no error or otherwise you will try to access undefined variables and the only error you'll see is that you access undefined and not the actual error that's in err in this case.
Your callback will either get the err or the files but not both.
Update:
After changing the code to display error you got:
Error: { [Error: ENOENT: no such file or directory, scandir 'C:\Users\Web stormProjects\ulog\app\serverfiles\logs\dit'] errno: -4058, code: 'ENOENT', syscall: 'scandir', path: 'C:\\Users\\sh529u\\WebstormProjects\\ulog\\app\\serverfiles‌​\\logs\\dit'
Which means that the program doesn't display the files in the directory because there is no such directory. Mystery solved. Now you can see how useful it is to actually check the errors.

Node script to import reddit JSON won't work

I want to import the reddit json thread into my mongodb local server, so I can begin analyzing it. Please help me out.
app.js file below
var MongoClient = require('mongodb').MongoClient
, request = require('request');
MongoClient.connect('mongodb://localhost:27017/trasmatter', function(err, db) {
if(err) throw err;
request('https://www.reddit.com/r/Seattle/comments/3e0q5u/why_metro_matters_infographic/.json', function (error, response, body) {
if (!error && response.statusCode == 200) {
var obj = JSON.parse(body);
var stories = obj.data.children.map(function (story) { return story.data; });
db.collection('reddit').insert(stories, function (err, data) {
if(err) throw err;
console.dir(data);
db.close();
});
}
});
});
Console error after running 'node app.js'
app.js:11
var stories = obj.data.children.map(function (story) { return sto
^
TypeError: Cannot read property 'children' of undefined
at Request._callback (/Users/yay/code/pulley/app.js:11:35)
at Request.self.callback (/Users/yay/code/pulley/node_modules/request/request.js:198:22)
at Request.emit (events.js:110:17)
at Request.<anonymous> (/Users/yay/code/pulley/node_modules/request/request.js:1057:14)
at Request.emit (events.js:129:20)
at IncomingMessage.<anonymous> (/Users/yay/code/pulley/node_modules/request/request.js:1003:12)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickCallback (node.js:355:11)
Another script file that works successfully. (I just plugged in
another link and change localhost link)
var MongoClient = require('mongodb').MongoClient
, request = require('request');
MongoClient.connect('mongodb://localhost:27017/course', function(err, db) {
if(err) throw err;
request('http://www.reddit.com/r/technology/.json', function (error, response, body) {
if (!error && response.statusCode == 200) {
var obj = JSON.parse(body);
var stories = obj.data.children.map(function (story) { return story.data; });
db.collection('reddit').insert(stories, function (err, data) {
if(err) throw err;
console.dir(data);
db.close();
});
On your second example, when requesting http://www.reddit.com/r/technology/.json the JSON result is an object:
{"kind":"stuff","data":{}}
But the first request result to an array of objects :
[{"kind":"stuff", "data":{}}, {"kind":"stuff","data":{}}]
In order to get your data, you need a loop or specify which object you want :
var obj = JSON.parse(body);
console.log(obj[0].data); // for example
Credit goes to this guy https://github.com/loganfranken
It looks like https://www.reddit.com/r/Seattle/comments/3e0q5u/why_metro_matters_infographic/.json returns an array as the top-level JSON element, so I think changing this:
obj.data.children
To this:
obj[1].data.children
Should do the trick.

Categories