DEV Community

Cover image for Node js file system, Easier than we think(1).
Abdullah al Mubin
Abdullah al Mubin

Posted on • Updated on

Node js file system, Easier than we think(1).

What is Node js?
If we want to run javascript without a browser, we must use an environment to execute js code. Using Node js we can create that runtime environment to execute js code.

It's built upon Chrome's v8 engine using C++.

We need to install Node js Node js in our system. We also need the editor to write codes. I'm going to use Visual studio to write and execute javascript code.

What is File system?
The file system module allows us to work with files in our system. Using the file system we can perform various tasks just like,

  • Create file

  • Read file

  • Update file

  • Delete file

  • Rename file

Need to know
The basic idea is to run a node js file.

Create File
There are three different methods to create new files,

  1. fs.writeFile()
  2. fs.open()
  3. fs.appendFile()

here 'fs' represent var fs =require('fs')

fs.writeFile()
fs.writeFile method helps us to create and write something on a file if that file does not exist. If that file already exists then it will replace with new content.

fs.writeFile( file, data, options, callback )
Enter fullscreen mode Exit fullscreen mode

In the above line,

  • file: represent, string, buffer, url or file path
  • data: represent specific content.
  • options: encoding, mode, flag
  • callback: we can set a function that will be called after that (fs.writeFile) function executed.

Example
write the below code to create a file with its content. Now execute node index.js(file name) command.

var fs = require('fs');

//create a file named firstFile.txt:
fs.writeFile('firstFile.txt', 'Hello world!', function (err) {
    if (err) throw err;
    console.log('Saved!');
});
Enter fullscreen mode Exit fullscreen mode

Image description

above code will create a text file named 'firstFile.txt' and its content will be 'Hello world!'

Image description

if you change its content 'Hello world!' to something else, then the will be replaced with the new content.

Example with encoding, mode and flag,

fs.writeFile('firstFile.txt', 'Hello world!',
  {
    encoding: "utf8",
    flag: "w",
    mode: 0o666
  },
  function (err) {
    if (err) throw err;
    console.log('Saved!');
});
Enter fullscreen mode Exit fullscreen mode

fs.open()
This method works differently than others, it takes the second parameter as a "flag", and if the second parameter is "w" for "writing", then the file is opened for writing. if the file does not exist then it will create an empty file.

var fs = require('fs');

fs.open('firstFile.txt', 'w', function (err, file) {
  if (err) throw err;
  console.log('Saved!');
});
Enter fullscreen mode Exit fullscreen mode

execute node index.js(filename) to see the changes.

types of flags are described below,

- w: Open file for writing. A file is created if it doesn’t exist.
- w+: Open the file to read and write. A file is created if it doesn’t exist.
- wx: It is the same as ‘w’ but fails if the path exists.
- wx+: Open the file to read and write. A file is created if it doesn’t exist.
- r: To open the file to read and throws an exception if the file doesn’t exist.
- r+: Open the file to read and write. Throws an exception if the file doesn’t exist.
- rs+: Open files in synchronous mode to read and write.
- a: Open the file to append. A file is created if it doesn’t exist.
- ax: It is the same as a but fails if the path exists.
- a+: Open the file for reading and appending. A file is created if it doesn’t exist.
- ax+: It is the same as ‘a+’ but fails if the path exists.

fs.appendFile()
It is used to asynchronously append the given data to a file. If the file does not exist, the file will be created

fs.appendFile( path, data[, options], callback )
Enter fullscreen mode Exit fullscreen mode

In the above line,

  • path: It can be string, buffer, url

  • data: It can be string, buffer.

  • options: encoding, mode, flag

  • callback: we can set a function that will be called after that (fs.appendFile) function executed.

below code helps us to understand more clearly, here, 'utf8' is refers to "encoding"

var fs = require('fs');

var data = "appendFileTest";

// Append data to file
fs.appendFile('appendFile.txt', data, 'utf8',
    // Callback function
    function () {
        console.log("Nicely done.")
    });
Enter fullscreen mode Exit fullscreen mode

Now, run the code with
node index.js // index.js is file name.

Image description
below image shows we created a file and its name "appendFile.txt"

Image description

that's for today, see yaa.

Top comments (0)