DEV Community

Cover image for How to sell an npm package: A Guide
Sebastian Hoitz
Sebastian Hoitz

Posted on • Originally published at basetools.io

How to sell an npm package: A Guide

When I quit the last company I was working at to pursue my own business adventures, building and selling a starter template or software library was high on my list of things I wanted to do.
However, when I started to think about how I could actually do this, I realized there are not a lot of tools available that do a good job at this. Gumroad is a provider used by many creators, but not well suited for code. Other services target nodejs/npm only and leave out all the rest of the programming languages. So I figured out: I need to scratch my own itch and build this service first. Similar to Gumroad, but laser-focused on developers. Talk about niching down! And just like that, the idea for basetools was born.

In this guide, I would like to show you how quickly you can start selling your own npm packages or other code.

It is very easy to start selling an npm package using basetools. However, there are some prerequisites to start selling: You have to have a private Github repository inside an organization and you have to publish your package using the Github registry. But we will cover all of this within this guide.

Set up the repository

For this guide, we will create a brand new repository on Github and start from there. If you have already created a repository, you can just skip this step.
For our example, we are creating a new private repository within our organization basetools-io.

image

In this case, our new repository is called basetools-npm-showcase. So let's get started on your machine:

mkdir basetools-npm-showcase
echo "# basetools-npm-showcase" >> README.md
echo 'module.exports = () => console.log("hello world")' >> index.js
git init
git add README.md index.js
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:basetools-io/basetools-npm-showcase.git
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

We have now created a private repository.

Setup the package.json

Next, we need to initialize the package.json, so that our repository can be treated as an npm package. We can easily do this by calling npm init. Important: Our package name has to contain our organization name. So instead of basetools-npm-showcase we have to call our package @basetools-io/basetools-npm-showcase. Make sure to commit the package.json to your repository.

Publish the package to your Github registry

In order to always publish this package to the Github registry, you have to configure the npm registry in your package.json:

"publishConfig": {
  "registry":"https://npm.pkg.github.com"
},
Enter fullscreen mode Exit fullscreen mode

You also have to log in with your Github account. To do that, first generate a personal access token here: https://github.com/settings/tokens. Make sure to activate the write:packages permission. Copy the generated token. Now, back in the terminal type in

npm login --registry=https://npm.pkg.github.com
Enter fullscreen mode Exit fullscreen mode

If this was successful, you can finally publish your package:

npm publish
Enter fullscreen mode Exit fullscreen mode

Adding the product on basetools

Once you have completed the steps above, you can create an account on basetools if you haven't done so yet. After registration click that you want to add a new product.

Add the npm packlage as a product to basetools

And just like that, you can start selling! basetools automatically creates a checkout page for you. You can link to this page from your website, for example. After a successful payment, your customer will be automatically invited to your repository as a collaborator. This will grant him access to the repository and the packages:

Confirmation of a sale of your npm package

How to install the package as your customer

Once your customer has bought access to your repository, he also has to log in to npm with the Github account that was used when buying your product:

npm login --registry=https://npm.pkg.github.com --scope=@OWNER
Enter fullscreen mode Exit fullscreen mode

Where @OWNER is the name of your repositories account, in our example @basetools-io. When this was successful, the package can be installed like any other npm package:

npm install @basetools-io/basetools-npm-showcase
Enter fullscreen mode Exit fullscreen mode

Did you like this guide? It was originally published on the basetools.io website. If you would like to go ahead and give basetools a try, you can find out more information right here: https://basetools.io/

Top comments (0)