Node Js Setup and using built-in and external modules
Node JS
1. Use of Built-in Modules
const anyName = require('module_name');
Example: How to copy a file Using File System in Node Javascript.
Solution: Suppose we have a file namely file1.txt and they contain some text in it.
const fs = require("fs"); //includes file system module
fs.copyFileSync("file1.txt", "file2.txt"); //copyFileSync is method to copy file
//here file2.txt will be created with the text that inside file1.txt and if file already exists it will be replaced
2. Use of External Node Modules
i. Initialize the your project using npm init command
ii. You'll be asked some questions give the answers
iii. This process will create a file Package.json which contain whole project details and package description.
iv. To use any npm packages we'll have to install it.
> npm install package_name;
eg. npm install superheroes // it will create a dependency in package.json
//superheroes is a package name here
similarly, you can uninstall any packages using npm uninstall package_name command
Comments
Post a Comment