{"id":728,"date":"2022-09-19T16:49:51","date_gmt":"2022-09-19T14:49:51","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=728"},"modified":"2022-09-19T16:49:51","modified_gmt":"2022-09-19T14:49:51","slug":"how-to-create-a-npm-package-ready-to-distribute-from-scratch","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/09\/19\/how-to-create-a-npm-package-ready-to-distribute-from-scratch\/","title":{"rendered":"How to Create a &#8220;npm Package&#8221; Ready to distribute from scratch"},"content":{"rendered":"\n<p>Node Package Manager, or npm (usually written in lower case) is one of the most commonly used package managers in JavaScript projects. It is built on top of Node and is so powerful that nearly everybody is using it.<\/p>\n\n\n\n<h4>What Is A Package Manager?<\/h4>\n\n\n\n<p>Imagine that you include library&nbsp;<strong>A<\/strong>&nbsp;to customize a text field. That library uses library&nbsp;<strong>B<\/strong>&nbsp;to format text and library&nbsp;<strong>C<\/strong>&nbsp;to show translations.<\/p>\n\n\n\n<p>Imagine that, at the same time, library\u00a0<strong>C<\/strong>\u00a0uses another five different libraries to handle different languages and you end up with a scheme like this one.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-22-1024x465.png\" alt=\"\" class=\"wp-image-2220\" width=\"602\" height=\"273\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-22-1024x465.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-22-300x136.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-22-768x349.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-22.png 1221w\" sizes=\"(max-width: 602px) 100vw, 602px\" \/><\/figure><\/div>\n\n\n\n<p>That\u2019s called the&nbsp;<em>dependency tree<\/em>&nbsp;of your application.<\/p>\n\n\n\n<p>Sooner or later your project will end up with dozens of dependencies (you won\u2019t even be aware of some of them). And what\u2019s even worse, each of those dependencies will only be compatible with certain specific versions.<\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-cyan-blue-color\">This situation would be a nightmare to manage manually. A simple update in a submodule might break your dependency tree and your app might not compile. <strong>That\u2019s exactly the problem that npm solves<\/strong>.<\/span><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-23-1024x451.png\" alt=\"\" class=\"wp-image-2221\" width=\"563\" height=\"248\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-23-1024x451.png 1024w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-23-300x132.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-23-768x338.png 768w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-23.png 1260w\" sizes=\"(max-width: 563px) 100vw, 563px\" \/><\/figure><\/div>\n\n\n\n<p>When you create an npm library you will create a json file called&nbsp;<code>package.json<\/code>&nbsp;in which you specify which dependencies your JS library has.<\/p>\n\n\n\n<p>At the same time, the dependencies of your library will have their own&nbsp;<code>package.json<\/code>&nbsp;files, creating a full dependency tree.<\/p>\n\n\n\n<p>If someone wants to add your library to their project, then they will just need to run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm install &lt;your-library-package-name><\/code><\/pre>\n\n\n\n<p>Magically, all the dependencies of the tree will be downloaded and installed. This system is amazing because in practice you don\u2019t need to care about the dependencies at all.<\/p>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-red-color\">Is There Any Downside of Using a Package Manager?<\/span><\/h4>\n\n\n\n<p>The main one is that developers lose control of which code they are including in their projects. Concerns about security have arisen in the past and critical systems require a very controlled and strict dependency tree.<\/p>\n\n\n\n<p>Take this example: a newly created project in React Native has more than 1500 dependencies. It is unlikely that more than a few people know them all. If someone introduced some malicious code \u2013 or just a bug \u2013 a lot of developers would probably adopt this code without even knowing it.<\/p>\n\n\n\n<p>Don\u2019t worry. This is a very rare occurrence. However\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.zdnet.com\/article\/microsoft-spots-malicious-npm-package-stealing-data-from-unix-systems\/\" target=\"_blank\">it has already happened<\/a>\u00a0a few times.<\/p>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-green-cyan-color\">Why Should You Create Your Own npm Package Libraries?<\/span><\/h4>\n\n\n\n<p>The fastest and safest way to write code is by\u00a0<em>not<\/em>\u00a0writing it. If you usually need to copy-paste some features among different projects, you might prefer to create a new JavaScript library and write those features only once.<\/p>\n\n\n\n<p>Then, the next time you need to use those features, you don\u2019t need to copy-paste or write them again \u2013 you just reuse your library. What\u2019s more, if you find a bug, you can solve it once and distribute the fix to the rest of your projects.<\/p>\n\n\n\n<p>Once you have written your node module, one of the easiest and fastest ways to include them in your projects is by using npm.<\/p>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\">Creating Your Own npm Package<\/span><\/h4>\n\n\n\n<ul><li>Create a New Git Repository<\/li><li>Create your package file<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cd folder\/to\/your project\n$ npm init<\/code><\/pre>\n\n\n\n<p>This tool will help you to create a new\u00a0<code>package.json<\/code>\u00a0asking you for the basic data needed. Some quick tips:<\/p>\n\n\n\n<ul><li><strong>Package name<\/strong>: remember to use\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/en.wiktionary.org\/wiki\/kebab_case\" target=\"_blank\">kebab-case<\/a>\u00a0as this is the convention in the npm community.<\/li><li><strong>Version:<\/strong>\u00a0you will be advised to start your project in version\u00a0<code>1.0.0<\/code>, however if you are just starting your project you\u2019re probably better off starting with something like\u00a0<code>0.0.1<\/code>\u00a0and switching to\u00a0<code>1.0.0<\/code>\u00a0when the code has been tested further. Also, other developers using your library will appreciate that your versioning reflects the state of your code.<\/li><li><strong>Description:<\/strong>\u00a0straight to the point and easy to understand.<\/li><li><strong>Entry point:<\/strong>\u00a0this is the entry file for your library. It is the file that other developers using your library will have to write when including it with\u00a0<code>reguire('your-package')<\/code>. If you are using only one file,\u00a0<code>index.js<\/code>\u00a0is enough. However, if your project is about to have more files, it is better that you use\u00a0<code>src\/index.js<\/code>.<\/li><\/ul>\n\n\n\n<p><strong>PS: <\/strong><em>If you don\u2019t want to fill the rest of the fields now, you can skip them and come back later to add them in the\u00a0package.json\u00a0file.<\/em><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"894\" height=\"762\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-24.png\" alt=\"\" class=\"wp-image-2222\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-24.png 894w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-24-300x256.png 300w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2022\/09\/image-24-768x655.png 768w\" sizes=\"(max-width: 894px) 100vw, 894px\" \/><\/figure>\n\n\n\n<h4>Adding Code To Your JavaScript Project and Establishing The Public API<\/h4>\n\n\n\n<p>Usually you have some code that you have been copy-pasting between various projects and you\u2019re now moving it to an npm module to reuse it in the future . Next you will need to think of the API that you will expose to the world.<\/p>\n\n\n\n<p><em><span class=\"has-inline-color has-vivid-cyan-blue-color\">Define Your Public API With <\/span><strong>module.exports<\/strong><\/em><\/p>\n\n\n\n<p>The way that you tell npm what your library is exposing is by using&nbsp;<code>module.exports<\/code>, an object that should be present in the entry point of your library (usually the&nbsp;<em>index.js<\/em>).<\/p>\n\n\n\n<p>You can specify whatever you want in the\u00a0<code>module.exports<\/code>. It could be a number, a function, a class\u2026 Imagine you are building the module from the previous sample\u00a0<em>bugfender-coffee-machine<\/em>. When a user types:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const coffeMachine = require('bugfender-coffee-machine');<\/code><\/pre>\n\n\n\n<p>Then, your code will go to the\u00a0<em>index.js<\/em>\u00a0and will ascertain the value of the module. For example, if inside your\u00a0<em>index.js<\/em>\u00a0you set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module.exports = 42;<\/code><\/pre>\n\n\n\n<p>Then&nbsp;<em>coffeMachine<\/em>&nbsp;will be a number with value&nbsp;<em>42.<\/em><\/p>\n\n\n\n<p>A more realistic approach would be to set this value to an instance of a class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ index.js\r\nmodule.exports = BugfenderCoffeeMachine; \r\n\r\n\/\/ In your app\r\nconst CoffeeMachine = require('bugfender-coffee-machine');\r\nconst coffeeMachine = new CoffeeMachine();<\/code><\/pre>\n\n\n\n<p><em>coffeeMachine<\/em>\u00a0is now an instance of\u00a0<em>BugfenderCoffeeMachine<\/em>\u00a0and you can now do:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>coffeeMachine.prepareCoffee() <\/code><\/pre>\n\n\n\n<h4>Add a README.md File<\/h4>\n\n\n\n<p>The easiest and trivial step is also one of the most important. Having a good&nbsp;<em>Readme<\/em>&nbsp;file ensures that other people will be able to understand what your library\u2019s about \u2013 and how to use it.<\/p>\n\n\n\n<p>You don\u2019t need to be too creative about it, just use a template like&nbsp;<a href=\"https:\/\/www.makeareadme.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">this one<\/a>&nbsp;and try to fill all the fields in a clear way.<\/p>\n\n\n\n<p>The users of your npm library will appreciate that you saved them time. Even a future version of you will be happy to have clear docs!<\/p>\n\n\n\n<h4>Publish on npm<\/h4>\n\n\n\n<p>Sign up to npm<\/p>\n\n\n\n<p>If you don\u2019t have a npm account, it\u2019s time to&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.npmjs.com\/signup\" target=\"_blank\">sign up<\/a>.<\/p>\n\n\n\n<p><em>npm<\/em>\u00a0is a\u00a0<em>command line tool.<\/em>\u00a0Open the console and write:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm login # you will be prompted your mail and password <\/code><\/pre>\n\n\n\n<p>You can check that you are correctly logged in with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm whoami<\/code><\/pre>\n\n\n\n<p><em>Tip: prior to publishing a new npm package, ensure that you are logged with the adequate user. This is especially important if you use the same machine for work and side projects.<\/em><\/p>\n\n\n\n<h4>Test Your Framework<\/h4>\n\n\n\n<p>We are almost ready to publish. But just before we do, we can run a quick local test.<\/p>\n\n\n\n<p>First, navigate to your library file path, the same path in which you placed your\u00a0<code>package.json<\/code>, and use the command\u00a0<code>npm link<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cd .\/route-to-your-library\/\r\n$ npm link # This adds the project to your local npm registry<\/code><\/pre>\n\n\n\n<p>Now create a new JavaScript project in your system and again use\u00a0<code>npm link<\/code>, but specifying the name of the package \u2013 the same one that you specified in the\u00a0<code>npm init<\/code>\u00a0step.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cd .\/route-to-a-new-javascript-project\/\r\n$ npm link &#91;name-of-the-package] # This installs the library in the project<\/code><\/pre>\n\n\n\n<p>You can use this process in the future when you plan to update your library so you don\u2019t need to publish every time you want to test new changes.<\/p>\n\n\n\n<h4>Publish<\/h4>\n\n\n\n<p>Now that you\u2019ve created the library and you tested it locally, it\u2019s time to share it with the world.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cd .\/route-to-your-library\/\r\n$ npm publish<\/code><\/pre>\n\n\n\n<p>npm will start to work on publishing your library to the official repository.<\/p>\n\n\n\n<p>When it finishes, the library should be available in\u00a0<a rel=\"noreferrer noopener\" href=\"https:\/\/www.npmjs.com\/\" target=\"_blank\">https:\/\/www.npmjs.com<\/a>\u00a0and every user in the world should be able to install it using\u00a0<code>npm install &lt;package-name><\/code><\/p>\n\n\n\n<h4><span class=\"has-inline-color has-vivid-cyan-blue-color\">Updating your library<\/span><\/h4>\n\n\n\n<p>If you start using it and other users adopt it, you will also need to maintain it. From time to time you will have to introduce new features or update deprecated code. Whenever you do that, remember to use&nbsp;<a href=\"https:\/\/semver.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Semantic Versioning<\/a>&nbsp;(major.minor.patch).<\/p>\n\n\n\n<p>npm eases the process of maintaining your code with the npm versioning tools:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ npm version patch # From 0.0.1 to 0.0.2\r\n$ npm version minor # From 0.1.0 to 0.2.0\r\n$ npm version major # From 1.0.0 to 2.0.0<\/code><\/pre>\n\n\n\n<p><strong>PS:<\/strong> <em>Be aware that\u00a0<code>npm version<\/code>\u00a0updates the\u00a0<code>package.json<\/code>, creates a commit and adds a TAG to git.<\/em><\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5>Reference:<\/h5>\n\n\n\n<p><a href=\"https:\/\/bugfender.com\/blog\/how-to-create-an-npm-package\/\">https:\/\/bugfender.com\/blog\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Node Package Manager, or npm (usually written in lower case) is one of the most commonly used package managers in JavaScript projects. It is built on top of Node and is so powerful that nearly everybody is using it. What Is A Package Manager? Imagine that you include library&nbsp;A&nbsp;to customize a text field. That library [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2223,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[26,5,83],"tags":[29,39,184,148,87],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/728"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=728"}],"version-history":[{"count":2,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/728\/revisions"}],"predecessor-version":[{"id":2224,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/728\/revisions\/2224"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/2223"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}