Home > Web Front-end > JS Tutorial > body text

Node.js installation tutorial and detailed explanation of using NPM package manager_node.js

WBOY
Release: 2016-05-16 16:39:36
Original
1859 people have browsed it

At the JSConf conference in 2009, a young programmer named Ryan Dahl showed people a project he was working on, a JavaScript running platform based on the Google V8 engine, which provides a set of event loops and low IO Application Programming Interface (API). Unlike other server-side platforms, JavaScript is inherently event-driven IO, and this project greatly reduces the complexity of writing event-driven applications, so it quickly grew in popularity at an incredible speed and was applied to actual projects. middle. (Jack: This translation is not very reliable. The original text: This project was not like other server-side JavaScript platforms where all the I/O primitives were event-driven and there was no way around it.)

This project is named Node.js, and developers are accustomed to calling it Node. Node provides a purely event-driven, non-blocking toolkit for building high-concurrency applications.

Note: Node allows you to easily build fast and scalable network services.

Since being introduced by Ryan Dahl, Node has received widespread attention in the industry. They have begun using Node to deploy fast and scalable network services. Node is so attractive.

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

On the other hand, because Node is petite and cute, Node's core function set is very small, and the existing APIs are very refined, minimizing complexity for developers. When you want to build some more complex applications, you just have to pick and install some third-party modules you like.

There is another reason why Node is so attractive. It is easy to get started. You can download, install and run it in a few minutes.

Usually just follow the steps on the official website (http://nodejs.org) to install Node. It supports Windows, Linux, Macintosh and Solaris.

Install Node on Windows

Node supports Windows starting from version 0.6.0. To install Node on Windows, just download node-v*.msi from Http://nodejs.org/#download, and then double-click to run That's it, and then you may encounter a security dialog box similar to Figure 1-1.

Figure 1-1

Click the "Run" button. After the download is completed, another security dialog box (Figure 1-2) will appear to remind you whether you are sure to operate.

Figure 1-2

If you agree, the Node installation wizard will appear (Figure 1-3). Click Next and Node will start the installation. It will be installed in a short time! See Figure 1-4

Figure 1-3

Figure 1-4

Installation under Mac OS X

If you use Mac OS X, you can use the installation wizard to install Node. First, download node-v*.pkg from http://nodejs.org/#download. Double-click to run, and you will see the first dialog box of the installation wizard, see Figure 1-5

Figure 1-5

Click "Continue" to install, and then the wizard will ask you to enter the password of the system user. After confirmation, the installation will begin. After another short while, Node is installed again! See Figure 1-6

Figure 1-6

Install from 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 it, install and run Node.

Note: Node relies on several third-party code libraries, but luckily most of them are already included in the Node distribution package. If you build from source, you need the following two things:

1.python (version 2.4 or above) - The build tools released with Node require a python environment to run
2.libssl-dev - If you plan to use SSL/TLS encryption, you need to install this. libssl is a class library used by the openssl tool. Under Linux and UNIX systems, you can usually install it using the system's package manager. libssl is pre-installed under Mac OS X, so if you use a Mac OS X system, you usually do not need to install libssl.

Select Node version

There are usually two different Node versions available for download on the official website nodejs.org: the stable version and the latest version.

For Node, the smallest digit of the version number represents the stability of this version. Stable versions use even numbers (such as 0.2, 0.4, 0.6), and unstable versions use odd numbers (0.1, 0.3, 0.5, 0.7).

The non-stable version is not only functionally unstable, but the API may also change in subsequent versions. The APIs released in the stable version will not be modified. For each stable branch, the new patch not only contains bug fixes, but also includes API modifications in non-stable versions.

Unless you want to test new features in the latest unstable version, you should choose the latest stable version. Unstable releases are like battlefields for the Node core team to test new features.

Although more and more projects and companies are successfully using Node in their products (shown on the official website homepage), you may have to learn to tolerate the changes in the API from the unstable version to the stable version. Of course, this is 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 to find the corresponding tar package, and then copy the download link. If you are using a UNIX system, your system Wget may already be installed, which means you can download it with a shell command:

Copy code The code is as follows:

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

If you don’t have wget installed, you may need to use curl:

Copy code The code is as follows:

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

If you do not have these two tools installed, you have to find other ways to download the tar package to your local directory - such as through a browser or through a local network.

(The examples in this book use the latest stable version at the time of writing: 0.6.12)

Build Node

Now that we have the source code, we can use it to build the Node executable. First, you need to unzip the tarball package downloaded earlier:

Copy code The code is as follows:

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

Then enter the source code directory:

Copy code The code is as follows:
$ cd node-v0.6.12

Configuration:
Copy code The code is as follows:
$ ./configure

If everything goes well, you will see a success message:
Copy code The code is as follows:
'configure' finished successfully (9.278s)

Then you can start compiling:
Copy code The code is as follows:

$ make

After compilation is completed, the following prompt will appear:
Copy code The code is as follows:

build' finished successfully (0.734s)

Install Node

When the build is complete, install Node using the following command:

Copy code The code is as follows:
$ make install

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

If you encounter permission issues, add sudo in front of the command and execute it as the root user:

Copy code The code is as follows:

$ sudo make install

Run Node

Now you can run Node. You can simply experience Node's command-line interface (CLI: command-line interface) first. You only need to call the Node executable file:

Copy code The code is as follows:

$ node

This operation will start Node's command line interactive interface and wait for your input. Enter the following command to let Node do something:
Copy code The code is 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 contain the following content:
Copy code The code is as follows:

console.log('Hello World!');

Then call the Node executable file with the file name of this script as the first argument:
Copy code The code is as follows:

$ node hello_world.js

Hello World!

Finally, use Ctrl D or Ctrl C to exit the Node command line interactive interface.

Preparing and using the Node package manager

So far, you can only use the language features and core functions of Node itself. This is why most programming platforms have a system for downloading, installing, and managing third-party modules. In Node, we use 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 registry service that contains all packages published by everyone, and provides a command line tool to download, install and manage these packages. You can follow Node's package format standards to formulate your package or other third-party packages that your application depends on.

Although you don’t need to know NPM to start using Node, you must learn it if you want to use third-party packages, because Node itself only provides some low-level APIs. Using third-party modules can greatly reduce development complexity. You don’t have to code anything yourself. NPM allows you to download and use modules in a sandbox, so you can experiment with things that interest you without worrying about polluting the global package environment.

NPM and Node used to need to be installed independently. From version 0.6.0 onwards, NPM has been included in the Node installation package.

Use NPM to install, upgrade and uninstall packages

NPM is very powerful and you can use it in many ways. Its code base centrally manages all public modules. You can access it through http://search.npmjs.org. Authors of Node open source modules can publish their modules to NPM, and others can download and install the module using the module name in the package installation description.

This part of the content includes some common operations for installing and deleting packages. Knowing these is enough for you to start managing the third-party packages that your own applications depend on. However, you still need to first understand that these commands are "global" and "local" mode, and how they affect dependencies and module lookup.

Global and local modes for NPM modules

NPM operates in two main modes: global and local. These two modes will affect the directory structure in which packages are stored and the order in which Node loads packages.

Local mode is the default operating mode of NPM. In this mode, NPM only works in the working directory and will not cause system-wide modifications. This mode allows you to install and test modules under a certain Node program. It will not affect other Node programs on your computer.

Global mode is suitable for common modules that will be used by many programs and are always loaded globally, such as command line tools, which are not directly used by applications.

If you don’t know which mode to install a module in, you should use local mode. If a module author requires a module to be installed globally, he will usually indicate this in the installation instructions.

Global Mode

If you use the default directory when installing Node, in global mode, NPM will install the package to /usr/local/lib/node_modules. If you execute the following command, NPM will search for and download the latest version named sax and install it in the /usr/local/lib/node_modules/sax directory.

Copy code The code is as follows:
$ npm install –g sax

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

$ sudo npm install –g sax

Then when you need the sax module in your Node script, use the following statement to load it:
Copy code The code is as follows:

var sax = require(‘sax’);

If you have not installed sax in local mode in the application directory, Node will look for 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 flag after the NPM command to enable global mode.

Local Mode

Local mode is the default recommended mode for the Node package dependency mechanism. In this mode, everything installed by NPM is in the current working directory (the root directory is no exception), without affecting any global settings. This mechanism allows you to set the application's dependent modules and their versions one by one without worrying about polluting the global module space. This means you can have two applications that depend on different versions of the same module without them conflicting.

In this mode, NPM uses the node_modules directory in the current working directory to store modules. For example, if your current working directory is /home/user/apps/my_app, NPM will use /home/user/apps/my_app/node_modules to store all local modules. This means that if you use the module name to reference the module in your code, Node will first search in the local node_modules directory. If it is not found, it will search the global node_modules directory. The priority of local modules is always higher than that of global modules. .

Install the module

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

Copy code The code is as follows:

$ npm install

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

Copy code The code is as follows:

$ npm install sax

This operation will create the node_modules subdirectory in the current directory (if it does not exist), and then install the sax module below.

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

Copy code The code is as follows:

$ npm install @

Just replace the placeholder in the command with the specified version number. For example, to download version 0.2.5 of the sax module, you only need to run:
Copy code The code is as follows:

$ npm install sax@0.2.5

The placeholder can also be replaced with a version range. For example, to install the latest version of the 0.2 branch of the sax module, you can run:
Copy code The code is as follows:

$ npm Viagra Canada Online install sax@0.2.x

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

$ npm install sax@”<0.3”

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

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

Uninstall module

Use the following command to uninstall a local module:

Copy code The code is as follows:

$ npm uninstall

If you want to uninstall a global module, just add the -g flag:
Copy code The code is as follows:

$ npm uninstall -g

Update module

Use the following command to update the local module:

Copy code The code is as follows:

$ npm update

This command will try to get the latest version of the module package and update the local version. If it is not installed locally, it will be installed. If the global environment needs to be updated, the -g flag needs to be added:
Copy code The code is as follows:

$ npm update –g

Use executable files

Modules can contain one or more executable files. If you use the default directory settings to install a global module, NPM will install the executable files to the /usr/local/bin directory, which is usually also set to Part of the system PATH environment variable. If you install this module locally, NPM will place all executable files in the ./node_modules/.bin directory.

Handling dependencies

NPM not only installs the module packages you need, but also installs other modules that these modules depend on. For example, if you need to install module A, and A depends on modules B and C, then when you install A, B and C will also be installed in the ./node_modules/A/node_modules directory.

For example, you install a module called nano locally using the following command:

Copy code The code is as follows:

$npm install nano

The output of NPM will look like this:

This tells you that the nano module depends on the underscore and request modules, and also indicates the installed version. If you check the ./node_modules/nano/node_modules directory now, you will find that these two modules have been installed:

Copy code The code is as follows:

$ ls node_modules/nano/node_modules

request underscore

Use package.json file to define dependencies

When you start writing an application, you can create a package.json file in the application root directory to define the application's metadata, such as the application's name, author, code library address, contact information, etc. External modules that the program depends on are also specified in this file.

If you don’t plan to publish the program to NPM, you don’t need to create this file. However, even if your program is private, this file is actually useful. It can tell NPM the dependencies of the application. (Translator's Note: For example, if you copy the project source code from the development environment to the production environment, you can install all dependent packages at once by calling npm install. npm will automatically complete the download and installation of dependent modules through the dependencies specified in package.json. , you don’t have to do it one by one, details will be introduced later)

package.json is a JSON format file that contains a series of attributes. However, if it is just to illustrate the dependencies of the program, only one dependencies attribute will be used. For example, an application called MyApp relies on the sax, nano and request modules. You only need to create a package.json like this:

Copy code The code is as follows:

{

"name" : "MyApp",

"version" : "1.0.0",

"dependencies" : {

"sax" : "0.3.x",

"nano" : "*",

"request" : ">0.2.0"

           }

}

You specified the MyApp application, which depends on version 0.3 of sax, any version of nano, and the request module version higher than 0.2.0.

Note: You may find that NPM does not work if you specify the name and version fields. This only happens with older versions of NPM because originally NPM was used for public modules, not private programs.

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

Copy code The code is as follows:

$ npm install

In this way, NPM will analyze the dependencies and your local node_modules directory, and automatically download and install missing modules.

You can also update all local modules to the latest version that matches your defined dependency settings by running the following command:

Copy code The code is as follows:

$npm update

In fact, you can just use the update method, because it will let NPM automatically obtain the missing dependency modules.

Summary

In this chapter, you learned how to install Node and the Node Package Manager (NPM). Now you can use NPM to install, uninstall, and delete any third-party modules. You also learned how to use NPM to manage application dependencies with the package.json file. item.

Now that you have installed Node and NPM, you can try it out. However, first you need to know some relevant knowledge about Node and event-driven, which will be introduced in the next chapter.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!