﻿{"id":822,"date":"2021-01-09T12:23:31","date_gmt":"2021-01-09T04:23:31","guid":{"rendered":"https:\/\/byy3.com\/?p=822"},"modified":"2021-01-09T12:23:31","modified_gmt":"2021-01-09T04:23:31","slug":"linux%e7%8e%af%e5%a2%83node-js%e5%92%8cnpm-install%e7%9a%84%e5%ae%89%e8%a3%85%e5%92%8c%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/byy3.com\/?p=822","title":{"rendered":"linux\u73af\u5883node.js\u548cnpm install\u7684\u5b89\u88c5\u548c\u4f7f\u7528"},"content":{"rendered":"<h2 id=\"installation-on-a-mac-or-linux\">Installation on a Mac or Linux<\/h2>\n<p>In order to install everything on a Mac, we'll be running commands in Terminal.app, and Linux distributions vary.<\/p>\n<h3 id=\"install-nodejs-and-npm-1\">Install Node.js and npm<\/h3>\n<p>We\u2019re going to use\u00a0<a href=\"https:\/\/byy3.com\/go\/?url=https:\/\/github.com\/creationix\/nvm\" rel=\"nofollow\" >Node Version Manager (nvm)<\/a>\u00a0to install Node.js and npm.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"shell\">\n<pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">curl<\/span> -o- https:\/\/raw.githubusercontent.com\/creationix\/nvm\/v0.33.1\/install.sh <span class=\"token operator\">|<\/span> <span class=\"token function\">bash<\/span><\/code><\/pre>\n<\/div>\n<p>Open the\u00a0<code class=\"language-text\">~\/.bash_profile<\/code>\u00a0file, and make sure\u00a0<code class=\"language-text\">source ~\/.bashrc<\/code>\u00a0is written in there somewhere. Restart the terminal.<\/p>\n<p>Run the install command.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"shell\">\n<pre class=\"language-shell\"><code class=\"language-shell\">nvm <span class=\"token function\">install<\/span> node<\/code><\/pre>\n<\/div>\n<p>Run the use command.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"shell\">\n<pre class=\"language-shell\"><code class=\"language-shell\">nvm use node<\/code><\/pre>\n<\/div>\n<div class=\"gatsby-highlight\" data-language=\"terminal\">\n<pre class=\"language-terminal\"><code class=\"language-terminal\">Now using node v8.2.0 (npm v5.3.0)<\/code><\/pre>\n<\/div>\n<p>Now that Node.js and npm are installed, test them by typing\u00a0<code class=\"language-text\">node -v<\/code>\u00a0and\u00a0<code class=\"language-text\">npm -v<\/code>.<\/p>\n<p><span class=\"gatsby-resp-image-wrapper\"><a class=\"gatsby-resp-image-link\" href=\"https:\/\/www.taniarascia.com\/static\/42a2e888eaa4e8581605e8a2914be8e4\/6b95e\/term-1.png\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" ><img decoding=\"async\" class=\"gatsby-resp-image-image\" src=\"https:\/\/www.taniarascia.com\/static\/42a2e888eaa4e8581605e8a2914be8e4\/a6d36\/term-1.png\" sizes=\"(max-width: 650px) 100vw, 650px\" srcset=\"\/static\/42a2e888eaa4e8581605e8a2914be8e4\/222b7\/term-1.png 163w,\n\/static\/42a2e888eaa4e8581605e8a2914be8e4\/ff46a\/term-1.png 325w,\n\/static\/42a2e888eaa4e8581605e8a2914be8e4\/a6d36\/term-1.png 650w,\n\/static\/42a2e888eaa4e8581605e8a2914be8e4\/e548f\/term-1.png 975w,\n\/static\/42a2e888eaa4e8581605e8a2914be8e4\/3c492\/term-1.png 1300w,\n\/static\/42a2e888eaa4e8581605e8a2914be8e4\/6b95e\/term-1.png 1458w\" title=\"linux\u73af\u5883node.js\u548cnpm install\u7684\u5b89\u88c5\u548c\u4f7f\u7528\u63d2\u56fe\" alt=\"linux\u73af\u5883node.js\u548cnpm install\u7684\u5b89\u88c5\u548c\u4f7f\u7528\u63d2\u56fe\" \/><\/a><\/span><\/p>\n<p>All set.<\/p>\n<h2 id=\"create-a-project\">Create a Project<\/h2>\n<p>At this point, you're set to start setting up Gulp, Webpack, Browserify, or whatever your aim is. We can also create a simple project to test that everything is working properly.<\/p>\n<h3 id=\"initialize-project\">Initialize Project<\/h3>\n<p>Navigate to the directory in which you want your project to exist - in my case,\u00a0<strong>sites\/node-test<\/strong>.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"shell\">\n<pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token builtin class-name\">cd<\/span> sites\/node-test<\/code><\/pre>\n<\/div>\n<p>Now initalize a new project with npm.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"shell\">\n<pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">npm<\/span> init<\/code><\/pre>\n<\/div>\n<p>The following will pop up in the terminal, and prompt you for a few<\/p>\n<div class=\"gatsby-highlight\" data-language=\"terminal\">\n<pre class=\"language-terminal\"><code class=\"language-terminal\">This utility will walk you through creating a package.json file.\r\nIt only covers the most common items, and tries to guess sensible defaults.\r\n\r\nSee `npm help json` for definitive documentation on these fields\r\nand exactly what they do.\r\n\r\nUse `npm install &lt;pkg&gt;` afterwards to install a package and\r\nsave it as a dependency in the package.json file.<\/code><\/pre>\n<\/div>\n<p>First, it will ask for a package name.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"shell\">\n<pre class=\"language-shell\"><code class=\"language-shell\">node-test<\/code><\/pre>\n<\/div>\n<p>Version number.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"text\">\n<pre class=\"language-text\"><code class=\"language-text\">1.0.0<\/code><\/pre>\n<\/div>\n<p>Description.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"text\">\n<pre class=\"language-text\"><code class=\"language-text\">Creating my first \"Hello, World!\" Node project.<\/code><\/pre>\n<\/div>\n<p>The rest you can just press enter and skip. Now you'll notice we have a\u00a0<strong>package.json<\/strong>\u00a0file that contains all the information we entered.<\/p>\n<div class=\"filename\">package.json<\/div>\n<div class=\"gatsby-highlight\" data-language=\"js\">\n<pre class=\"language-js\"><code class=\"language-js\"><span class=\"token punctuation\">{<\/span>\r\n <span class=\"token string\">\"name\"<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">\"node-test\"<\/span><span class=\"token punctuation\">,<\/span>\r\n <span class=\"token string\">\"version\"<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">\"1.0.0\"<\/span><span class=\"token punctuation\">,<\/span>\r\n <span class=\"token string\">\"description\"<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">\"Creating my first \\\"Hello, World!\\\" Node project.\"<\/span><span class=\"token punctuation\">,<\/span>\r\n <span class=\"token string\">\"main\"<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">\"index.js\"<\/span><span class=\"token punctuation\">,<\/span>\r\n <span class=\"token string\">\"scripts\"<\/span><span class=\"token operator\">:<\/span> <span class=\"token punctuation\">{<\/span>\r\n <span class=\"token string\">\"test\"<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">\"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\"<\/span>\r\n <span class=\"token punctuation\">}<\/span><span class=\"token punctuation\">,<\/span>\r\n <span class=\"token string\">\"author\"<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">\"Tania Rascia\"<\/span><span class=\"token punctuation\">,<\/span>\r\n <span class=\"token string\">\"license\"<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">\"ISC\"<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/code><\/pre>\n<\/div>\n<p>A\u00a0<strong>package.json<\/strong>\u00a0is a file that contains metadata about the project, and handles the dependencies (additional software and modules) of the project.<\/p>\n<p>Now, we're going to install our first dependency - a very important and useful package called\u00a0<strong>left-pad<\/strong>, which will add white space to the left side of a string, adding up to a number.<\/p>\n<p>For example, writing this:<\/p>\n<div class=\"gatsby-highlight\" data-language=\"js\">\n<pre class=\"language-js\"><code class=\"language-js\"><span class=\"token function\">leftPad<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'String'<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<\/div>\n<p>Will output this:<\/p>\n<div class=\"gatsby-highlight\" data-language=\"terminal\">\n<pre class=\"language-terminal\"><code class=\"language-terminal\">console<\/code><\/pre>\n<\/div>\n<div class=\"gatsby-highlight\" data-language=\"js\">\n<pre class=\"language-js\"><code class=\"language-js\">String<\/code><\/pre>\n<\/div>\n<p><a href=\"https:\/\/byy3.com\/go\/?url=https:\/\/www.npmjs.com\/package\/left-pad\" rel=\"nofollow\" >left-pad<\/a>\u00a0is a package on npm, which as we stated previously contains the registry for all publicly available packages.<\/p>\n<h3 id=\"install-dependencies\">Install dependencies<\/h3>\n<p>To install a dependency with npm, we use the command\u00a0<code class=\"language-text\">npm install dependency-name-here<\/code>. Now, simply running\u00a0<code class=\"language-text\">npm install<\/code>\u00a0will download the dependency, but it won't save it to the project. Since we've already created our\u00a0<strong>package.json<\/strong>, we'll use the flag\u00a0<code class=\"language-text\">--save<\/code>\u00a0to install the dependency\u00a0<em>and<\/em>\u00a0add it to\u00a0<strong>package.json<\/strong>.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"shell\">\n<pre class=\"language-shell\"><code class=\"language-shell\"><span class=\"token function\">npm<\/span> <span class=\"token function\">install<\/span> left-pad --save<\/code><\/pre>\n<\/div>\n<p>As long as you ran this command inside the project directory, it will successfully install the dependency by creating a\u00a0<strong>node_modules<\/strong>\u00a0directory. It will also create a\u00a0<strong>package-lock.json<\/strong>\u00a0file, which we can ignore. Finally, it updated our\u00a0<strong>package.json<\/strong>\u00a0file with a new line.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"js\">\n<pre class=\"language-js\"><code class=\"language-js\"><span class=\"token string\">\"dependencies\"<\/span><span class=\"token operator\">:<\/span> <span class=\"token punctuation\">{<\/span>\r\n <span class=\"token string\">\"left-pad\"<\/span><span class=\"token operator\">:<\/span> <span class=\"token string\">\"^1.1.3\"<\/span>\r\n<span class=\"token punctuation\">}<\/span><\/code><\/pre>\n<\/div>\n<p>Now the project recognizes the left-pad dependency as existing<\/p>\n<blockquote><p>You can also run\u00a0<code class=\"language-text\">npm install --save-dev<\/code>\u00a0to specify that the dependency will only be used for development (not production) purposes.<\/p><\/blockquote>\n<h3 id=\"run-node-in-the-terminal\">Run Node in the terminal<\/h3>\n<p>Let's create\u00a0<strong>index.js<\/strong>\u00a0in the root of our directory. This is everything you should have now:<\/p>\n<p><span class=\"gatsby-resp-image-wrapper\"><a class=\"gatsby-resp-image-link\" href=\"https:\/\/www.taniarascia.com\/static\/0999a65c5c9c2a2d7eff5cd1d5ec7b44\/2e237\/Screen-Shot-2017-07-21-at-5.28.47-PM.png\" target=\"_blank\" rel=\"noopener\" rel=\"nofollow\" ><img decoding=\"async\" class=\"gatsby-resp-image-image\" src=\"https:\/\/www.taniarascia.com\/static\/0999a65c5c9c2a2d7eff5cd1d5ec7b44\/a6d36\/Screen-Shot-2017-07-21-at-5.28.47-PM.png\" sizes=\"(max-width: 650px) 100vw, 650px\" srcset=\"\/static\/0999a65c5c9c2a2d7eff5cd1d5ec7b44\/222b7\/Screen-Shot-2017-07-21-at-5.28.47-PM.png 163w,\n\/static\/0999a65c5c9c2a2d7eff5cd1d5ec7b44\/ff46a\/Screen-Shot-2017-07-21-at-5.28.47-PM.png 325w,\n\/static\/0999a65c5c9c2a2d7eff5cd1d5ec7b44\/a6d36\/Screen-Shot-2017-07-21-at-5.28.47-PM.png 650w,\n\/static\/0999a65c5c9c2a2d7eff5cd1d5ec7b44\/2e237\/Screen-Shot-2017-07-21-at-5.28.47-PM.png 790w\" title=\"linux\u73af\u5883node.js\u548cnpm install\u7684\u5b89\u88c5\u548c\u4f7f\u7528\u63d2\u56fe1\" alt=\"linux\u73af\u5883node.js\u548cnpm install\u7684\u5b89\u88c5\u548c\u4f7f\u7528\u63d2\u56fe1\" \/><\/a><\/span><\/p>\n<p>For future reference, don't bother looking in the\u00a0<strong>node_modules<\/strong>\u00a0rabbit hole. It will get really overwhelming with bigger projects.<\/p>\n<p>In order to use a dependency, we use\u00a0<code class=\"language-text\">require()<\/code>\u00a0and put it in a variable, like so:<\/p>\n<div class=\"gatsby-highlight\" data-language=\"js\">\n<pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const<\/span> leftPad <span class=\"token operator\">=<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'left-pad'<\/span><span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<\/div>\n<p>This will be the entirety of our\u00a0<strong>index.js<\/strong>\u00a0file, in which we require left-pad, run a\u00a0<code class=\"language-text\">leftPad()<\/code>\u00a0function, and send it to the console.<\/p>\n<div class=\"filename\">index.js<\/div>\n<div class=\"gatsby-highlight\" data-language=\"js\">\n<pre class=\"language-js\"><code class=\"language-js\"><span class=\"token keyword\">const<\/span> leftPad <span class=\"token operator\">=<\/span> <span class=\"token function\">require<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'left-pad'<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\">\/\/ Require left pad<\/span>\r\n<span class=\"token keyword\">const<\/span> output <span class=\"token operator\">=<\/span> <span class=\"token function\">leftPad<\/span><span class=\"token punctuation\">(<\/span><span class=\"token string\">'Hello, World!'<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">15<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token comment\">\/\/ Define output<\/span>\r\n\r\n<span class=\"token comment\">\/\/ Send output to the console<\/span>\r\nconsole<span class=\"token punctuation\">.<\/span><span class=\"token function\">log<\/span><span class=\"token punctuation\">(<\/span>output<span class=\"token punctuation\">)<\/span><\/code><\/pre>\n<\/div>\n<p>Since Node.js is not recognized by the browser, we'll be testing this in the console. In your shell, run the\u00a0<code class=\"language-text\">node<\/code>\u00a0command followed by the filename in the root of your project.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"shell\">\n<pre class=\"language-shell\"><code class=\"language-shell\">node index.js<\/code><\/pre>\n<\/div>\n<p>If everything went well, you should have printed\u00a0<code class=\"language-text\">Hello, World!<\/code>\u00a0to the console, with two spaces on the left.<\/p>\n<div class=\"gatsby-highlight\" data-language=\"terminal\">\n<pre class=\"language-terminal\"><code class=\"language-terminal\">Hello, World!<\/code><\/pre>\n<\/div>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this tutorial, we learned the following:<\/p>\n<ul>\n<li>What Node.js is<\/li>\n<li>What npm is<\/li>\n<li>How to install Node.js and npm on Windows or Mac<\/li>\n<li>How to make a local project<\/li>\n<li>How to install a dependency with npm<\/li>\n<li>How to run a file using a\u00a0<strong>node_modules<\/strong>\u00a0dependency in a shell<\/li>\n<\/ul>\n<p>If you got lost at any point,\u00a0<a href=\"https:\/\/byy3.com\/go\/?url=https:\/\/github.com\/taniarascia\/node-test\" rel=\"nofollow\" >view the source on GitHub<\/a>.<\/p>\n<p>With this knowledge, you're ready to start using Gulp, Grunt, Webpack, Browserify, or anything else that depends on Node.js or npm.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Installation on a Mac or Linux In order to install ever [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14,15],"tags":[368,158,364,365,366,367],"class_list":["post-822","post","type-post","status-publish","format-standard","hentry","category-linux","category-wordpress","tag-linux-node","tag-linux","tag-node","tag-node-js","tag-npm","tag-npn-install"],"_links":{"self":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts\/822","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=822"}],"version-history":[{"count":0,"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts\/822\/revisions"}],"wp:attachment":[{"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=822"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=822"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=822"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}