How to create NodeJS custom libraries
Custom NodeJS module / library

How to create NodeJS custom libraries

Hello 👋🏼,

My name is Andrei, I am a Senior NodeJS Developer & #AWS Architect Consultant / Contractor / Freelancer. 👨🏻💻

This post will guide you on creating #NodeJS custom libraries for your projects. 😊___________________

Prerequisites:

  • Entry level 👶 knowledge of Javascript

___________________

What is NodeJS? What is npm? 👀

Node.js is a free, open-sourced, cross-platform JavaScript run-time environment that lets developers write command line tools and server-side scripts outside of a browser. 🚀 (ref: https://nodejs.dev/ )

#npm is installed with Node.js, it is the largest software registry. The name npm (Node Package Manager) stems from when npm was first created as a package manager for Node.js. (ref: https://www.w3schools.com/whatis/whatis_npm.asp)

___________________

Resources

  • Hello World Application: link 💻
  • Logger Custom Library: link 📚

___________________

Hello World Application 💻

Okay, so first things first 📌

npm init        
No alt text provided for this image

The application code is simple - the sum of two numbers received as input arguments.

No alt text provided for this image

Application Run

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image

___________________

Library's content 📚

Inside the #library, you can add code that can be used in multiple nodeJS projects (multiple #lambdas on AWS). 🚀🚀🚀

For the library repository, we will do the npm init again.

No alt text provided for this image

As of now, the sample library includes the sum function and a new logger (winston 😎) as an external library, in fact, it is present inside the package.json

No alt text provided for this image

In order to create a version of the library, you can create a release 🎁

No alt text provided for this image
No alt text provided for this image

Once the code is pushed on #GitHub we can update the #package.json of the hello world application in order to use the library functions. 💪

No alt text provided for this image

If you want to use the code from a feature branch, you need to replace the tag (v1.0.0) with the branch name (feature/implement-library). 😌

Now, let’s run npm i and update the code with our custom library.

No alt text provided for this image

Now if we run the application again, we will use the methods defined inside the library. 🥳

No alt text provided for this image

______________________

That’s it! A simple #NodeJS hello world application using a custom #library! 🎉

If you need any help or considerations, feel free to drop me a message 📩

To view or add a comment, sign in

Others also viewed

Explore content categories