How to create your first npm package? 🧰
Richard Dobroň
Posted on August 7, 2022
What is NPM
NPM stands for Node Package Manager and is the default JavaScript package manager powered by Node.js. The npm manager has two parts:
- CLI (Command Line Interface) — a tool for publishing and downloading packages,
- online repositories containing over 2 million JavaScript packages.
Don't have an account?
Navigate to npmjs.com and click sign up.
1. Create an npm project
- Create a folder named
arraySort
mkdir arraySort
2. Initialize project as npm package
a) Using npm init -y or -yes
- This process will automatically generate a
package.json
file with default options.
npm init -y
b) Using npm init
- In this process, you will be asked a number of questions such as the name of the author, the description, and the license, etc.
npm init
3. Create a file named index.js
with this content:
function arraySort(array, compareFunction) {
const cloned = array.slice();
if (compareFunction) {
return cloned.sort(compareFunction);
}
return cloned.sort();
}
module.exports = arraySort;
4. Create a README.md (optional)
- You should provide a brief description of the repository and its functions.
5. Login into NPM
npm login
6. Publish the package
npm publish
You Did It!
Congratulations, you have published your first NPM package!
💖 💪 🙅 🚩
Richard Dobroň
Posted on August 7, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.