DEV Community

Cover image for How to create your first npm package? 🧰
Richard Dobroň
Richard Dobroň

Posted on

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!

Top comments (0)