With Node.js at Scale we are creating a collection of articles focusing on the needs of companies with bigger Node.js installations, and developers who already learned the basics of Node.
See all chapters of Node.js at Scale:
- Using npm
- npm Tricks and Best Practices
- SemVer and Module Publishing [ this article ]
- Understanding the Module System, CommonJS and require
- Node.js Internals Deep Dive
- Building with Node.js
- Testing + Node
- Node.js in Production
- Node.js + Microservices are not a tool, rather a way of thinking when building software applications. Let's begin the explanation with the opposite: if you develop a single, self-contained application and keep improving it as a whole, it's usually called a monolith. Over time, it's more and more difficult to maintain and update it without breaking anything, so the development cycle may...
npm Module Publishing
When writing Node.js apps, there are so many things on npm that can help us being more productive. We don’t have to deal with low-level things like padding a string from the left because there are already existing modules that are (eventually) available on the npm registry.
Where do these modules come from?
The modules are stored in a huge registry which is powered by a CouchDB instance.
The official public npm registry is at https://registry.npmjs.org/. It is powered by a CouchDB database, which has a public mirror at https://skimdb.npmjs.com/registry. The code for the couchapp is available at https://github.com/npm/npm-registry-couchapp.
How do modules make it to the registry?
When should I consider publishing?
- If you want to share code between projects,
- if you think that others might run into the very same problem and you’d like to help them,
- if you have a bit (or even more) code that you think you can make use of later.
Creating a module
First let’s create a module:
npm init -y should take care of it, as you’ve learned in the previous post.
"test": "echo \"Error: no test specified\" && exit 1"
Let’s break this down really quick. These fields in your
package.json are mandatory when you’re building a module for others to use.
First, you should give your module a distinct
name because it has to be unique in the npm registry. Make sure it does not collide with any trademarks out there!
main describes which file will be returned when your users do a
require('modulename'). You can leave it as default or set it to any file in your project, but make sure you actually point it to a valid filename.
keywords should also be included because npm is going to index your package based on those fields and people will be able to find your module if they search those keywords in npm’s search, or in any third party npm search site.
author, well obviously that’s going to be you, but if anyone helps you develop your project be so kind to include them too! 🙂 Also, it is very important to include where can people contact you if they’d like to.
repository field, you can see where the code is hosted and the
bugs section tells you where can you file bugs if you find one in the package. To quickly jump to the bug report site you can use
npm bug modulename.
Solid license and licenses adoption helps Node adoption by large companies. Code is a valuable resource, and sharing it has it’s own costs.
Licensing is a really hard, but this site can help you pick one that fits your needs.
Generally when people publish modules to npm they use the MIT license.
The MIT License is a permissive free software license originating at the Massachusetts Institute of Technology (MIT). As a permissive license, it puts only very limited restriction on reuse and has therefore an excellent license compatibility.
#2 Semantic Versioning
Versioning is so important that it deserves its own section.
Most of the modules in the npm registry follow the specification called semantic versioning. Semantic versioning describes the version of a software as 3 numbers separated by “.”-s. It describes how this version number has to change when changes are made to the software itself.
Given a version number MAJOR.MINOR.PATCH, increment the:
- MAJOR version when you make incompatible API changes,
- MINOR version when you add functionality in a backwards-compatible manner, and
- PATCH version when you make backwards-compatible bug fixes.
Additional labels for the pre-release and the build metadata are available as extensions to the MAJOR.MINOR.PATCH format.
These numbers are for machines, not for humans! Don’t assume that people will be discouraged from using your libraries when you often change the major version.
You have to start versioning at 1.0!
Most people think that doing changes while the software is still in “beta” phase should not respect the semantic versioning. They are wrong! It is really important to communicate breaking changes to your users even in beta phase. Always think about your users who want to experiment with your project.
Having a proper documentation is imperative if you’d like to share your code with others. Putting a
README.md file in your project’s root folder is usually enough, and if you publish it to the registry npm will generate a site like this one. It’s all done automatically and it helps other people when they try to use your code.
Before publishing, make sure you have all documentation in place and up to date.
#4 Keeping secret files out of your package
Using a specific file called
.npmignore will keep your secret or private files from publishing. Use that to your advantage, add files to
.npmignore that you wish to not upload.
If you use
.gitignore npm will use that too by default. Like git, npm looks for
.gitignore files in all subdirectories of your package, not only in the root directory.
#5 Encouraging contributions
When you open up your code to the public, you should consider adding some guidelines for them on how to contribute. Make sure they know how to help you dealing with software bugs and adding new features to your module.
There are a few of these available, but in general you should consider using github’s issue and pull-request templates.
Now you understand everything that’s necessary to publish your first module. To do so, you can type:
npm publish and the npm-cli will upload the code to the registry.
Congratulations, your module is now public on the npm registry! Visit
www.npmjs.com/package/yourpackagename for the public URL.
If you published something public to npm, it’s going to stay there forever. There is little you can do to make it non-discoverable. Once it hits the public registry, every other replica that’s connected to it will copy all the data. Be careful when publishing.
I published something that I didn’t mean to.
We’re human. We make mistakes, but what can be done now? Since the recent leftpad scandal, npm changed the unpublish policy. If there is no package on the registry that depends on your package, then you’re fine to unpublish it, but remember all the replicas will copy all the data so someone somewhere will always be able to get it.
If it contained any secrets, make sure you change them after the act, and remember to add them to the
.npmignore file for the next publish.
Private Scoped Packages
If you don’t want or you’re not allowed to publish code to a public registry (for any corporate reasons), npm allows organizations to open an organization account so that they can push to the registry without being public. This way you can share private code between you and your co-workers.
Further read on how to set it up: https://docs.npmjs.com/misc/scope
If you’d like to further tighten your security by running a registry by yourself, you can do that pretty easily. npm has an on-premise version that can be run behind corporate firewalls. Read more about setting up npm enterprise.
Now that you know all these things, go and build something. If you’re up for a little bragging, make sure you tweet us (@risingstack) the name of the package this tutorial helped you to build! If you have any questions, you’ll find me in the comments.
In the next part of the Node.js at Scale series, you’re going to learn about the Node.js module system and require.