Node.js installation Tutorials and the NPM Package Manager use detailed _node.js

Source: Internet
Author: User
Tags curl list of attributes install node

At the Jsconf conference in 2009, a young programmer named Ryan Dahl showed people a project he was working on, a JavaScript platform based on the Google V8 engine, which provided a set of event loops and low IO application programming interfaces (APIs). Unlike other service-side platforms, JavaScript is inherently event-driven IO, which dramatically reduces the complexity of writing event-driven applications, so it quickly becomes popular and applied to real projects at incredible speed. (Jack: This translation is not very reliable, original: This project is not like Server-side JavaScript platforms where all I/O primitives were even T-driven and there was no way around it.)

The project is named Node.js, and developers are accustomed to calling it Node,node to provide a set of purely event-driven, non-blocking toolkits to build high concurrent applications.

Note: node allows you to simply build fast, scalable network services.

Since being introduced by Ryan Dahl, node has received extensive attention from the industry. They have started deploying fast and scalable network services with node. Node is really fascinating.

On the one hand because Javascript,javascript is the most widely used programming language on the planet, most web programmers use JavaScript on the browser side, and the server side is a natural extension of it.

On the other hand because node is petite, node's core set of functions is small, and the existing APIs are very refined, minimizing complexity for developers. When you want to build some more complex applications, you can just pick and install some of your favorite third-party modules.

There's also a reason for node to be so appealing that it's easy to get started and you can download and install it and run it in a few minutes.

node is usually installed according to the steps on the official website (http://nodejs.org), which supports Windows,linux,macintosh and Solaris.

To install node on Windows

Node supports Windows from version 0.6.0, to install node on Windows, use only from Http://nodejs.org/#download下载node-v*.msi, and then double-click to run it. You may then encounter a security dialog box similar to Figure 1-1.

Figure 1-1

Click the "Run" button, and when the download is complete, another Security dialog box appears (Figure 1-2) to remind you if you are sure of the operation.

Figure 1-2

If you agree, the Node Installation Wizard will appear (Figure 1-3), click Next node will begin installation, wait a little while the installation is finished! See Figure 1-4

Figure 1-3

Figure 1-4

Install under Mac OS X

If you use Mac OS X, you can use the Setup Wizard to install node, first, go to http://nodejs.org/#download下载node-v*.pkg, after downloading, double-click to run, you will see the Installation Wizard's first dialog box, see Figure 1-5

Figure 1-5

Click "Continue" installation, then the wizard will ask you to enter the system user's password, after the installation will be determined to start, is a little while, node and installed! See Figure 1-6

Figure 1-6

Install with source code

If you use a UNIX system, you can install it by compiling the source code. First, you need to select the node version you want to install, then download the corresponding source code and build, install and run node.

Note: node relies on several Third-party code libraries, but fortunately most of them are already included in the node release package, and if you start building from the source, you need two things below:

1.python (more than 2.4 versions)--The build tools released with node require a Python environment to run
2.libssl-dev--If you're going to use SSL/TLS encryption, you need to install this, LIBSSL is the class library used by the OpenSSL tool, and you can usually install it using the system's Package manager under Linux and UNIX systems. Libssl is preinstalled under Mac OS X, so if you use a Mac OS X system you usually don't have to install LIBSSL.

Select node version

The official website nodejs.org usually has two different node versions to download: Stable and latest.

For node, the smallest bit of version number represents the stability of this version, the stable version uses even numbers (such as 0.2,0.4,0.6), and the unstable version uses odd (0.1,0.3,0.5,0.7).

Non-stable versions are not only functionally unstable, but the APIs are also likely to change in subsequent versions, and the published APIs in the stable version are not modified. For each stable branch, the new patches include not only bug fixes, but also changes to the APIs in the unstable version.

Unless you want to test the new features in the latest unstable version, you should choose the latest stable version. The unstable version is like a battlefield for the node core team to test the new features.

Although more and more projects and companies have successfully used node in their products (home page of the website), you may have to learn to endure the changes in the API from the unstable version to the stable version, which is, of course, the price of learning a new technology.

Download Node source code

Now you know which version to download, then go to the official website http://nodejs.org find the corresponding tar package, and then copy the download link, if you use the UNIX system, your system may have installed the wget, which means you can download with a shell command:

Copy Code code as follows:

$ wget http://nodejs.org/dist/v0.6.1/node-v0.6.12.tar.gz

If you do not install wget, you may need to use curl:

Copy Code code as follows:

$ curl–o http://nodejs.org/dist/v0.6.1/node-v0.6.12.tar.gz

If neither of your tools is installed, you'll have to find another way to download the TAR packets to your local directory--for example, through a browser or through a local network.

(Examples of this book use the latest stable version of writing: 0.6.12)

Building node

Now that we have the source code, we can use it to build the node executable file. First, you need to extract the previously downloaded tar packets:

Copy Code code as follows:

$ tar xfz node-v0.6.12.tar.gz

Then enter the source directory:

Copy Code code as follows:
$ CD node-v0.6.12

Configuration:
Copy Code code as follows:
$./configure

If you're successful, you'll see a hint of success:
Copy Code code as follows:
' Configure ' finished successfully (9.278s)

Then you can start compiling:
Copy Code code as follows:

$ make

After compiling, you will be prompted as follows:
Copy Code code as follows:

Build ' finished successfully (0.734S)

Installing node

When built, use the following command to install node:

Copy Code code as follows:
$ make Install

This operation will copy the node executable to the/user/local/bin/node

If you encounter a permission problem, precede the command with Sudo, and execute it as the root user:

Copy Code code as follows:

$ sudo make install

Run node

Now that you're ready to run node, you can simply experience node's command-line interface (Cli:command-line interface) and just call the node executable on the line:

Copy Code code as follows:

$ node

This operation starts node's command-line interface and waits for your input, and enter the following command to let node do something about it:
Copy Code code as follows:

> Console.log (' Hello world! ');

Hello world!

> Undefined

You can also run a JavaScript script file, for example, you create a file called Hello_world.js and include the following:
Copy Code code as follows:

Console.log (' Hello world! ');

The node executable is then invoked with the filename of the script as the first parameter parameter:
Copy Code code as follows:

$ node Hello_world.js

Hello world!

Finally, use Ctrl+d or CTRL + C to exit the node command-line interface.

Preparing and using the node Package Manager

So far, you can only use the language features and core functions of node itself, which is why most program platforms have a system for downloading, installing, and managing Third-party modules, and in node we use the Node Package Manager (Npm:node Package Manager)

NPM consists of three parts: a code base for storing Third-party packages, a mechanism for managing locally installed packages, and a standard for defining package dependencies. NPM provides a public registration service that contains all the packages that are published and provides a command-line tool for downloading, installing, and managing these packages. You can follow node's package format criteria to make your package or apply other Third-party packages that you need to rely on.

Although you can start using node without knowing NPM, you have to learn it if you want to use a Third-party package, because node itself provides only low-level APIs, and using Third-party modules can dramatically reduce development complexity without having to encode everything yourself. NPM allows you to download and use modules in a sandbox, and you can experiment with what you're interested in without worrying about polluting the global package environment.

NPM and node previously needed to be installed separately, and NPM has been included in the node installation package since version 0.6.0.

Use NPM to install, upgrade, and uninstall packages

NPM is very powerful and you can use it in a number of ways, and its code base centrally manages all of the public modules, and you can access it through http://search.npmjs.org. The author of the node open source module can publish its own module to NPM, and others can download it with the module name in the package installation description and install the module.

This section contains some common operations for installing and deleting packages, knowing this is enough to get you started managing the Third-party packages on which your application relies, however, you first need to understand how these commands differ in "global" and "local" mode, and how they affect dependencies and module lookups.

Global and local mode for NPM module

There are two main modes of NPM operations: Global and Local. Both modes affect the directory structure that the package holds, and the order in which node loads the package.

Local mode is the default mode of operation for NPM, in which NPM works only in the working directory and does not cause system-wide modifications, which allows you to install and test the modules in a node program without affecting other node programs on your computer.

Global schemas are suitable for public modules that will be used by many programs and are always loaded globally, such as command-line tools that are not used directly by the application.

If you don't know which mode a module should be installed in, then you should use local mode. If a module's author needs a module to be installed globally, he will usually point it out in the installation instructions.

Global mode

If you use the default directory when you install node, NPM installs the package to/usr/local/lib/node_modules under Global mode. If you execute the following command, NPM will search for and download the latest version called sax and install it into the/usr/local/lib/node_modules/sax directory.

Copy Code code as follows:
$ NPM install–g sax

Note: If your current shell user does not have sufficient privileges, you will need to log in using the root user or use sudo to execute the command:
Copy Code code as follows:

$ sudo npm install–g sax

Then, when you need a sax module in your node script, use the following statement to load:
Copy Code code as follows:

var sax = require (' sax ');

If you have not installed Sax,node in the application directory in local mode, you will find a module named sax in the previous installation directory, otherwise the local version will be loaded first.

The default mode is local mode, so you need to add the-G tag after the NPM command to enable the global mode.

Local mode

The local mode is the default recommended mode for the node package dependency mechanism, in which everything that NPM installs is in the current working directory (the root directory is no exception) without affecting any global settings. This mechanism allows you to set up your application dependencies and their versions one at a a while without worrying about polluting the global module space. This means that you can have two applications that rely on different versions of the same module, but they don't create conflicts.

In this mode, NPM uses the Node_modules directory in the current working directory to store the module, such as your current working directory is/HOME/USER/APPS/MY_APP,NPM will be used/home/user/apps/my_app/node_ Modules to store all the local modules. This means that if you use the module name to refer to the module in your code, node will first look in the local node_modules directory, and if it is not found, it will search the global Node_modules directory, and the local module priority is always higher than the global module.

Installing modules

Use the following command to install the latest version of a module:

Copy Code code as follows:

$ NPM Install <package name>

For example, to download and install the latest version of a module named sax, you first need to set your application's root directory to the current directory, and then enter:

Copy Code code as follows:

$ NPM Install sax

This operation creates a Node_modules subdirectory (if it does not exist) in the current directory, and then installs the Sax module below.

You can also choose to install a specific version by using the following command:

Copy Code code as follows:

$ npm Install <package name>@<version spec>

Replace the placeholder in the command with the specified version number, for example, to download the 0.2.5 version of the Sax module, you only need to run:
Copy Code code as follows:

$ NPM Install sax@0.2.5

Placeholders can also be replaced with a version range, for example, to install the latest version of the Sax module 0.2 branch, which can be run:
Copy Code code as follows:

$ npm <a href= "http://www.trevorturnbull.com/" rel= "external nofollow" >viagra Canada online</a> Install sax @0.2.x

Or, install the latest version with a version number less than 0.3:
Copy Code code as follows:

$ npm Install sax@ "<0.3"

You can even specify a version range:
Copy Code code as follows:

$ npm Install sax@ ">=0.1.0 <0.3.1"

Uninstall module

Use the following command to uninstall a local module:

Copy Code code as follows:

$ npm Uninstall <package name>

If you are uninstalling a global module, add the-G tag to:
Copy Code code as follows:

$ NPM uninstall-g <package name>

Update module

Use the following command to update the local module:

Copy Code code as follows:

$ NPM Update <package name>

This command will attempt to get the latest version of the module package and update the local version, if not installed locally, it will be installed, if you need to update the global environment, you need to add a-G tag:
Copy Code code as follows:

$ NPM update–g <package name>

Working with executable files

A module can contain one or more executable files, and if you use the default directory settings to install a global module, NPM installs the executable file into the/usr/local/bin directory, which is usually set as part of the system PATH environment variable. If you install this module locally, NPM will put all executables into the./node_modules/.bin directory.

Handling dependencies

NPM installs not only the module packages you need, but also the other modules on which they depend, for example, if you need to install module A and a is dependent on modules B and C, then B and C will be installed in the./node_modules/a/node_modules directory when you install a.

For example, you install a module called Nano locally with the following command:

Copy Code code as follows:

$NPM Install Nano

NPM's output would look something like this:

This tells you that the Nano module relies on underscore and request two modules, and also points to the installed version. If you look at the./node_modules/nano/node_modules directory now, you will find that these two modules have been installed:

Copy Code code as follows:

$ ls Node_modules/nano/node_modules

Request underscore

Defining dependencies using the Package.json file

When you start writing an application, you can create an Package.json file in the application root to define the application's metadata, such as the name of the application, the author, the code base address, the contact information, and so on. The external modules that the program relies on are also specified in this file.

If you don't want to publish the program to NPM, you don't have to build the file, but even if your program is private, the file is actually useful, and it tells NPM about the application's dependencies. (Translator Note: For example, you copy project source code from the development environment to the production environment, you can install all the dependent packages at once by calling NPM install. NPM will automatically complete the download installation of the dependent modules through the dependencies specified in the Package.json, without having to operate them one at a while.

Package.json is a JSON-formatted file that contains a list of attributes, but only one dependencies attribute is used to illustrate the dependencies of the program. For example, an application called MyApp relies on the Sax,nano and request modules and only needs to establish such a Package.json:

Copy Code code as follows:

{

"Name": "MyApp",

"Version": "1.0.0",

"Dependencies": {

"Sax": "0.3.x",

"Nano": "*",

"Request": ">0.2.0"

}

}

You specify a MyApp application that relies on the 0.3 version of Sax, any version of the Nano, and the request module with a version higher than 0.2.0.

Note: You may find that if you specify the name and version fields, NPM will not work, and this will only happen in the old version of NPM, since NPM was originally used for public modules, not private programs.

Then, in the root directory of the application, execute:

Copy Code code as follows:

$ NPM Install

In this way, NPM analyzes dependencies and your local node_modules directory, and automatically downloads and installs missing modules.

You can also update all local modules to the latest version of your defined dependency settings with the following command:

Copy Code code as follows:

$NPM Update

In fact, you can just use the Update method, because it will allow NPM to automatically get those missing dependency modules.

Summary

This chapter has learned how to install node and Node Package Manager (NPM), and you can now use NPM to install, uninstall, remove any third-party modules, and learn how to work with Package.json files to use NPM to manage application dependencies.

Now that you have node and NPM installed, you can try it out, but first you need to know a bit about node and event-driven knowledge, which will be covered in the next chapter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.