How I published my first npm package?
Tags: npm, javascript, productivity, typescript
Published: 2020-05-23 23:04:39

> This article is originally posted [here](https://en.taishikato.com/posts/how-i-published-my-first-npm-package).

I thought making and publishing an NPM package isn’t easy.

Actually, it’s so easy! Your package doesn’t have to be very tricky, unique, and awesome.

You can publish your favorite utility code and install it on your project with npm or yarn command.
All I want to say is…it’s not complicated. We can do it!

![We can!](https://dev-to-uploads.s3.amazonaws.com/i/40g96r2lio2px3gku6m4.png)

## TL;DR

This time I publish an npm package called [@taishikato/slug-generator](https://www.npmjs.com/package/@taishikato/slug-generator) which generates slug string from text such as blog post title.

e.i. The string below is the slug for this URL(`https://medium.com/@TaishiKato/how-i-published-my-first-npm-package-5388564bf643`).

```
how-i-published-my-first-npm-package-5388564bf643
```

## How to publish

**Create an account**
Let’s make an npm account [here](https://www.npmjs.com/signup).

**Login via CLI**
npm command takes care of you.

```shell
$ npm adduser
Username: your-username
Password:
Email: (this IS public) your-email
Logged in as your-username on https://registry.npmjs.org/.
```

Great. Now you are logged in.
Then make a directory for the package.

```shell
$ mkdir slug-generator && cd $_
```

Now you are under the slug-generator directory.
We want to make our package [scoped package](https://docs.npmjs.com/misc/scope) to use the name (slug-generator in this case) that already is taken by someone.
Execute `yarn init` to generate a package.json. You will be asked some questions so please answer them.

```shell
$ yarn init
yarn init v1.16.0
warning ../../package.json: No license field
question name (slug-generator): @taishikato/slug-generator
question version (1.0.0):
question description: generate slug string
question entry point (index.js):
question repository url: https://github.com/taishikato/slug-generator
question author: taishikato
question license (MIT):
question private: false
success Saved package.json
✨  Done in 68.06s.
```

Then you need to use `npm publish — access=public` to publish a public package.

```shell
$ npm publish --access=public
npm notice
npm notice 📦  @taishikato/slug-generator@1.0.0
npm notice === Tarball Contents ===
npm notice 258B package.json
npm notice === Tarball Details ===
npm notice name:          @taishikato/slug-generator
npm notice version:       1.0.0
npm notice package size:  257 B
npm notice unpacked size: 258 B
npm notice shasum:        bf71ac427082c283c6d2cf600f7d1691ab0b5964
npm notice integrity:     sha512-clwDAYf181ObB[...]5pwvhOJbTUAiA==
npm notice total files:   1
npm notice
+ @taishikato/slug-generator@1.0.0
```

All done. Too quick? But yes version 1.0.0 of your package is on [npm](https://www.npmjs.com/).
But we still don’t have a README, LICENSE file, and actual lines of codes😇.

## Add README!

Yes, we need a blazing README.
Go to [shields.io](https://shields.io/) to generate budgets and show how cool we are😎.
At first, we generate a budge to display the version of your package on npm.

![shields.io](https://dev-to-uploads.s3.amazonaws.com/i/hj6dr5pc9d8uzyjezdur.png)

Next, we got an error because we don’t have any code yet… but generate it anyway🙄.

![bundle size](https://dev-to-uploads.s3.amazonaws.com/i/yvn4ye6hi85m29zbn4en.png)

Make a README.md file and paste the budgets you made.

![Preview](https://dev-to-uploads.s3.amazonaws.com/i/0i0i0mfauxr1bmtr6nbb.png)

## Let’s add some code (Finally)
Just simple code here.

```typescript
import { v4 as uuidv4 } from 'uuid';
const generateSlug = (target, hasUuidSuffix = false) => {
  const text = target.toLowerCase();
  const filterdText = text.replace(/[^a-zA-Z0-9]/g, ' ');
  let textArray = filterdText.split(/\s|\n\t/g);
  textArray = textArray.filter(text => {
    return text !== '';
  });
  const slug = textArray.join('-');
  if (hasUuidSuffix) return `${slug}-${uuidv4().split('-')[0]}`;
  return slug;
};
export default generateSlug;
```

## License

Hit this page (Insights→Community) on Github.

![License](https://dev-to-uploads.s3.amazonaws.com/i/bnbkgfuhk3uuj8tdq5bo.png)

Choose MIT anyway😅

![MIT](https://dev-to-uploads.s3.amazonaws.com/i/gxyoybu23n1csp8hub3j.png)

## Version

By the way, npm uses [Semantic Versioning](https://semver.org/). You don’t need to know the detail of it now but the main rules and concepts are

Given a version number MAJOR.MINOR.PATCH, increment the:

1. MAJOR version when you make incompatible API changes,
2. MINOR version when you add functionality in a backwards compatible manner, and
3. PATCH version when you make backwards compatible bug fixes.

We need to change the major version so use the command below.

```shell
$ npm version major
v2.0.0
```

## Publish🚀

```shell
$ npm publish
npm notice
npm notice 📦  @taishikato/slug-generator@2.0.0
npm notice === Tarball Contents ===
npm notice 1.1kB LICENSE
npm notice 496B  index.js
npm notice 304B  package.json
npm notice 901B  README.md
npm notice === Tarball Details ===
npm notice name:          @taishikato/slug-generator
npm notice version:       2.0.0
npm notice package size:  1.7 kB
npm notice unpacked size: 2.8 kB
npm notice shasum:        a43b58c8d1434faaeaf207778619981d5b372bd5
npm notice integrity:     sha512-u4jsqO8B7j3PY[...]kfdDVtGHT4+Zw==
npm notice total files:   4
npm notice
+ @taishikato/slug-generator@2.0.0
```

## Add some keywords on package.json
Mine is something like this

```json
{
  "name": "@taishikato/slug-generator",
  "version": "2.0.0",
  "description": "generate slug string",
  "main": "index.js",
  "repository": "https://github.com/taishikato/slug-generator",
  "author": "taishikato",
  "license": "MIT",
  "private": false,
  "dependencies": {
    "uuid": "^7.0.2"
  },
  "keywords": [
    "slug",
    "npm",
    "package",
    "taishikato",
    "slug generator"
  ]
}
```

## Thank you!

Now you can ship your code on npm!
You can do it for your future projects.
You can do it for the developers’ community.
It’s great for any reason.

What are you waiting for?
Let’s make a package.json and output something in this world🌎!

## Reference

Thank you [Jonathan Wood](https://medium.com/@Bamblehorse) for the great article!
[How to make a beautiful, tiny npm package and publish it](https://www.freecodecamp.org/news/how-to-make-a-beautiful-tiny-npm-package-and-publish-it-2881d4307f78/)

## 🖊️taishikato/slug-generator🖋️

[taishikato/slug-generator: Slug generator for blog posts or any other contents](https://github.com/taishikato/slug-generator)