How to make an npm package in less than 30 mins?
Introducing bookquotes-calling all the book lovers out there
I was looking to create a basic project for my GitHub community - Opentek
I went on and explored npm - Node Package Manager. We all have used npm at some point in our life so I thought of creating a one. While building Node.js projects, we use various packages that make development easier. All of these packages are managed by the Node Package Manager. After a few google searches and reading some articles, I have made one.
The name of the package is bookquotes. and check its web-page here
A simple NPM Package that returns random Lines from people's favorite books with names of the author as well as the book. It provides great and aesthetic quotes to display in your application. If you like to read books or loves literature this package is for you!
Umm! This blog is about how to make one!!! Let's do that.
Table of contents
Create an npm account
Let's create an npm account here
Click on the sign-up button.
Enter a username, email address, and password.
Now, you have to verify your account. Check the inbox of the email address that you provided and click on the verification email. This will verify your npm account.
You have successfully created an NPM account and can start publishing packages.
Log in using terminal or command prompt
Go to your terminal and type:
npm adduser
You can also use the command:
npm login
You’ll get a prompt for your username, password, and email. Add them in there!
- We have successfully signed in.
Building the package
The main purpose of the blog is to know how to make a package and also this was the same thought of mine when I made bookquotes
- First, we need a folder to hold our code. Create one in whichever way is comfortable for you. I’m naming my package bookquotes.
mkdir bookquotes
- Let's get into the bookquotes folder.
cd bookquotes
Now let's create a brand new repository on GitHub and add our project there. It will create a README file. The reason for doing this before initializing the package is because that way when we initialize our
package.json
file it's going to have all of our GitHub information linked inside of it.Next step is to create a
package.json
file. The file is just a way to describe what the package does, having the necessary information about our project. We need to run the commandnpm init
and give all the necessary information
{
"name": "bookquotes",
"version": "1.0.3",
"description": "Collection of people's favorite quotes from books around the world",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/Opentek-Org/bookquotes.git"
},
"keywords": [
"literature",
"books",
"quotes",
"aesthetic",
"captions",
"philosophy"
],
"author": "Anirudh Panda",
"license": "MIT",
"bugs": {
"url": "https://github.com/Opentek-Org/bookquotes/issues"
},
"homepage": "https://github.com/Opentek-Org/bookquotes#readme"
}
Our main file here is
index.js
so we need to create a file called index.js where we will write our main code.
I have created a function and built my simple npm package bookquotes.
getLines()
method returns an object containing the line, book, and author.
{
"line": "The gods grow jealous of too much contentment anywhere, and they show their displeasure all of a sudden.",
"book": "Malgudi Days",
"author": "R. K. Narayan"
}
getRandomLine()
method returns a random aesthetic line from people's favorite author or book!
The gods grow jealous of too much contentment anywhere, and they show their displeasure all of a sudden.
Testing the package!
Make sure you test your package before publishing it to NPM. I have created a test.js
file and tested my package using the following code:
const favline = require("./index.js");
var myLine = favline.getLines();
console.log("*********************************");
console.log("WE ARE GRAMMAR_NAZIS\n");
console.log("*********************************");
console.log("The lines I love- " + myLine.line);
console.log("Written by - " + myLine.author);
console.log("From the book - " + myLine.book);
OUTPUT
Voilaa! Our package is ready to be shipped!
Publishing the package
- To deploy the package, enter the command:
npm publish
The terminal should look something like the below:
Just a moment after publishing, we will get an email from the NPM registry indicating that your package has been successfully published!
We love you too npm
Using the Package
Now as our package is published on NPM we can try it out!
Create a folder and initialize a
package.json
by using the command:
npm init -y
The -y option creates a package.json without an interactive process.
- Now, let’s install our published npm package.
npm i bookquotes
We have the correct output:
We can see
bookquotes
in our dependencies:Create an
index.js
file and let’s code.
const favoriteLine = require("bookquotes");
var myLine = favoriteLine.getLines();
console.log("The lines I love- " + myLine.line);
console.log("Written by - " + myLine.author);
console.log("From the book - " + myLine.book);
OUTPUT:
- We have successfully deployed and tested our NPM package!
Summary
To summarize what we did:
Created an NPM Account from npmjs.org.
Login to the terminal using your npm credentials using the command,
npm login
.Initialized the
package.json
file using thenpm init
command.Wrote some code.
Deployed the package using,
npm publish
.Use your deployed package using
npm i <package-name>
.