This article mainly introduces how to install and run the Node. js environment. Node is a JavaScript interpreter based on the GoogleChromeV8 engine. For more information, see
Installer
NodeJS provides some installation programs that can be downloaded and installed at nodejs.org.
In Windows, select an installation file with the. msi suffix that matches the system version. In Mac OS X, select the installation file with the. pkg suffix.
Compile and install
There are no ready-made installation programs available in Linux. Although some release versions can be installed using apt-get or other methods, they may not be installed to the latest version. Therefore, NodeJS is usually installed in the following way in Linux.
1. Make sure that the g ++ version is later than 4.6 and the python version is later than 2.6.
2. download the latest NodeJS source code package with the extension tar.gz from nodejs.org and decompress it to a certain location.
3. Go to the decompressed directory and run the following command to compile and install the package.
$ ./configure$ make$ sudo make install
Run
Open the terminal and type node to enter the command interaction mode. You can enter a code statement and execute it immediately and display the result, for example:
$ node> console.log('Hello World!');Hello World!
If you want to run a large piece of code, you can write a JS file before running it. For example, the following is hello. js.
function hello() { console.log('Hello World!');}hello();
After writing the code, enter node hello. js in the terminal to run it. The result is as follows:
$ node hello.jsHello World!
Permission problems
In Linux, the root permission is required to use NodeJS to listen to port 80 or port 443 to provide the HTTP (S) service. There are two ways to do this.
One way is to run NodeJS using the sudo command. For example, the server. js running through the following command has the permission to use port 80 and port 443. This method is generally recommended to ensure that only the root permission is provided for JS scripts that are needed.
$ sudo node server.js
Another method is to use the chmod + s command to make NodeJS always run with the root permission. The specific method is as follows. This method gives any JS script the root permission, which is not safe. Therefore, it is not recommended to use it in a secure system.
$ sudo chown root /usr/local/bin/node$ sudo chmod +s /usr/local/bin/node