Yarn – Publishing a Package

Publishing a package to the npm repository has never been simpler. With a few steps you can create a package that is redistributable to all of your projects.

If you do not have yarn already configured. Click Here For Setup Instructions

One thing to note when publishing a package. Once you publish a package at a specific version, you can never change that version again.

Publishing a Package

Within your projects root directory you’ll have a package.json file which will define all of the details regarding your package. Within that directory you’d run the following command to publish your package.

yarn publish

You’ll be prompted for your username and password for npmjs.com. If you do not already have an account, be sure to create your account by Clicking Here.

If you have a gzipped tarball .tgz of your package that you want to publish instead you can run the following command.

yarn publish [tarball]

If you’re outside of your projects root directory, you can define the folder you wish to publish as so.

yarn publish [folder]

You’ll want to make sure that the folder you’re publishing contains a package.json file within the root of the directory.

If you want to publish a specific tag of your package, then use the following command within the project’s root directory to specify the tag.

yarn publish --tag <tag>

You can also control the access within the npm registry to a package you’re publishing by defining the access flag as so.

yarn publish --access <public|restricted>

Un Publish A Package From The Repository

You’ll want to be careful with this command, as if anyone is using your package you can cause issues for them. To remove a package from the npmjs.com repository simply run the following.

npm unpublish --force

At the time of this writing, there does not seem to be a way that I’m familiar with to unpublish a package via yarn so the traditional npm method will suffice.

Before you are able to use this command you will also need to login to npmjs.com via the command line by running the following command npm login within your projects root directory.

Example Package

First we’ll start by creating a blank directory named logger

Within the directory, lets start by running the following command which will create our package.json file.

yarn init

This command will take you through a series of questions and then output a file package.json with the following contents.

Your file will contain slightly different information, depending on how you answered each of the prompts previously. Keep in mind that the name should also be unique to what you find in npmjs.com, if not your package will fail when attempting to publish.

We now have our package.json file and listed that our main file would be named index.js. Let’s now create the file and put our code that we want for the package.

Here are the contents for my index.js file.

Once you have your files setup, you can now run the command yarn publish within your projects root directory. Shortly after it finishes, you will get a confirmation message letting you know that your package was published successfully.

Now to install your newly created package you’d simply run the following command within your future projects.

yarn add example-logger