DEV Community

Cover image for How I build my first NPM package and you can, everything from starting, updating, using.
Abhay Prajapati
Abhay Prajapati

Posted on

How I build my first NPM package and you can, everything from starting, updating, using.

npm init -y
Enter fullscreen mode Exit fullscreen mode

onces the package is created, make a main.js file

touch main.js
Enter fullscreen mode Exit fullscreen mode

add main.js file in bin section, if it is not then create one.

#!/usr/bin/env node
// at the top of the main.js file.
Enter fullscreen mode Exit fullscreen mode

go write the logic of your package, what you want to do.
our simply write.

console.log('hello world this is my First Package.')
Enter fullscreen mode Exit fullscreen mode

after doing this open command palete and type

npm login
# you will be propmt for username and password and OTP
Enter fullscreen mode Exit fullscreen mode
npm publish
# this will publish your package to npm.
# make sure you are have unique name for your package.
Enter fullscreen mode Exit fullscreen mode

Updating Package:

for updating package go to your root file (main.js) and make your update.
After, this will update your package.

npm version patch/minor/major
# Patch: is nominal edits version updates last decimal.
# Minor: is minor bug fixed version, updates second last.
# Major: is major version updates Main.
Enter fullscreen mode Exit fullscreen mode

Using Package:

for using packages in your local machine type

npm i -g <package-name>
# -g is for global package, so you can access it from anywhere in you computer
Enter fullscreen mode Exit fullscreen mode
<package-name>
#  to run your package.
# console.log('hello world this is my First Package.')
Enter fullscreen mode Exit fullscreen mode

What Package I have built ? and how can you use it

This is my first package, and accutally I don't have any proper use case of this, but I am built for learning purpose

with this package you can convert you text to slug

makeslug convert this into slug
# convert-this-into-slug
Enter fullscreen mode Exit fullscreen mode

How to use it

npm i -g makeslug
Enter fullscreen mode Exit fullscreen mode
makeslug <your-text>
Enter fullscreen mode Exit fullscreen mode

So, this is simple story of my first package.

View on NPM

๐Ÿค๐ŸพConnect me on:
Twitter: ๐Ÿ•Š๏ธ@Abhayprajapati_
Github: ๐Ÿง@theabhayprajapati
Linkedin: ๐Ÿ“Œ@abhayprajaapati
Youtube: ๐Ÿ“บ@Abhayprajapati

Top comments (0)