Articles/JavaScript/Yarn: Publishing a Package

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.

November 18, 2016·3 min read

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.

BASH
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.

BASH
yarn publish [tarball]

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

BASH
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.

BASH
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.

BASH
yarn publish --access <public|restricted>

Unpublish 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.

BASH
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 log in to npmjs.com via the command line by running npm login within your project's root directory.

Example Package

First we'll start by creating a blank directory named logger.

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

BASH
yarn init

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

JSON
{
  "name": "example-logger",
  "version": "1.0.0",
  "description": "Logs a message to the console.",
  "main": "index.js",
  "author": "Frank Perez <frank@frankperez.net>",
  "license": "MIT"
}

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 in the code we want for the package.

Here are the contents for my index.js file.

JAVASCRIPT
exports.logger = function() {
    console.log('Hello from our Logger Package.');
}

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.

BASH
yarn add example-logger