Quickly master the installation and operation methods of ES0en. js environment

  • 2020-12-21 17:58:12
  • OfStack

The installation program
NodeJS offers 1 installer that can be downloaded and installed from nodejs.org.

Under Windows system, select the installation file with the.msi suffix matching the system version. Under Mac OS X system, select the.pkg suffix installation file.

Compile the installation
There is no off-the-shelf installer available for Linux systems, and although some distributions can be installed using the apt-ES18en approach, it is not guaranteed to work with the latest version. Therefore, NodeJS is generally installed using the following compilation method under Linux system.

1. Ensure that g++ version under the system is above 4.6 and python version is above 2.6.

2. Download the latest version of the NodeJS source code package with the suffix tar.gz from ES27en.org and unzip to a location.

3. Go to the unzipped directory, compile and install using the following commands.


$ ./configure

$ make

$ sudo make install

run

Open the terminal and enter node into command interaction mode. You can enter 1 code statement and immediately execute it and display the result, for example:


$ node
> console.log('Hello World!');
Hello World!

If you want to run a large chunk of code, you can write an JS file to run it. For example, there is hello.js.


function hello() {
  console.log('Hello World!');
}
hello();

After writing, type node ES50en. js at the terminal to run. The results are as follows:


$ node hello.js
Hello World!

Permission problems
Under Linux system, using NodeJS to listen on port 80 or port 443 to provide HTTP(S) service requires root permissions. There are two ways to do this.

One way is to run NodeJS using the sudo command. For example, server.js, run with the following command, has permission to use ports 80 and 443. This approach is generally recommended to ensure that root permissions are provided only for JS scripts that require them.


$ sudo node server.js

Another option is to use the chmod +s command to have NodeJS always run with root permissions, as follows. Because this approach gives any JS script root permissions and is not secure, it is not recommended for systems where security is a concern.


$ sudo chown root /usr/local/bin/node
$ sudo chmod +s /usr/local/bin/node


Related articles: