{"componentChunkName":"component---src-templates-post-js","path":"/post/an-open-source-maintainers-guide-to-publishing-npm-packages","result":{"data":{"contentfulHero":{"coverImage":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/1jqMTE2RJBiMLIcZAnu6It/5495c6ec7df10a55768df442e73fe4b2/home-hero-min.jpg","fileName":"home-hero-min.jpg"}}}},"pageContext":{"post":{"id":"601085fc-c724-5a3c-9652-200ddc6ddbbb","slug":"an-open-source-maintainers-guide-to-publishing-npm-packages","title":"An Open Source Maintainer's Guide to Publishing npm Packages","createdAt":"Aug 21, 2020","tweetText":null,"type":"blog","video":null,"podcast":null,"categories":[{"id":"be5e89a3-5b53-5136-8a8f-9be27e2c286b","name":"npm","slug":"npm"}],"image":{"file":{"url":"//images.ctfassets.net/xmu5vdhtphau/6Hm9assbNzRxqvEc1jGLVj/3936b3dc76a6a9c53efcf7c7dbb01c1f/Untitled-1.png","fileName":"Untitled-1.png"}},"author":{"name":"Kadi Kraman","bio":{"childMarkdownRemark":{"html":"<p>Building things in JavaScript, React, React Native, GraphQL. Love open source. Sometimes speak at conferences.</p>"}},"avatar":{"file":{"fileName":"d6454d24-94d8-4089-ba44-e7f2d734e9ca.jpeg","url":"//images.ctfassets.net/xmu5vdhtphau/2zoF7AveagbXCMKGPSIgu6/27bfe13a3a64527921a695d9354ab32e/d6454d24-94d8-4089-ba44-e7f2d734e9ca.jpeg"}}},"bodyContent":{"childMarkdownRemark":{"html":"<p>This article was first published in <a href=\"https://dev.to/kadikraman/an-open-source-maintainer-s-guide-to-publishing-npm-packages-1218\">DEV.TO</a></p>\n<hr>\n<p>The JavaScript community is built on Open Source and being able to give back to the community feels most gratifying. However, publishing a package for the first time might feel rather daunting, and publishing a release candidate may be a bit scary even for experienced authors. I hope to give some insight and helpful tips, especially for new authors.</p>\n<p>I have owned two open source libraries: a tiny utility library for DraftJS called <a href=\"https://github.com/kadikraman/draftjs-md-converter\">draftjs-md-converter</a> and a React Native security library called <a href=\"https://github.com/FormidableLabs/react-native-app-auth\">react-native-app-auth</a>. I am certainly not as heavily involved in Open Source as some, but I have had the sole responsibility of publishing new versions of these libraries for several years so I think I have enough experience to justify this blog post! I remember how scary it was to publish a library for the first time, and I still remember how apprehensive I felt publishing a release candidate. The purpose of this writeup is to produce a guide on how to publish packages: both a comprehensive guide for new authors, and a sanity check for future me.</p>\n<p>I will cover the following:</p>\n<ul>\n<li>Setting up your repo for publishing</li>\n<li>Publishing a package (initial release)</li>\n<li>Versioning</li>\n<li>Publishing a package (subsequent releases)</li>\n<li>Publishing alpha / beta / release candidate versions</li>\n</ul>\n<p>You can use npm or yarn, it's completely up to you. The publish commands are identical. I will be using npm throughout.</p>\n<h2>Setting up your repo for publishing</h2>\n<p>Before you're ready to run the <code>publish</code> command, you should check that your package is in a good state to be published. Here are a few things you might want to consider:</p>\n<h3>Check the package name</h3>\n<p>The <code>name</code> field in your <code>package.json</code> will be the name of your published package. So for instance if you name your package <code>package-name</code>, users will import it with</p>\n<pre><code>import ... from \"package-name\";\n</code></pre>\n<p>The name needs to be unique, so make sure you check the name is available on <a href=\"https://www.npmjs.com/\">https://www.npmjs.com/</a> or your publish command will fail.</p>\n<h3>Set the initial version</h3>\n<p>The <code>version</code> attribute in your <code>package.json</code> will determine the version of the package when published. For your initial release you might use:</p>\n<pre><code>{\n  \"version\": \"0.0.1\"\n}\n</code></pre>\n<p>or</p>\n<pre><code>{\n  \"version\": \"1.0.0\"\n}\n</code></pre>\n<p>NPM packages use <a href=\"https://semver.org/\">semver</a> for versioning, which means the version consists of 3 numbers: the major, minor and patch version. We will talk more about versioning in the \"Versioning\" section.</p>\n<h3>Make sure your repository is not private</h3>\n<p>In the <code>package.json</code> you may have an attribute <code>\"private\": true</code>. It is a <a href=\"https://docs.npmjs.com/files/package.json#private\">built in protection</a> so you wouldn't accidentally publish something that's meant to be private. It is generally a good practice to use this if you're building something that is not meant to be open source, like a personal or a client project. However if you're about to publish the repo as a package, this line should be removed.</p>\n<h3>Add a license</h3>\n<p>Ensure you have set the <a href=\"https://docs.npmjs.com/files/package.json#license\">license</a> in your <code>package.json</code>. This is to let people know how you are permitting them to use your package. The most common licenses are \"MIT\" and \"Apache-2.0\". They are both permissive, allowing users to distribute, modify, or otherwise use the software for any purpose. You can read more about the differences <a href=\"https://snyk.io/blog/mit-apache-bsd-fairest-of-them-all/\">here</a>. I have always used the \"MIT\" license.</p>\n<h3>Check the entry point</h3>\n<p>The <code>main</code> field in your <code>package.json</code> defined the main entry file for the package. This is the file the users will access then importing your package. It'll usually be something like <code>./index.js</code> or <code>./src/index.js</code> depending on the location of your entry file.</p>\n<h3>Restrict the files you want to publish</h3>\n<p>Be default, publishing a package will publish everything in the directory. Sometimes you might not want to do that, e.g. if your repository includes an example folder or if you have a build step and only want to publish the built bundle. For that purpose, there is a <a href=\"https://docs.npmjs.com/files/package.json#files\">files</a> field in the <code>package.json</code>. If omitted, the field defaults to <code>[\"*\"]</code>, but when set, it includes only the files and directories listed in the array. Note that certain files are always included, even if not listed: <code>package.json</code>, <code>README</code>, <code>CHANGES / CHANGELOG / HISTORY</code>, <code>LICENSE / LICENCE</code>, <code>NOTICE</code>, and the file in the \"main\" field.</p>\n<h3>Add a build step</h3>\n<p>Sometimes, you may need a build step. For example if you've written your package using Flow, TypeScript or cutting edge JavaScript features that aren't implemented in all browsers, and want to compile/transpile your package to vanilla JavaScript that anyone could use. For this you can use a <code>prepublish</code> script like so:</p>\n<pre><code>{\n  \"scripts\": {\n    \"prepublish\": \"npm run your-build-script\"\n  }\n}\n</code></pre>\n<p>This will be run automatically when you run the publish command. For example in <a href=\"https://github.com/kadikraman/draftjs-md-converter/blob/master/package.json\">this package</a> I use the <code>prepublish</code> script to rebuild the app in the <code>dist</code> directory. Notice also that the <code>main</code> field in this <code>package.json</code> is set to <code>\"main\": \"dist/index.js\"</code> since I want the users to access the built file.</p>\n<p>There are more built in scripts for various occasions, but this is the only one I've had to use when publishing packages. You can read more about other available scripts <a href=\"https://docs.npmjs.com/misc/scripts\">here</a>.</p>\n<h2>Publishing a package (initial release)</h2>\n<p>Clone your repo, and make sure you're on the latest <code>master</code> branch (or whatever your main branch is) with no uncommitted changes.</p>\n<p>Create an account on <a href=\"https://www.npmjs.com/\">https://www.npmjs.com/</a> if you don't have one already, and use these credentials to log in on your terminal:</p>\n<pre><code>npm login\n</code></pre>\n<p>Finally, in your project repo, run:</p>\n<pre><code>npm publish\n</code></pre>\n<p>If you've set up two factor authentication, you'll get a prompt for it in your terminal before the publish is completed. Once the command has finished successfully, you should be able to instantly see your package at <a href=\"https://www.npmjs.com/package/package-name\">https://www.npmjs.com/package/package-name</a> where <code>package-name</code> is the name of your package set in the <code>package.json</code>, and people will be able to install your package with:</p>\n<pre><code>npm install package-name\n</code></pre>\n<h2>Versioning</h2>\n<p>Publishing subsequent versions of the package involves more thought, because now we need to consider versioning. As mentioned above, npm packages are versioned using semver. This means that there are 3 types of releases: major, minor and patch, and you should use these to communicate the types of changes in your library.</p>\n<ul>\n<li><strong>Major</strong> changes include anything that is incompatible with the previous versions</li>\n<li><strong>Minor</strong> changes are usually new features and bigger bug fixes</li>\n<li><strong>Patch</strong> changes are tiny bug fixes or additive changes</li>\n</ul>\n<p>One thing to note is that the semver naming is a bit misleading, specifically in \"major\" - a major release doesn't necessarily mean that a lot has changed. It means that when going from the previous to current version, there is a breaking change, and that the users of your library might need to change something in their code in order to accommodate the latest version. For instance, changing an exported function name or parameter order would be considered major changes. A lot of maintainers tend to collect breaking changes and release them all together to minimise how often the major version is incremented.</p>\n<p>The reason you should only do breaking changes in major releases is because the users of your library may opt in to all future patch and minor versions silently, so the next time they run <code>npm install</code> you might end up breaking their build.</p>\n<p>In the <code>package.json</code>, this is communicated with ~ and ^ where ~ opts in for all future patch releases and ^ opts in for all future patch and minor releases:</p>\n<p><code>~1.2.3</code> will match all <code>1.2.x</code> versions but will miss <code>1.3.0</code></p>\n<p><code>^1.2.3</code> will match any <code>1.x.x</code> release including <code>1.3.0</code>, but will hold off on 2.0.0.</p>\n<p>Side note: when the major version is <code>0</code>, the package is considered unstable and so the ^ acts the same as ~ and for all releases before major 1 you may publish breaking changes in <code>minor</code> releases.</p>\n<p>There is no option to opt in for all future major releases, because they are expected to be breaking and thus should be manual upgrades. <a href=\"https://medium.com/@Hardy2151/caret-and-tilde-in-package-json-57f1cbbe347b\">Source</a>.</p>\n<h2>Publishing a package (subsequent releases)</h2>\n<p>This is how you do releases after the initial one. As before, you should ensure you're on the master branch with no uncommitted changes. Consider what type of release this is: major, minor or patch. Now run the command to increment the version in your working directory, using <code>major</code>, <code>minor</code> or <code>patch</code> depending on the type of the change:</p>\n<pre><code>npm version minor\n</code></pre>\n<p>This is a convenience method that does a couple of things:</p>\n<ul>\n<li>Increments the version in your <code>package.json</code> based on the type of the change</li>\n<li>commits this version bump</li>\n<li>creates a tag for the current release in your <code>.git</code> repo</li>\n</ul>\n<p>Now all that's left to do is to run the publish command as before:</p>\n<pre><code>npm publish\n</code></pre>\n<p>It is important to make sure you do the version bump before the publish. If you try to publish the same version of the library twice, the command will fail.</p>\n<p>Finally, you'll want to push the version bump commit and the tag:</p>\n<pre><code>git push\ngit push --tags\n</code></pre>\n<p>Notice that you'll have to push the tags separately using the <code>--tags</code> flag.</p>\n<p>If you're using GitHub to host your repository, the tags will show up under the Releases tab on your repository, e.g. <a href=\"https://github.com/kadikraman/draftjs-md-converter/releases\">https://github.com/kadikraman/draftjs-md-converter/releases</a></p>\n<p>It is good practice to write some release notes against the tag. I usually also use this space to thank any contributors!</p>\n<h2>Publishing an alpha / beta / release candidate</h2>\n<p>An alpha / beta / release candidate is usually an early access version of a major release. When you're about to do a major change, you might want to give your users a chance to try out the new release first on an opt-in basis but with the expectation that it may be unstable.</p>\n<p>Basically what we want to achieve is to publish a new version \"silently\" that would allow only interested users to opt in for.</p>\n<p>Let's look at how you might create a release candidate. Suppose you are currently on <code>v3.4.5</code> and you want to do a release candidate for <code>v4</code>.</p>\n<p>First, we increment the version. The <code>npm version</code> command allows you to provide your own version number. In this case we're going to say it's the first release candidate for v4:</p>\n<pre><code>npm version 4.0.0-rc1\n</code></pre>\n<p>As before, this does the version bump, commits the change, and creates the tag. Now to publish the package. This is the most important step, since you want to ensure none of your users get this release candidate version by default.</p>\n<p>The publish command has a <code>--tag</code> argument, which defaults to \"latest\". This means that whenever someone <code>npm installs</code> your library, they get the last publish that has the \"latest\" tag. So all we need to do is provide a different tag. This can be anything really, I have usually used \"next\".</p>\n<pre><code>npm publish --tag next\n</code></pre>\n<p>This will publish your package under the next tag, so the users who do <code>npm install package-name</code> will still get <code>v3.4.5</code>, but users who install <code>npm install package-name@next</code> or <code>npm install package-name@4.0.0-rc1</code> will get your release candidate. Note that if you publish another release candidate with the \"next\" tag, anyone installing <code>npm install package-name@next</code> will get the latest version.</p>\n<p>When you're ready to do the next major release, you can run <code>npm version major</code> (this will bump the version to <code>4.0.0</code>) and <code>npm publish</code> as normal.</p>\n<h2>Conclusion</h2>\n<p>That's pretty much it! Publishing packages can be a bit scary the first few times, but like anything it gets easier the more you do it.</p>\n<p>Thank you for contributing to Open Source!</p>"}}}}}}