Last updated on Jan. 20, 2022, 4:37 a.m. by rugved
If you use Node.js you must be using npm packages all the time. They are so helpful and surprisingly it is very easy to make your own npm package and publish it on the internet where it is accessible to the world.
In this article, we will build a simple npm package that returns the current date and time. Later, we will publish it and install it in a different project.
mkdir get-the-date-and-time
cd get-the-date-and-time
npm init
exports.functionName = () => {
// function logic goes here
}
exports.getDateAndTime = () => {
var today = new Date();
var date = today.getDate() + "-" + (today.getMonth()+1) + "-" + today.getFullYear();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var dateTime = date + ' ' + time;
return dateTime
}
npm publish
npm install YOUR_PACKAGE_NAME
Hurray! Next time you build any useful function, publish it so that others could install it too :)