William Le
Here’s a little guide for publishing your own packages on npm.
You’ve written some piece of software code that you think is really useful! Now you want publish it on npm so that others can use your wonderful bundle of code!
There’s a lot of configurations for npm but many times these will be project-specific. This article is just going to cover the essentials for getting your packages published on npm.
npm is included with Node.js. To check if npm is installed on your system, run this command in your terminal: npm -v
Let’s create a folder that’s going to hold our package’s source code. In your terminal:
# This will create, and navigate
# into the `wonderful-bundle` directory
$ mkdir wonderful-bundle
$ cd wonderful-bundle
Now that you’re in the folder, here’s where we start using npm
commands!
$ npm init
Running npm init
will ask you a few setup questions (for example, the name of your package, your package’s description, etc).
You can simply hit “Enter” for each question and this default boilerplate for package.json
file will be created in your directory:
{
"name": "wonderful-bundle",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
You can compare the package.json file to a recipe card for your favorite meal.
The package.json
file contains all of the descriptive metadata about the project (like the name “Apple Crumb Pie”), and all of the dependencies needed to properly run it (the ingredients: “apples”, “pie crust”, “sugar”, etc).
Using all of this info, npm brings everything together so that your package can be effortlessly downloaded & run by other people.
Let’s edit package.json
to include a description, and author information.
{
"name": "wonderful-bundle",
"version": "1.0.0",
"description": "outputs an uplifting message",
"main": "index.js",
"author": "Chompy MacPherson <chompymac@alligator.io>",
"license": "ISC"
}
The only fields that are required in package.json
are “name”, “version”, and “main”. The “scripts” field was removed since we don’t have any tests written yet.
The “main” field is the file path to the JavaScript code. When someone uses your package this JavaScript file will be used. Let’s create the index.js
file in our terminal:
$ touch index.js
In a text editor of your choice…
module.exports = function() {
console.log("you're wonderful!");
return;
};
Remember to export
your code in the same way you would for files that are local to your software project.
Generally it’s a good idea to include documentation for your package so others know how to use it. The README file is typically used for this purpose.
Let’s create the README file in the root of your package’s directory:
# create the README file
$ touch README
# put some text into README
$ echo "## Wonderful Bundle \n\n Get an uplifting message!" > README
Currently, this is what the file directory for wonderful-bundle
looks like:
wonderful-bundle
|_ index.js
|_ README
|_ package.json
Essentially, this is the basic structure of a npm package. Not much is needed to publish your software to npm!
Now that we feel pretty good about our package, let’s publish it!
$ npm publish
You’ll need an account on the npm registry website, and if you’re not logged into it from the CLI you’ll be asked to log in. You also have to use a package name that hasn’t been used on the registry already.
That’s it! Your package is now published on npm. To recap, there are only 3 steps to go from zero-to-published:
npm init
index.js
and README
npm publish
Now when someone wants to use your package, they just run this in their terminal:
$ npm install wonderful-bundle
This will download, and install any dependencies needed for your package in other people’s software projects! Technology is amazing 🤤
Hopefully this guide has shown you how easy it is to contribute your software to the Open Source community, regardless of how significant or small it might be 📦 👉 🌎
If you prefer using Yarn check out this guide: npm vs Yarn Commands Cheat Sheet
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!