npm a simple demo
Prerequisites: Please review the following articles:
1. Verify node is installed
node --version
v10.24.1
2. Verify that npm is successfully installed
npm --version
6.14.12
3. Create a new working directory
mkdir ~/working/dir
4. cd into the working directory
cd ~/working/dir
5. Create a new javascript file
vi ~/working/dir/date.js
6. Add the following code to it:
var myDate = new Date();
console.log (myDate);
7. Test the output using node
~/working/dir/node date.js
023-09-27T10:27:53.503Z
8. from your working directory create package.json
npm init
5. Go to https://www.npmjs.com/ and search for the package you would like to use. In this case, I will choose a package called moment as it is for formatting the date.
6. Install moment using npm
npm install moment
7. Next do an ls within the working dir and notice a new directory has been created called node_modules
8. Next cd into node_modules and do an ls there, you will see the new package we just installed call moment.
9. Add the following code to date.js
var moment = require("moment");
myDate = new Date();
var myCoolDate = moment(myDate).format("L");
console.log (myCoolDate);
9. Next Test the output again using the node
~/working/dir/node date.js
09/27/2023