How to Publish Your First NPM Package
The npm (Node Package Manager) is the world’s largest source repository. Developers from all continents use this platform to share their javascript packages and modules. Have you ever imagined what it would be like to use Node.js without Express , Next.js or Mongoose ? Difficult, right? 😅😅
In addition to all these features, npm can be a great ally for beginning JS developers who want to demonstrate their knowledge and skills while helping the community.
In this tutorial, I will show you step-by-step on how to publish your first npm package. For this guide, I assume that you already have Node.js / NPM installed and know the basics of JavaScript.
Starting the Package
First, we must create a folder where all the source code of our package will be stored.
Using your command-line tool (CMD, Bash, etc.), navigate to the directory created earlier and enter the command:
npm init
Answer the requested questions and your application’s package.json will be generated.
ATTENTION: npm does not accept that two published packages have the same name, before giving your package a name click here and search if the name you want is not in use.
Name chosen, open the directory in your favorite IDE / Text Editor and let’s start creating your package logic.
Programming the Package
For this example, I will create a package that capitalizes the first letter of each word in a string.
Inside the project directory, create an index.js file and paste the following code:
The above code is all we need to solve the problem previously proposed, the function capitalizeString()
receives a string and returns another with the initials of each word in uppercase.
Another required file is Readme.md. In this file, written using markdown, all the presentation data of your package, a brief description, example of use, relevant additional information, etc. must be placed. NPM will use this file on the presentation page of its online package.
For this project I will use the following Readme.md:
# themesfinity-String-Capitalize
NPM Package to Capitalize the First Letter of Each Word of a String. For Educational Purposes.
## Instalation
`` ` npm i themesfinity-string-capitalize ` ``
## Usage
`` `
const ksc = require('themesfinity-string-capitalize')
console.log(tsc.capitalizeString("hello world!")) // "Hello World!"
With this part ready, we can now carry out the last configurations to publish our package.
Registering at npmjs.com
To publish your package, you must register at npm site. Register by clicking here and confirm your account by the link that will be sent to your email.
Publishing the Package
After verifying your account, navigate from the command line to your project’s directory and type npm login
. You will be asked for the username, password and email registered in the previous step.
Finally, type npm publish
and you’re done. ”
If all goes well, you will get the following feedback:
$ npm publish
npm notice
npm notice package: themesfinity-string-capitalize@1.0.0
npm notice === Tarball Contents ===
npm notice 1.1kB LICENSE
npm notice 245B index.js
npm notice 643B package.json
npm notice 327B README.md
npm notice === Tarball Details ===
npm notice name: themesfinity-string-capitalize
npm notice version: 1.0.0
npm notice package size: 1.4 kB
npm notice unpacked size: 2.3 kB
npm notice shasum: a3e7a65aad3cd92b88d4b7281895883d033123e2
npm notice integrity: sha512-G4zCTirrCMf3p[...]qfDrUIDc/asMw==
npm notice total files: 4
npm notice
+ themesfinity-string-capitalize@1.0.0
Access your account at https://www.npmjs.com/ and there will be your public package for programmers from around the world.
Conclusion
Congratulations, you have just published your first npm package. If you have any questions or comments about the above content, please leave them in the comments and I will be responding as soon as possible.
Comments
No comment yet.