{"id":1295,"date":"2018-01-08T19:05:41","date_gmt":"2018-01-09T00:05:41","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1295"},"modified":"2018-01-08T19:05:41","modified_gmt":"2018-01-09T00:05:41","slug":"javascript-use-npm-install-packages","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/01\/08\/javascript-use-npm-install-packages\/","title":{"rendered":"JavaScript: How to use NPM to install packages"},"content":{"rendered":"<p>NPM or &#8220;npm&#8221; stands for Node Package Manager. It is the default package manager for the JavaScript runtime environment Node.js. The NPM program is installed on your computer when you install Node.js. The website <a href=\"https:\/\/www.npmjs.com\/\">npmjs.com<\/a> contains hundreds of thousands of free open source code packages that can be downloaded and used via npm.<\/p>\n<p><strong>Downloading Individual Packages<\/strong><\/p>\n<p>Individual Node.js packages can be downloaded for use as modules in your projects by typing the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">npm install<\/code> command in your command window. First you cd to the current project directory. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">cd C:\\Users\\christon\\Desktop\\Projects\\travel-site<\/pre>\n<p>Then type <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">npm install<\/code> and the name of the module you want to use. For example, jquery:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">npm install jquery<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img_ds\" class=\"alignnone wp-image-1297 size-full\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install_jquery.png\" alt=\"\" width=\"583\" height=\"121\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install_jquery.png 583w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install_jquery-300x62.png 300w\" sizes=\"auto, (max-width: 583px) 100vw, 583px\" \/><\/p>\n<p>When npm installs a package, it creates a folder in your project called &#8220;node_modules&#8221; and inside this folder there is another folder with the name of the package that was installed. For example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img_ds\" class=\"alignnone size-full wp-image-1317\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/node_modules2.png\" alt=\"\" width=\"200\" height=\"120\" \/><\/p>\n<p>From the npmjs.com website:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img_ds\" class=\"alignnone wp-image-1310 \" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install_a_lot-1024x353.jpg\" alt=\"\" width=\"717\" height=\"247\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install_a_lot-1024x353.jpg 1024w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install_a_lot-300x103.jpg 300w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install_a_lot-768x265.jpg 768w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install_a_lot-676x233.jpg 676w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install_a_lot.jpg 1143w\" sizes=\"auto, (max-width: 717px) 100vw, 717px\" \/><\/p>\n<p><strong>npm init creates package.json<\/strong><\/p>\n<p>The package.json file tells Node.js which modules or packages are installed for your project. This doesn&#8217;t have to be created manually. The command <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">npm init<\/code> will generate this file automatically for you. First cd to the project folder, then run this command:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">npm init<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img_ds\" class=\"alignnone wp-image-1313 size-full\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_init.png\" alt=\"\" width=\"614\" height=\"203\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_init.png 614w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_init-300x99.png 300w\" sizes=\"auto, (max-width: 614px) 100vw, 614px\" \/><\/p>\n<p>Then click enter a few more times and you should see this confirmation screen:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img_ds\" class=\"alignnone wp-image-1314 size-full\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_init_confirmation.png\" alt=\"\" width=\"636\" height=\"578\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_init_confirmation.png 636w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_init_confirmation-300x273.png 300w\" sizes=\"auto, (max-width: 636px) 100vw, 636px\" \/><\/p>\n<p>When this step is complete, there is a package.json file in the project folder.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img_ds\" class=\"alignnone size-full wp-image-1316\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/packag_json.png\" alt=\"\" width=\"152\" height=\"145\" \/><\/p>\n<p>Note: In the previous example I used the command <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">npm install jquery --save<\/code>. The &#8211;save option instructs npm to include the package inside of the dependencies section of your package.json file automatically (which saves the extra step of doing this manually). As of npm 5.0.0, installed modules are added to the package.json file as a dependency by default, so the &#8211;save option is no longer needed. This of course happens only if a package.json file already exists in your project folder.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" id=\"img_ds\" class=\"alignnone wp-image-1303 size-full\" src=\"http:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install-save2.png\" alt=\"\" width=\"539\" height=\"208\" srcset=\"https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install-save2.png 539w, https:\/\/bluegalaxy.info\/codewalk\/wp-content\/uploads\/2018\/01\/npm_install-save2-300x116.png 300w\" sizes=\"auto, (max-width: 539px) 100vw, 539px\" \/><\/p>\n<p><strong>Use package.json to install list of packages<br \/>\n<\/strong><\/p>\n<p>Let&#8217;s say there are a couple of packages listed in your package.json file that were added by a combination of using <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">npm init<\/code> and <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">npm install<\/code>. There is another way to use the package.json file that is very handy. If you want to work on a new project and don&#8217;t have any of the modules installed yet, you can get a pre-defined package.json file and use that to install all of the modules you need. Just place the pre-defined package.json file into your project folder, (overwriting the previous version if necessary), and run <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">npm install<\/code> with no package specified. If no package is specified, npm will open the package.json and install all of the modules it finds listed there.<\/p>\n<p>For more about using NPM, see:<br \/>\n<a href=\"https:\/\/www.npmjs.com\/\">https:\/\/www.npmjs.com\/<\/a><br \/>\n<a href=\"https:\/\/www.w3schools.com\/nodejs\/nodejs_npm.asp\">https:\/\/www.w3schools.com\/nodejs\/nodejs_npm.asp<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NPM or &#8220;npm&#8221; stands for Node Package Manager. It is the default package manager for the JavaScript runtime environment Node.js. The NPM program is installed on your computer when you install Node.js. The website npmjs.com contains hundreds of thousands of free open source code packages that can be downloaded and used via npm. Downloading Individual &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/01\/08\/javascript-use-npm-install-packages\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript: How to use NPM to install packages<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[45,58,91],"class_list":["post-1295","post","type-post","status-publish","format-standard","hentry","category-javascript-language","tag-javascript","tag-node-js","tag-npm"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1295"}],"version-history":[{"count":17,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1295\/revisions"}],"predecessor-version":[{"id":1321,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1295\/revisions\/1321"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}