It's all about
connecting the dots

Install Node.js

Node.js Install Location

After you install Node.js by using an installer from the Node.js website. You’ll see some executable files and the node_modules folder:

  • node.exe: This file starts a Node.js JavaScript engine. If you pass in a JavaScript file location, Node.js executes that script. If no target avaScript file is specified, a script prompt allows you to execute JavaScript code directly from the console.
  • npm.cmd: You use this command to manage the Node.js packages.
  • node_modules: This folder contains the installed Node.js packages. These packages act as libraries that extend the capabilities of Node.js.

Verifying Node.js Executables

Open a command prompt and execute the following command to bring up a Node.js VM:


Then, you are now at the Node.js prompt. Execute the following to write “Hello World” to the screen:

console.log(”Hello World);

After you see “Hello World” written to the console screen, exit the console by pressing Ctrl+C in Windows.

Next, verify that the npm command is working by executing the following command at the command prompt:

npm version

What you see may be similar to the following:

{npm: ‘3.3.12’,
area: ‘1.10.1-DEV’,
http_parser: ‘2.6.0’,
icu: ‘56.1’,
modules: ’47’,
node: ‘5.2.0’,
openssl: ‘1.0.2e’,
uv: ‘1.7.5’,
v8: ‘’,
zlib: ‘1.2.8’ }

Selecting a Node.js IDE

For example, Eclipse has some great Node.js plugins, and the WebStrom IDE by IntelliJ has some good features for Node.js built in.

You can use any editor you want to generate your Node.js web applications. In reality, all you need i a decent text editor. Almost all he code you will be generating will be .js, .json, .html, and .css. So pick the editor you feel the most comfortable using to write those types of files.

Working with Node.js Packages

One of the most powerful features of the Node.js framework is the ability to easily extend it with additional Node Packaged Modules (NPMs), using the Node Package manager (NPM).

A Node Packaged Module is a packaged library that can easily be shared, reused, and installed in different projects. There are many different modules available for a variety of purposes. For example, the Mongoose module provides an ODM for MongoDB, Express extends Node’s HTTP capabilities, and so on.

Node.js modules are created by various third-party organizations to provide important features that Node.js lacks out of the box. This community of contributors is very active in ading and updating modules.

Each Node Packaged Module includes a package.json file that defines the packages. The package.json file includes informational metadata such as the name, version, author, and contributors, as well as control metadata such as dependencies and other requirements that the Node Package manager will use when performing actions such as installation and publishing.

Using the Node Package Manager

The simplest way to really explain the Node Package Manager is to list some of the command-line options and what they do. The following is a list of the Node Package Manager commands (with express as the package, where appropriate).

search: Finds module packages in the repository.

npm search express

install: Installs a package either using a package.json file from the repository or from a local location.

npm install

npm install express

npm install express@0.1.1

npm install ../tModule.tgz

install -g: Installs a package in a globally accessible location.

npm install express -g

remove: Removes a module.

npm remove express

pack: Packages the module defined by the package.json file into a .tgz file.

npm pack

view: Displays module details.

npm view express

publish: Publishes the module defined by a package.json file to the registry

npm publish

unpublish: Unpublishes a module you have published

npm unpublish myModule

owner: Allows you to add, remove, and list owners of a package in the repository

npm add bdayley myModule

npm rm bdayley myModule

npm ls myModule

Using package.json

All Node modules must include a package.json file in their root directory. package.json is a simple JSON text file that defines a module, including dependencies. The package.json file can contain a number of different directives to tell the Node Package Manager how to handle the module.

Here is an example of a package.json file with a name, version, description, and dependencies:

“name”: “my_module”,
“version”: “0.1.0”,
“description”: “a simple node.js module”,
“dependencies”: {“express”: “latest”}

The only required directives in the package.json file are name and version; the rest depend n what you would like to include. The following describes the most common directives.

name: Unique name of package.

“name”: “camelot”

preferGlobal: Indicator that the module prefers to be installed globally.

“preferGlobal”: true

version: Version of the module.

“version”: 0.0.1

author: Author of the project.

“author”: “”

description: Text description of module.

“description”: “a silly place”

contributors: Additional contributors to the module.

“contributors”: {{“name”: “gwen”, “email”: “”}}

bin: Binary to be installed with the project.

“bin”: {“excalibur”: “./bin/excalibur”}

scripts: Parameters tat execute console apps when launching node.

“scripts”: {“start”: “node./bin/excalibur”, “test”: “echo testing”}

main: The main entry point for the app which can be a binary or a .js file.

“main”: “./bin/excalibur”

repository: The repository type and location of the package.

“repository”: {“type”: “git”, “location”: “http://???.com/c.git”}

keywords: Keywords that show up in the npm search

“keywords”: [“swallow”, “unladen”]

dependencies: Modules and versions this module depends on; you can use the * and x wildcards (通配符、万用字符).

“dependencies”: {“express”: “latest”, “connect”: “2.x.x”, “cookies”: “*”}

engines: The version of node this package works with.

“engines”: {“node”: “>=0.6”}

A great way to use package.json files is to automatically download and install the dependencies for your Node.js app. All you need to do is create a package.json file in the root of your project code and add necessary dependencies to it. For example, the following package.json file requires the express module as a dependency:

“name”: “my_module”,
“version”: “0.1.0”,
“dependencies”: {“express”: “latest”}

Then you run the following command from the root of your package, and the express module is automatically installed:

npm install

Notice that no module is specified in the command npm install. That is because npm looks for a package.json file by default. Later, as you need additional modules, all you need to do is add those to the dependencies directive and then run npm install again.

Reference: Node.js, MongoDB, and AngularJS Web Development.

赞(0) 打赏
文章名称:《Install Node.js》

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址