{"id":764,"date":"2021-02-21T20:20:46","date_gmt":"2021-02-21T19:20:46","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=764"},"modified":"2021-02-21T20:43:47","modified_gmt":"2021-02-21T19:43:47","slug":"how-to-create-an-npm-package-ready-to-distribute-from-scratch","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2021\/02\/21\/how-to-create-an-npm-package-ready-to-distribute-from-scratch\/","title":{"rendered":"How to Create an npm Package 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<h5>What Is A Package Manager?<\/h5>\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&nbsp;<strong>C<\/strong>&nbsp;uses another five different libraries to handle different languages and you end up with a scheme like this one.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/bugfender.com\/wp-content\/uploads\/2020\/07\/dependency-tree-ok-1024x498-1.png\" alt=\"\" width=\"422\" height=\"205\"\/><\/figure>\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>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. That\u2019s exactly the problem that npm solves.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" src=\"https:\/\/bugfender.com\/wp-content\/uploads\/2020\/07\/dependency-tree-wrong-1024x498-1.png\" alt=\"\" width=\"423\" height=\"206\"\/><\/figure>\n\n\n\n<p>When you create an npm library you will create a json file called<span class=\"has-inline-color has-vivid-cyan-blue-color\">&nbsp;<code><strong>package.json<\/strong><\/code><\/span>&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<h5>Is There Any Downside of Using a Package Manager?<\/h5>\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&nbsp;<a href=\"https:\/\/www.zdnet.com\/article\/microsoft-spots-malicious-npm-package-stealing-data-from-unix-systems\/\" target=\"_blank\" rel=\"noreferrer noopener\">it has already happened<\/a>&nbsp;a few times.<\/p>\n\n\n\n<p>Another example is the&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/arstechnica.com\/information-technology\/2016\/03\/rage-quit-coder-unpublished-17-lines-of-javascript-and-broke-the-internet\/\" target=\"_blank\">dispute in 2019 between an open-source developer and an American company<\/a>&nbsp;that almost broke the world\u2019s internet. Long story short, the developer decided to remove a quite-trivial-yet-often-used library from npm and suddenly the dependency tree of thousands of projects around the world was broken.<\/p>\n\n\n\n<h5>Why Should You Create Your Own npm Package Libraries?<\/h5>\n\n\n\n<p>The fastest and safest way to write code is by&nbsp;<em>not<\/em>&nbsp;writing 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 by using npm. 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<h3><span class=\"has-inline-color has-vivid-red-color\">Creating Your Own npm Package<\/span><\/h3>\n\n\n\n<h4>1. Create a New Git Repository<\/h4>\n\n\n\n<p>If you are starting from scratch, it is time to&nbsp;<em>git init.<\/em>&nbsp;Then you can go to GitHub\/gitLab and create a new git repo.<\/p>\n\n\n\n<h4>2. Create your package file<\/h4>\n\n\n\n<p>open the terminal and navigate to your project, then type &#8220;npm init&#8221; <\/p>\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&nbsp;<code>package.json<\/code>&nbsp;asking you for the basic data needed. Some quick tips:<\/p>\n\n\n\n<ul><li><strong>Package name<\/strong>: remember to use&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/en.wiktionary.org\/wiki\/kebab_case\" target=\"_blank\">kebab-case<\/a>&nbsp;as this is the convention in the npm community.<\/li><li><strong>Version:<\/strong>&nbsp;you will be advised to start your project in version&nbsp;<code>1.0.0<\/code>, however if you are just starting your project you\u2019re probably better off starting with something like&nbsp;<code>0.0.1<\/code>&nbsp;and switching to&nbsp;<code>1.0.0<\/code>&nbsp;when 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>&nbsp;straight to the point and easy to understand.<\/li><li><strong>Entry point:<\/strong>&nbsp;this 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&nbsp;<code>reguire('your-package')<\/code>. If you are using only one file,&nbsp;<code>index.js<\/code>&nbsp;is enough. However, if your project is about to have more files, it is better that you use&nbsp;<code>src\/index.js<\/code><\/li><\/ul>\n\n\n\n<p>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&nbsp;<em>package.json<\/em>&nbsp;file.<\/p>\n\n\n\n<h4>3. 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 moving it to an npm module to reuse it in the future.<\/p>\n\n\n\n<p>If that\u2019s the case, now it\u2019s time to add this code to the npm project. Next you will need to think of the API that you will expose to the world.<\/p>\n\n\n\n<p><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Define Your Public API With module.exports<\/strong><\/span><\/p>\n\n\n\n<p>The way that you tell npm what your library is exposing is by using<strong>&nbsp;<code>module.exports<\/code><\/strong>, 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&nbsp;<code><strong>module.exports<\/strong><\/code>. It could be a number, a function, a class\u2026 Imagine you are building the module from the npm-project-name &#8220;<em><strong>my-muster-project<\/strong><\/em>&#8220;. When a user types:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const musterVariable = require('my-muster-project');<\/code><\/pre>\n\n\n\n<p>Then, your code will go to the&nbsp;<em>index.js<\/em>&nbsp;and will ascertain the value of the module. For example, if inside your&nbsp;<em>index.js<\/em>&nbsp;you 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>musterVariable<\/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\nmodule.exports = MyMusterProject; \n\n\/\/ In your app\nconst MusterClass = require('my-muster-project');\nconst musterVariable = new MusterClass();<\/code><\/pre>\n\n\n\n<p>musterVariable&nbsp;is now an instance of&nbsp;MyMusterProject&nbsp;and you can now do:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> \/\/call some function of your npm-project\nmusterVariable.prepareCoffee() <\/code><\/pre>\n\n\n\n<h4>4. 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 rel=\"noreferrer noopener\" href=\"https:\/\/www.makeareadme.com\/\" target=\"_blank\">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<p><span class=\"has-inline-color has-vivid-red-color\">example : <\/span><\/p>\n\n\n\n<p><br># Markdown Input<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Foobar\n\nFoobar is a Python library for dealing with word pluralization.\n\n## Installation\n\nUse the package manager &#91;pip](https:\/\/pip.pypa.io\/en\/stable\/) to install foobar.\n\n```bash\npip install foobar\n```\n\n## Usage\n\n```python\nimport foobar\n\nfoobar.pluralize('word') # returns 'words'\nfoobar.pluralize('goose') # returns 'geese'\nfoobar.singularize('phenomena') # returns 'phenomenon'\n```\n\n## Contributing\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n## License\n&#91;MIT](https:\/\/choosealicense.com\/licenses\/mit\/)<\/code><\/pre>\n\n\n\n<p>#And here, the rendered view:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"716\" height=\"755\" src=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/02\/image-4.png\" alt=\"\" class=\"wp-image-880\" srcset=\"https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/02\/image-4.png 716w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/02\/image-4-285x300.png 285w, https:\/\/nguenkam.com\/blog\/wp-content\/uploads\/2021\/02\/image-4-24x24.png 24w\" sizes=\"(max-width: 716px) 100vw, 716px\" \/><\/figure>\n\n\n\n<h4>5. Publish on npm<\/h4>\n\n\n\n<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\">Sign up to npm<\/span><\/h5>\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>&nbsp;is a&nbsp;<em>command line tool.<\/em>&nbsp;Open 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<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Test Your Framework<\/strong><\/span><\/h5>\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&nbsp;<code>package.json<\/code>, and use the command&nbsp;<code>npm link<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cd .\/route-to-your-library\/\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&nbsp;<code>npm link<\/code>, but specifying the name of the package \u2013 the same one that you specified in the&nbsp;<code>npm init<\/code>&nbsp;step.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ cd .\/route-to-a-new-javascript-project\/\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<h5><span class=\"has-inline-color has-vivid-cyan-blue-color\"><strong>Publish<\/strong><\/span><\/h5>\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\/\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&nbsp;<a rel=\"noreferrer noopener\" href=\"https:\/\/www.npmjs.com\/\" target=\"_blank\">https:\/\/www.npmjs.com<\/a>&nbsp;and every user in the world should be able to install it using<strong>&nbsp;<code>npm install &lt;package-name&gt;<\/code><\/strong>.<\/p>\n\n\n\n<h4>6. Updating your library<\/h4>\n\n\n\n<p>We\u2019re arriving at the end of the article, but it\u2019s only the beginning of your library.<\/p>\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\n$ npm version minor # From 0.1.0 to 0.2.0\n$ npm version major # From 1.0.0 to 2.0.0<\/code><\/pre>\n\n\n\n<p>Be aware that&nbsp;<strong><code>npm version<\/code>&nbsp;<\/strong>updates the<strong>&nbsp;<code>package.json<\/code><\/strong>, creates a commit and adds a TAG to git.<\/p>\n\n\n\n<p><\/p>\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":883,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,5],"tags":[218,184,148],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/764"}],"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=764"}],"version-history":[{"count":9,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/764\/revisions"}],"predecessor-version":[{"id":882,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/764\/revisions\/882"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/883"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=764"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=764"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=764"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}