Internal packages
Course: Node.js Runtime - chapter 5
Node.js provides you some packages out of the box. Some common ones include
- fs - Filesystem
- path - Path related functions
- http - Create HTTP Servers (we’ll discuss this tomorrow)
fs package
Section titled “fs package”The fs (Filesystem) package is used to read, write, update contents on the filesystem.
const fs = require('fs');const path = require('path');
const filePath = path.join(__dirname, 'a.txt');
fs.readFile(filePath, 'utf8', (err, data) => { if (err) { console.log(err); } else { console.log(data); }});Why use the path library?
- Cross platform joins (Windows has Users\kirat\dir , linux has Users/kirat/dir)
- Gives you a bunch of helper functions (dirname)
- Normalises paths (Converts
/Users/kirat/Proejcts/../../Projectsto/Users/kirat/Projects