0

I'm looking for a node js code to read and write in one text file. On guthub I have found a promising script from Arahnoid:

https://gist.github.com/Arahnoid/9925725#file-read-write-file-js

If I try to execute it, I get the error "File is not defined (line 3 ... new file(txt...). I guess, I have to install a npm package. But I don't know which one.

Can anybody give my a hint how I get to run this javascript?

NewbieXXL
  • 155
  • 1
  • 1
  • 11
  • http://stackoverflow.com/questions/2496710/writing-files-in-node-js – mplungjan Jun 16 '16 at 09:26
  • thanks, mplungjan. I already have found this thread too. But unfortunately I have to less experience to understand and use that. Therefore I was looking for a read to use script. – NewbieXXL Jun 16 '16 at 09:31
  • Just to be sure, you are talking about node.js environment and not in browser right? In node look @ http://stackoverflow.com/questions/10058814/get-data-from-fs-readfile to read a file and @mplungjan paster how to write a file – irimawi Jun 16 '16 at 09:32

2 Answers2

2

I guess, I have to install a npm package

You don't. If you needed to install an npm package then the code would include a require statement trying to load it.

Can anybody give my a hint how I get to run this javascript?

It is completely undocumented code found lying around in the Internet. Stop trying.

If you want to read a file using NodeJS then look at NodeJS' documentation. A basic search will turn up the built in file system api.

var fs = require("fs");
fs.readFile('/etc/passwd', function (err, data) {
  if (err) {
        throw err;
  }
  console.log(data);
});

(And there's a matching writeFile method to go with that).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
-1

It requires Nodejs Enviroment to run the file.

Resource: http://www.tutorialspoint.com/nodejs/nodejs_file_system.htm

rittam
  • 328
  • 1
  • 3
  • 13