


Node.js installation tutorial and detailed explanation of using NPM package manager_node.js
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:
$ 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:
$ 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:
$ tar xfz node-v0.6.12.tar.gz
Then enter the source code directory:
Configuration:
If everything goes well, you will see a success message:
Then you can start compiling:
$ make
After compilation is completed, the following prompt will appear:
build' finished successfully (0.734s)
Install Node
When the build is complete, install Node using the following command:
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:
$ 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:
$ 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:
> 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:
console.log('Hello World!');
Then call the Node executable file with the file name of this script as the first argument:
$ 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.
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:
$ sudo npm install –g sax
Then when you need the sax module in your Node script, use the following statement to load it:
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:
$ 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:
$ 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:
$ 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:
$ 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:
Or, install the latest version with a version number less than 0.3:
$ npm install sax@”<0.3”
You can even specify a version range:
$ npm install sax@">=0.1.0 <0.3.1"
Uninstall module
Use the following command to uninstall a local module:
$ npm uninstall
If you want to uninstall a global module, just add the -g flag:
$ npm uninstall -g
Update module
Use the following command to update the local module:
$ 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:
$ 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:
$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:
$ 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:
{
"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:
$ 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:
$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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Use the pip command to easily install OpenCV tutorial, which requires specific code examples. OpenCV (OpenSource Computer Vision Library) is an open source computer vision library. It contains a large number of computer vision algorithms and functions, which can help developers quickly build image and video processing related applications. Before using OpenCV, we need to install it first. Fortunately, Python provides a powerful tool pip to manage third-party libraries

PyCharm installation tutorial: Easily learn how to install Selenium, specific code examples are needed. As Python developers, we often need to use various third-party libraries and tools to complete project development. Among them, Selenium is a very commonly used library for automated testing and UI testing of web applications. As an integrated development environment (IDE) for Python development, PyCharm provides us with a convenient and fast way to develop Python code, so how

Quick Start with PyCharm Community Edition: Detailed Installation Tutorial Full Analysis Introduction: PyCharm is a powerful Python integrated development environment (IDE) that provides a comprehensive set of tools to help developers write Python code more efficiently. This article will introduce in detail how to install PyCharm Community Edition and provide specific code examples to help beginners get started quickly. Step 1: Download and install PyCharm Community Edition To use PyCharm, you first need to download it from its official website

Recently, many friends have asked me how to install solidworks2016. Next, let us learn the installation tutorial of solidworks2016. I hope it can help everyone. 1. First, exit the anti-virus software and make sure to disconnect from the network (as shown in the picture). 2. Then right-click the installation package and select to extract to the SW2016 installation package (as shown in the picture). 3. Double-click to enter the decompressed folder. Right-click setup.exe and click Run as administrator (as shown in the picture). 4. Then click OK (as shown in the picture). 5. Then check [Single-machine installation (on this computer)] and click [Next] (as shown in the picture). 6. Then enter the serial number and click [Next] (as shown in the picture). 7.

Essential for Python novices: Simple and easy-to-understand pip installation tutorial Introduction: In Python programming, installing external libraries is a very important step. As the officially recommended package management tool for Python, pip is easy to understand and powerful, making it one of the essential skills for Python novices. This article will introduce you to the pip installation method and specific code examples to help you get started easily. 1. Installation of pip Before you start using pip, you need to install it first. Here is how to install pip: First,

Simple pandas installation tutorial: Detailed guidance on how to install pandas on different operating systems, specific code examples are required. As the demand for data processing and analysis continues to increase, pandas has become one of the preferred tools for many data scientists and analysts. pandas is a powerful data processing and analysis library that can easily process and analyze large amounts of structured data. This article will detail how to install pandas on different operating systems and provide specific code examples. Install on Windows operating system

Friends, do you know how to install NeXus desktop beautification? Today I will explain the installation tutorial of NeXus desktop beautification. If you are interested, come and take a look with me. I hope it can help you. 1. Download the latest version of the Nexus desktop beautification plug-in software package from this site (as shown in the picture). 2. Unzip the Nexus desktop beautification plug-in software and run the file (as shown in the picture). 3. Double-click to open and enter the Nexus desktop beautification plug-in software interface. Please read the installation license agreement below carefully to see if you accept all the terms of the above license agreement. Click I agree and click Next (as shown in the picture). 4. Select the destination location. The software will be installed in the folder listed below. To select a different location and create a new path, click Next

Pygame installation tutorial: Quickly master the basics of game development, specific code examples are required Introduction: In the field of game development, Pygame is a very popular Python library. It provides developers with rich features and easy-to-use interfaces, allowing them to quickly develop high-quality games. This article will introduce you in detail how to install Pygame and provide some specific code examples to help you quickly master the basics of game development. 1. Installation of Pygame Install Python and start installing Pyga
