|
| 1 | +## Getting Started with Node and NPM |
| 2 | +Let's start with the basics. |
| 3 | + |
| 4 | +1. Install Node.js: https://nodejs.org. |
| 5 | +> :bulb: When you install Node.js, you'll want to ensure your `PATH` variable is set to your install path so you can call Node from anywhere. |
| 6 | +
|
| 7 | +2. Create a new directory named `hello-world`, add a new `app.js` file: |
| 8 | + ```js |
| 9 | + /* app.js */ |
| 10 | + console.log('Hello World!') |
| 11 | + ``` |
| 12 | + |
| 13 | +3. In the commmand prompt, run `node app.js`. |
| 14 | +> :bulb: Your environment variables are set at the time when the command prompt is opened, so ensure to open a new command prompt since step 1 if you get any errors about Node not being found. |
| 15 | +
|
| 16 | +4. Moving beyond from simple console applications... |
| 17 | + ```js |
| 18 | + /* app.js */ |
| 19 | + |
| 20 | + // Load the built-in 'http' module. |
| 21 | + var http = require('http'); |
| 22 | + |
| 23 | + // Create an http.Server object, and provide a callback that fires after 'request' events. |
| 24 | + var server = http.createServer(function (request, response) { |
| 25 | + // Respond to the http request with "Hello World" and a basic header. |
| 26 | + response.writeHead(200, {'Content-Type': 'text/plain'}); |
| 27 | + response.end('Hello World!\n'); |
| 28 | + }); |
| 29 | + |
| 30 | + // Try retrieving a port from an environment variable, otherwise fallback to 8080. |
| 31 | + var port = process.env.PORT || 8080; |
| 32 | + |
| 33 | + // Start listening on the specified port and print out a url to visit. |
| 34 | + server.listen(port); |
| 35 | + console.log('Listening on http://localhost:' + port); |
| 36 | + ``` |
| 37 | + |
| 38 | +5. In the commmand prompt, run `node app.js`, and visit the url that's printed out to the console. |
| 39 | + |
| 40 | +6. To stop the application, run `Ctrl+C`. |
| 41 | + |
| 42 | +## Working with npm packages |
| 43 | +As shown above, it's pretty impressive what you can do with so few lines of code in Node.js. Part of the philosophy of Node.js is that the core should remain as small as possible. It provides just enough built-in modules, such as filesystem and networking modules, to empower you to build scalable applications. However, you don't want to keep re-inventing the wheel every time for common tasks. |
| 44 | + |
| 45 | +Introducing, npm! |
| 46 | + |
| 47 | +npm is the package manager for JavaScript. npm ships with Node.js, so there's no need to install it seperately. |
| 48 | + |
| 49 | +### Using an existing npm package |
| 50 | +To get a sense for how to use npm packages in your app, let's try getting started with `express`, the most popular web framework for Node.js. |
| 51 | + |
| 52 | +1. Create a new directory entitled `my-express-app`, then install `express` from within that directory. When `express` is installed, the package and its dependencies appear under a `node_modules` folder. |
| 53 | + ``` |
| 54 | + C:\src> mkdir my-express-app |
| 55 | + C:\src> cd my-express-app |
| 56 | + C:\src\my-express-app> npm install express |
| 57 | + ``` |
| 58 | + |
| 59 | + > :bulb: We recommend starting with a short path like C:\src to work around any potential MAX_PATH issues. |
| 60 | +
|
| 61 | +2. Now, create a new file, `app.js`. This code will load the express module we just installed, and use it to start a lightweight web server. |
| 62 | + ```js |
| 63 | + /* app.js */ |
| 64 | + |
| 65 | + var express = require('express'); |
| 66 | + var app = express(); |
| 67 | + |
| 68 | + app.get('/', function (req, res) { |
| 69 | + res.send('Hello World!'); |
| 70 | + }) |
| 71 | + |
| 72 | + var port = process.env.PORT || 3000; |
| 73 | + |
| 74 | + app.listen(port); |
| 75 | + console.log('Listening on http://localhost:' + port); |
| 76 | + ``` |
| 77 | + |
| 78 | +3. Start the app by running `node app.js` in the command line. Tada! |
| 79 | + |
| 80 | +There are many more packages available at your disposal (200K and counting!). Head on over to https://www.npmjs.com to start exploring the ecosystem. |
| 81 | + |
| 82 | +> :bulb: Most of the packages available via npm tend to be pure JavaScript, but not all of them. For instance, there's a small percentage of native module addons available via npm that provide Node.js bindings, but ultimately call into native C++ code. This includes packages with `node-gyp`, `node-pre-gyp`, and `nan` dependencies. In order to install and run these packages, some additional machine configuration is required (described below). |
| 83 | +
|
| 84 | +### Managing npm dependencies |
| 85 | +Once you start installing npm packages, you'll need a way to keep track of all of your dependencies. In Node.js, you do this through a `package.json` file. |
| 86 | + |
| 87 | +1. To create a `package.json` file, run the `npm init` in your app directory. |
| 88 | + ``` |
| 89 | + C:\src\my-express-app> npm init |
| 90 | + ``` |
| 91 | + |
| 92 | +2. Npm will prompt you to fill in the details about your package. |
| 93 | +3. In the `package.json` file, there is a "dependencies" section, and within it, an entry for `"express"`. A value of `"*"` would mean that the latest version should be used. To add this entry automatically when you install a package, you can add a `--save` flag: `npm install express --save`. |
| 94 | + > :bulb: If you only require a dependency as part of a development environment, then you could/should install the package in the "devDependencies". This is accomplished by using the `--save-dev` parameter. For example: `npm install --save-dev mocha`. |
| 95 | +
|
| 96 | +4. Now that your packages are listed in `package.json`, npm will always know which dependencies are required for your app. If you ever need to restore your packages, you can run `npm install` from your package directory. |
| 97 | + |
| 98 | +> :bulb: When you distribute your application, we recommend adding the `node_modules` folder to `.gitignore` so that you don't clutter your repo with needless files. This also makes it easier to work with multiple platforms. If you want to keep things as similar as possible between machines, npm offers many options that enable you to fix the version numbers in `package.json`, and even more fine-grained control with `npm-shrinkwrap.json`. |
| 99 | +
|
| 100 | +### Publishing npm packages to the registry |
| 101 | +Once you've created a package, publishing it to the world is only one command away! |
| 102 | + |
| 103 | +`C:\src\my-express-app> npm publish` |
| 104 | + |
| 105 | +> :bulb: Use npm's private modules. |
| 106 | +
|
| 107 | +> :triangular_flag_on_post: **TODO** Add description about how to authorize the machine using `npm adduser`. |
| 108 | +
|
| 109 | +### Local vs. global packages |
| 110 | +There are two types of npm packages - locally installed packages and globally installed packages. It's not an exact science, but in general... |
| 111 | +* Locally installed packages are packages that are specific to your application |
| 112 | +* Globally installed packages tend to be CLI tools and the like |
| 113 | + |
| 114 | +We went through locally installed packages above, and installing packages globally is very similar. The only difference is the `-g` command. |
| 115 | + |
| 116 | +1. `npm install http-server -g` will install the module globally. |
| 117 | + |
| 118 | + > :bulb: The module will be installed to the path indicated by `npm bin -g`. |
| 119 | +
|
| 120 | +2. Run `http-server .` to start a basic fileserver from any directory. |
| 121 | + |
| 122 | +### And much more! |
| 123 | +* [npm docs and tutorials](https://docs.npmjs.com/) |
| 124 | +* [Laurie Voss - npm past, present, and future](https://www.youtube.com/watch?v=-fqu-5IuOkc) |
0 commit comments