How to create your first npm package? 🧰

dobron

Richard Dobroň

Posted on August 7, 2022

How to create your first npm package? 🧰

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

CLI process

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
Enter fullscreen mode Exit fullscreen mode

CLI process

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;
Enter fullscreen mode Exit fullscreen mode

4. Create a README.md (optional)

  • You should provide a brief description of the repository and its functions.

5. Login into NPM

npm login
Enter fullscreen mode Exit fullscreen mode

6. Publish the package

npm publish
Enter fullscreen mode Exit fullscreen mode

You Did It!

Congratulations, you have published your first NPM package!

💖 💪 🙅 🚩
dobron
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.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024

How to Use KitOps with MLflow
beginners How to Use KitOps with MLflow

November 29, 2024