@[TOC]

WebRTC source code research (5) Nodejs build environment

Introduction to the Node. Js

Node.js is a JavaScript runtime framework built with Chrome’s V8 JavaScript Engine. Node.js’s use of event-driven, non-blocking I/O modules makes it very lightweight and very efficient. Node.js ecosystem, NPM is the largest open source library ecosystem in the world.

  • What is NPM?

NPM stands for Node Package Manager and is the Package Manager for Nodejs. Node.js itself provides basic modules, but we need to write a lot of code and do more work to develop practical applications using these basic modules. There are already many Node.js libraries or frameworks available on NPM that can help Node.js developers accomplish more complex applications with less code. NPM has more than 300,000 libraries and frameworks, including thousands of Node.js libraries and frameworks, making it the largest open source library ecosystem in the world.

1. Nodejs installation:

There are many ways to install Nodejs. Here are three ways to install Nodejs for MacOS:

  1. PKG installation
  2. Binary installation
  3. The source code to install

You can install it on demand, but in general, the most flexible installation is through source code, we can install custom libraries

1.1 Installation procedure for the PKG

A PKG installation is a dumb-ass installation, as is an exe installation on Windows.

  • Download it from the official websiteNode.js

After a successful download, this file looks like this:

  • Double-click to install

Keep clicking to continue to complete the installation

Click on the install

1.2 Binary library Installation

Relatively speaking, through the binary library installation is more convenient, because under the Ubuntu or Mac, or CenterOS have corresponding installation tools, we only through the installation tools to knock this command, it can be installed directly, this has been a common practice, if this way is not successful, we can also through the second way, Install node.js source code directly and make file to compile.

  1. throughaptThe brew oryum install nodejsTo install thenodejs

Depending on the platform, use apt if you are Ubuntu, Brew if you are MAC, and yum if CentOS.

 apt/brew/yum install nodejs
Copy the code
  1. installednodejsAfter that, we have to go throughaptorbreworyum install npmTo install thenpm.npmWe’re actually developing itnodejsIt will be placed on a common source, so we passnpmThis tool, you can download these dependencies directly down, will be very convenient.
apt/brew/yum install npm
Copy the code
  1. Install the dependency library. First open the console, where I bought a public network service, which you can buy through Ali Cloud and other cloud computing vendors. It is best to be able to buy a domain name, we can do it on their own machine, it doesn’t matter. The installation method is very simple, I have an Ubuntu machine here, I useaptTo perform the installation, there’s a command in here called-cache searchThis is the one that you can look up on this sourcenodejsThe name of the associated version and the associated library.

Let’s just type in

apt-cache search nodejs
Copy the code

And here you can see the sumnodeAll the relevant sources are listed, find the ones that we care aboutnodejs:

The next step is to officially install NodeJS

apt install nodejs
Copy the code

After installation, you should remember that this way it will always have a message, if make a mistake, because it will also tell you what went wrong, you don’t perform this instruction, then I don’t see any information, one thousand wrong, for some reason, there is a dependence on the underlying these libraries, is installed or not, that no success, You can’t find it if you start the service, so many students will encounter this kind of problem.

The next step is to install NPM

NPM can be installed by using the command apt install NPM

This is the first method, but let’s look at the second method, installing NodeJS from source code:

1.2 Installation procedure of source code

In general, there are three steps:

  • Download Nodejs source code
  • Generated Makefile
  • Execute the commandmake -j 4 && sudo make install

Download the source code: Source code installation is more complex than binary installation, but it is flexible, such as you can specify the directory to which the code is installed.

And then you can do some optimizations and what I need and what I don’t need, some tailoring of the source code, and you can do that.

I can also choose which version to install, for example, I want to use the latest nodeJS, or I can use an older nodeJS, depending on my needs.

Visit nodejs. Cn/download /

We can also download it via wget by typing the following command:

Wget -c https://npm.taobao.com/mirrors/node/v10.15.1/node-v10.15.1.tar.gzCopy the code

-c means that when you run this command after a network interruption, it can continue downloading from the breakpoint.

You can download the public decompression command decompression

Decompress the input command:

The tar - ZVXF node - v10.15.1. Tar. GzCopy the code

Go to the node directory:

There is a configure file in this directory

From this script we can generate the makefileConfigure the installation path:./configure --prefix=/usr/local/nodejs

The prefix command specifies which directory you want to install to

At this point, the corresponding Makefile will be generated, and when it’s installed, it will tell you to execute this command, which requires you to install Python, or if you haven’t already

Execute configure to generate the makefile

Generate the corresponding Makefile

Run the script:

This generates a makefile:

We don’t care what is written in the makefile, we just need to compile the makefile. Make, make install

-jIt’s how many threads I’m going to compile at the same time, so I’m going to use 4 threads here, and if I don’t write, it’s going to compile with one thread. If you have a high performance machine, a high number of cores, you can multiply your number of cores by 2

Compile in parallel through these threads

Execute after you compilesudo make installTo install the installed program to the specified directory

After the installation is successful, check the directory:We have another bin below this, and we go to bin nodeandnpmAre already installed in this directory, this way, you can also addnodejsOkay, so that’s installationnodejsIn two ways.

Last but not least, if you are installing from source code, you will need to change some environment variables. Set the environment variables by typing vi ~/.bashrc

The environment variable PATH contains the PATH, and when you type the command, it will find the command.

The vi ~/. Bashrc command is executed

We also need to call source ~/.bashrc to make it work

Let’s search for this PATH

env | grep PATH
Copy the code

We see that the first segment is the node path we specified

Search environment variables

When we hit node it starts executing and then we can look at the node version

You can install the Node on your system using the above method.