{"id":2000,"date":"2022-06-23T12:00:00","date_gmt":"2022-06-23T10:00:00","guid":{"rendered":"https:\/\/kindsonthegenius.com\/blog\/how-to-publish-package-to-npm-step-by-step\/"},"modified":"2026-07-05T03:26:33","modified_gmt":"2026-07-05T01:26:33","slug":"how-to-publish-package-to-npm-step-by-step","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/blog\/how-to-publish-package-to-npm-step-by-step\/","title":{"rendered":"How to Publish Package to NPM \u2013 Step by Step"},"content":{"rendered":"<p>In this tutorial, you will learn how to create and publish your first npm package step by step.<\/p>\n<h4><strong>Prerequisite<\/strong><\/h4>\n<ul>\n<li>Make sure you have an NPM account. If not, just go to <a href=\"https:\/\/www.npmjs.com\/\" target=\"_blank\" rel=\"noopener\">https:\/\/www.npmjs.com\/<\/a> and create a free account.<\/li>\n<li>You need to have and IDE, either VS Code or IntelliJ<\/li>\n<\/ul>\n<p>We would cover the following<\/p>\n<ol>\n<li><a href=\"#t1\">Create a Node Application<\/a><\/li>\n<li><a href=\"#t2\">Test the Package<\/a><\/li>\n<li><a href=\"#t3\">Publish the Application<\/a><\/li>\n<li><a href=\"#t4\">Publish a Scoped Package<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t1\">1. Create a Node Application<\/strong><\/h4>\n<p>We&#8217;ll begin by creating a simple node application. This is the application that would contain the functions we want to expose through npm. So follow the steps below:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create an empty Github repository<\/p>\n<p><strong>Step 2<\/strong> &#8211; Create an empty directory and open it in VS Code ( you can also use IntelliJ).<\/p>\n<p><strong>Step 3<\/strong> &#8211; Create an empty\u00a0 sub-directory ( I call it package). This will contain the actual package we&#8217;ll be publishing. By the way, a package is just a fancy name for a collection of one or more files (modules) which contains function(s)<\/p>\n<p><strong>Step 4<\/strong> &#8211; Navigate into your package sub-folder<\/p>\n<p><strong>Step 5<\/strong> &#8211; Initialize your application by copying the commands from git and running on your command line.<\/p>\n<p><strong>Step 6<\/strong> &#8211; Initialize the node application using npm init command and fill in the prompts.<\/p>\n<p><strong>Note<\/strong> &#8211; Give the package a name. Here I call it <strong>genius-greet<\/strong>. The name of the package is important!<\/p>\n<p><strong>Step 7<\/strong> &#8211; Create a file inside the package folder. Name it index.js just like in the npm init prompt. The content of the file is as follows:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/Collection of Functions to say a greeting<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">function<\/span> sayHello(name) {\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>Hello ${name}<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>\r\n}\r\n\r\n <span style=\"color: #008800; font-weight: bold;\">const<\/span> sayHi <span style=\"color: #333333;\">=<\/span> (name) <span style=\"color: #333333;\">=&gt;<\/span> {\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>Hi ${name}<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>\r\n}\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">function<\/span> sayWelcome(name) {\r\n    <span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>Welcome ${name}<span style=\"color: #ff0000; background-color: #ffaaaa;\">`<\/span>\r\n}\r\n\r\n module.exports <span style=\"color: #333333;\">=<\/span> {sayHello, sayHi, sayWelcome}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t2\">2. Test the Application<\/strong><\/h4>\n<p>Generally before you publish anything, it&#8217;s good to test it to make sure it works as expected. So we would create a test application\u00a0 that would use the package we are creating.<\/p>\n<p>We would use the<strong> npm link<\/strong> command to simulate the behaviour of an npm package. The steps are given below:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Create a folder to hold the test application. <strong>npm-demo<\/strong><\/p>\n<p><strong>Step 2<\/strong> &#8211; Login to npm using the command npm login and enter your username and password.<\/p>\n<p><strong>Step 3<\/strong> &#8211; While in the original package directory, run the command <strong>npm link.<\/strong> This command allows your local package to to simulate an npm package.<\/p>\n<p><strong>Step 4<\/strong> &#8211; Navigate into the test directory.<\/p>\n<p><strong>Step 5<\/strong> &#8211; While in this directory, run the command npm link <strong>genius-greet<\/strong>. This command is equivalent to <strong>npm install<\/strong>. You will notice that our package is install in the test application.<\/p>\n<p><strong>Step 6<\/strong>\u00a0&#8211; Create a file named <strong>client.js<\/strong> ( you can use any name). The content of the file is given below<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #888888;\">\/\/A file to test our new package<\/span>\r\n\r\n<span style=\"color: #008800; font-weight: bold;\">const<\/span> greet <span style=\"color: #333333;\">=<\/span> require(<span style=\"background-color: #fff0f0;\">\"genius-greet\"<\/span>)\r\n\r\nconsole.log(greet.sayHi(<span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span>))\r\n\r\nconsole.log(greet.sayHello(<span style=\"background-color: #fff0f0;\">\"Kindson\"<\/span>))\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 7<\/strong> &#8211; Test the application by running the command node client.js. You will notice that the greetings are logged in the console. This means that everything worked as expected.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t3\">3. Publish the Package<\/strong><\/h4>\n<p>Once you&#8217;ve confirmed that everything worked as expected. It&#8217;s time to publish the package. This is done using a single command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">npm publish\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If this command runs successfully, your package will be published and available in npm! So you can check that it&#8217;s there. After now you can just install your package just like any other package using <em><strong>npm install genius greet<\/strong><\/em>.<\/p>\n<p>&nbsp;<\/p>\n<h4><strong id=\"t4\">4. Publish a Scoped Package<\/strong><\/h4>\n<p>A scoped package is a package published under some user. The name of this package is prefixed by the your npm username. For example <strong><em>kindsonthegenius\/greetings<\/em><\/strong>.<\/p>\n<p>This is very important because you many have a package with an already existing name. In that case you need to publish it as a scoped package. That requires only two steps as given below:<\/p>\n<p><strong>Step 1<\/strong> &#8211; Change the name of the package in the package.json file to be prefixed with your username<\/p>\n<p><strong>Step\u00a02<\/strong> &#8211; Publish the package using the following command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">npm publish --access=public\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Now you can check your npm profile and see that the package is available there as well.<\/p>\n<p>If you&#8217;ve come this far, big thumbs up to you! If you have any issues, just write me a comment below. Also do watch the video tutorial on my <a href=\"https:\/\/www.youtube.com\/c\/KindsonTheTechPro\" target=\"_blank\" rel=\"noopener\">YouTube Channel &#8211; Kindson The Tech Pro<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to create and publish your first npm package step by step. Prerequisite Make sure you have an NPM &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"pagelayer_contact_templates":[],"_pagelayer_content":"","footnotes":""},"categories":[414],"tags":[],"class_list":["post-2000","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/comments?post=2000"}],"version-history":[{"count":1,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2000\/revisions"}],"predecessor-version":[{"id":2168,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/posts\/2000\/revisions\/2168"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/media?parent=2000"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/categories?post=2000"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/blog\/wp-json\/wp\/v2\/tags?post=2000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}