{"id":42,"date":"2019-09-16T19:50:10","date_gmt":"2019-09-16T19:50:10","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/nodejs\/?p=42"},"modified":"2019-09-22T07:04:25","modified_gmt":"2019-09-22T07:04:25","slug":"05-node-js-package-manager","status":"publish","type":"post","link":"https:\/\/kindsonthegenius.com\/nodejs\/05-node-js-package-manager\/","title":{"rendered":"Node.js \u2013 Package Manager"},"content":{"rendered":"<p>As a Node.js developer, you&#8217;ll need several tools in addition to Node.js. This is especially true when you need to develop front-end components.<\/p>\n<p>These tools are known as packages.<\/p>\n<p>You use Node Package Manager (NPM)\u00a0 to install and manage this packages in a useful manner. In addition to providing for installation of packages, NPM also provides a useful interface to work with these packages.<\/p>\n<p>The two thing NPM provides are:<\/p>\n<ul>\n<li>Online package repositories<\/li>\n<li>Command-line utility<\/li>\n<\/ul>\n<p>We would now examine the basics of NPM. I would take you through how to install packages in local and global mode. Also, you will learn how to install, delete and update a package based on version. Then there&#8217;s something called package.json which is used to manage dependencies. You&#8217;ll learn that too.<\/p>\n<p>Feel free to watch the video lesson as well if you want.<\/p>\n<p>&nbsp;<\/p>\n<p>First, you may have to check if NPM is already installed. To do that, use the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$ npm --version\r\n2.8.1\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>If you see the version number as shown above, then node.js is installed. But if not, then you need to install it using the command below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$ sudo npm install npm -g\r\n<\/pre>\n<p>After installation, you may also have to update it to the latest version. To do that, use the command below.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">sudo npm install npm<span style=\"color: #996633;\">@latest<\/span> -g\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Installing Packages with NPM<\/strong><\/p>\n<p>To install a module\/package, type the command below, with the name of the module<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$ npm install &lt;Module_Name&gt;\r\n<\/pre>\n<p>For example, we want to install a module called Uglify (a JavaScript based tool used for minification), you use the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$ npm install uglify-js\r\n<\/pre>\n<p>Then to use this module in your program, you also have to write this code:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #008800; font-weight: bold;\">var<\/span> uglify<span style=\"color: #333333;\">=<\/span> require(<span style=\"background-color: #fff0f0;\">'uglify-js'<\/span>);\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Global and Local Mode<\/strong><\/p>\n<p>Packages are installed in Local Mode by default. This means that the package is installed in the directory that exists where Node.js is present. The directory is called node_modules. So to use a local package, we must use the require() method.<\/p>\n<p>Also, you can use the ls command to view all the locally installed modules.<\/p>\n<p>On the contrary, Global packages are placed in the system directory. They can be used in the command-line interface(CLI) but not importable using the require() function in Node. To install a package in global mode, use the switch &#8211;global or -g.<\/p>\n<p>For example, the previous package can be installed in global mode like this:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$ npm install uglify-js --global\r\n<\/pre>\n<p>Now, to list the modules available globally, use the command:<\/p>\n<p><!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$ npm ls -g\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Using package.json<\/strong><\/p>\n<p>package.json file is used to define the properties of a package. It is available in the root directory of any Node application.<\/p>\n<p><strong>Exercise<\/strong>: Open a package.json file and view the content<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Properties of Package.json<\/strong><\/p>\n<ul class=\"list\">\n<li><b>name<\/b> \u2212 the name of the package<\/li>\n<li><b>version<\/b> \u2212 the version of the package<\/li>\n<li><b>description<\/b> \u2212 the description of the package<\/li>\n<li><b>homepage<\/b> \u2212 the homepage of the package<\/li>\n<li><b>author<\/b> \u2212 the author of the package<\/li>\n<li><b>contributors<\/b> \u2212 the names of the contributors to the package<\/li>\n<li><b>dependencies<\/b> \u2212 a list of dependencies.The dependencies mentioned here are installed by default in the node_module folder of the package.<\/li>\n<li><b>repository<\/b> \u2212 a repository type and URL of the package<\/li>\n<li><b>main<\/b> \u2212 entry point to the package<\/li>\n<li><b>keywords<\/b> \u2212 set of keywords<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>To Uninstall a Module<\/strong>, use the command below:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$ npm uninstall uglify-js\r\n<\/pre>\n<p>Once a module is uninstalled, then you can check by examining the content of the node_modules directory using the ls command<\/p>\n<p>&nbsp;<\/p>\n<p><strong>To Update a Module:<\/strong><\/p>\n<p>When a module is updates, the version of the dependencies is upateded to the latest version. The command below updates a module:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$ npm update uglify-js\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>To Search for a Module<\/strong>, use the command:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">$ npm search uglify-js\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Creating a Module<\/strong><\/p>\n<p>This means that you need to generate a package.json file. This you can also do using NPM. The command below does it<\/p>\n<p>$ npm init<\/p>\n<p>Then follow the instructions.<\/p>\n<p>At the end, you will then execute:<\/p>\n<p>$ npm publish<\/p>\n<p>to finally publish the module<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a Node.js developer, you&#8217;ll need several tools in addition to Node.js. This is especially true when you need to develop front-end components. These tools &hellip; <\/p>\n","protected":false},"author":1,"featured_media":44,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[2,1],"tags":[4,3,5],"class_list":["post-42","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-node-js-tutorials","category-uncategorized","tag-node-package-manager","tag-npm","tag-package-json"],"_links":{"self":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":2,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":45,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/posts\/42\/revisions\/45"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/media\/44"}],"wp:attachment":[{"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kindsonthegenius.com\/nodejs\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}