The latest Node.js installation detailed tutorial and node.js configuration in 2023

Contents
1. Introduction to Node.js and its download address
1. Differences between compilers, running environments, and development environments
2. Node.js and JavaScript, TypeScript
3. Node.js and browsers
2. Click Downloads on the website and select the corresponding System download Nodejs
3. Node.js installation program installation and installation option settings
4. Node.js common configuration (Configuration, settings)
1. Modify the global installation path and cache path of the npm package
2. Configure the mirror source (mirror Station)
3. Configure package management tools
5. Operation of node.js

The latest Node.js installation detailed tutorial and node.js configuration in 2023

1. Introduction to Node.js and its download address

1. The difference between compiler, running environment and development environment

       For the relationship between the compiler and the development environment, see Chapter 5 of Compiler (Compiler) and C/C++ Compiler Installation (c+ Installation) .

       The runtime environment (runtime environment, run-time system) is a compiler (broadly speaking) role and attaches some other resources to enable the running of the code. The compiler is the core of the runtime environment. Node.js is a running environment. In fact, generally speaking, when we install a certain language, we also obtain the running environment of that language (for example: python installation). The relationship between the runtime environment and the compiler is shown in Figure 1-1.

 Figure 1-1 Runtime environment and compiler

    The IDE (Integrated Development Environment) we mentioned earlier is a development environment (Development Environment), such as: PyCharm, VS, VS Code, etc. The development environment focuses on code development. When running the code, the compiler and other resources will be called. The concept of the development environment is broader and can include the running environment (the running environment can be integrated into the development environment), but generally speaking, the development environment and the running environment It is a calling relationship (the development environment calls the running environment).

       In terms of scope, development environment > runtime environment > compiler, and in terms of core role, compiler (or runtime environment) > development environment.

2、Node.js与JavaScript、TypeScript

      JavaScript ("JS" for short) is a lightweight interpreted programming language that does not require a compiler (actually it can be said to rely on other compilers). It is compiled and compiled with the help of a web browser (Browser) or Node.js. Execution, TypeScript was developed from JavaScript and has a compiler, but the TypeScript compiler is only responsible for compiling TypeScript code into JavaScript code, and then compiles and executes it with the help of a browser or Node.js.

Node.js is a JavaScript runtime environment (Runtime Environment). It can be said that JavaScript runs on the machine with the help of the Node.js compiler.

3. Node.js and browser

      Server refers to a computer system that can provide certain services to other machines in the network. This kind of service is usually called server (Server). The server can be regarded as a high-performance computer. .

       Server (Server) and Client (Client) are relative terms. The server serves the client. The server is a targeted service program that provides resources for the client. Save client data. A Web browser (Browser) is a client (Client) application. Its main function is to present the Web resources requested by the user from the server and display them in the browser window.

       JavaScript can run in the browser, that is, the browser supports the compilation of JavaScript. Therefore, the browser (Browser) can also be regarded as a runtime environment for JavaScript, a running environment on the client. JavaScript uses the browser of compilers running on the machine.

       Node.js is also a JavaScript runtime environment (Runtime Environment), which enables JavaScript to run on the server side. Simply put, Node.js allows JavaScript to run on the machine like python/java without relying on the browser.

       Of course, whether it is a web browser (Browser) or Node.js, the compiler (compiler) is only a core component of them. They also have other functions and their own characteristics, which will not be discussed further here. In addition, unless otherwise specified, the compiler here is a broad concept (that is, the general term for compiling code into machine code).

       The download address of Node.js is shown in Table 1-1.

Table 1-1 Nodejs download URL

Node.js download URL

https://nodejs.org/en

2. Click Downloads on the website and select the corresponding system to download Nodejs.

        Open the download link in Table 1-1 above, and we can see that there are two Downloads locations on the page, as shown in Figure 2-1.

 Figure 2-1 Node.js page

     The ① red arrow in Figure 2-1 points to the download of the two default versions: LTS (Long Time Support, tested, relatively complete and stable version) for 64-bit Windows systems and Current (under internal testing, may not be available) The latest and most complete version of node.js.

       We can click Downloads pointed by the ② red arrow in Figure 2-1, and the page we enter is shown in Figure 2-2, which provides Node.js programs for different operating systems and hardware systems.

 Figure 2-2  Node.js installation programs for different operating systems and hardware systems

       We can choose the corresponding Node.js program under LTS. Figure 2-2 provides Node.js programs suitable for Windows, macOS, and Linux systems. The red box in Figure 2-2 is the default download, and below the red box are the downloads corresponding to specific parameters. For example: the default download of Windows in the red box is actually equivalent to the download of Windows Installer (.msi) → 64-bit under the red box.

        For an explanation of the meaning of the parameter names in Figure 2-2, see 1 , 2 , 3, below .

1、Installer、Binary、Source

       Installer means an installation program, which generates a binary executable file after downloading and installing;

       Binary is a compiled binary executable file, which can usually be used after downloading and decompressing;

       Source is source code (source code), that is, a file that has not been compiled and needs to be compiled before it can be an executable file.

       Note that the executable file mentioned here refers to a file that has been compiled and can be executed (also called a machine code file or a binary file composed of machine code).

       We need to download the executable file Node.js in order to run JavaScript (JS). Therefore, we can choose the program with Installer or Binary in Figure 2-2.

2、.msi、.zip、.pkg、.tar.gz

      .msi is the installation file format of Windows;

      .zip is a compressed file format;

      .pkg is commonly used in the installation file format of macOS systems;

      .tar.gz is a compressed package, similar to .zip compressed package, but the .tar.gz format is suitable for Linux operating systems and MacOSX systems (systems based on UNIX core).

3、32-bit、64-bit(x64)、ARM64、ARMv7、ARMv8

       The parameters of 32-bit, 64-bit (x64), ARM64, ARMv7, and ARMv8 are related to the hardware CPU.

       64-bit (x64) refers to a 64 -bit operating system, based on a 64-bit processor, and the downloaded program is suitable for 64-bit operating systems. Right-click My Computer and left-click Properties in the menu to view the hardware configuration of your computer. The type of your computer's operating system is shown in the red box in Figure 2-3 below.

 Figure 2-3 Computer specifications

       32-bit refers to a 32-bit operating system, based on a 32-bit processor, and the downloaded program is suitable for 32-bit operating systems.

       Arm64 is the processor architecture in the AArch64 state. The downloaded installation program is suitable for the Arm64 system processor launched by ARM, which is based on 64-bit processors.

       ARMv7 is ARM's processor architecture, which contains two instruction sets, 32-bit and 16-bit. The architecture can be understood as a technical specification, and the processor is designed according to the architecture;

       ARMv8 is ARM's processor architecture, including 64-bit and 32-bit instruction sets;

       ARMv7/ARMv8 in Figure 2-2 indicates that the program is suitable for processors of this architecture.

       Taking windows as an example, we can choose the Node.js installer of the 64-bit Windows Installer (.msi).

4、Additional Platforms

       The download page also provides Node.js suitable for other platforms, such as the download corresponding to the options under Additional Platforms in Figure 2-4.

 Figure 2-4 Other platforms and other downloads and installations

Docker Image

       Docker image platform, Docker is a standardized packaging of software and its dependent environment.

Linux on Power LE Systems

       A Linux system running on a hardware system designed with IBM's proprietary Power LE series of chips.

Linux on System z

      Linux system running on IBM's System z series mainframe.

AIX on Power Systems

      A set of UNIX-like operating systems running on minicomputer hardware systems designed with IBM's proprietary Power series chips.

       The right side of the above options in Figure 2-4 is the downloaded file. The next three items are the Node.js program for the IBM server. The red box in Figure 2-4 is the download and installation instructions for other versions.

3. Installation and installation option settings of Node.js installer

       Here, we take the installation of Node.js under the Windows operating system as an example. We download Windows Installer (.msi) → 64-bit corresponding Nodejs, that is, click one of the downloads pointed by the red arrow in Figure 3-1.

 Figure 3-1 Various types of nodejs installation programs

1. Run Node.js Setup

        Run the nodejs installation program downloaded above. The welcome installation interface will appear. Select next to enter the installation interface in Figure 3-2 below.

 Figure 3-2 Node.js installation

      Check I accept the terms in the License Agreement, click Next in Figure 3-2, and enter the Node.js installation location setting interface shown in Figure 3-3.

2. Installation location settings

 Figure 3-3 Node installation location

       You can customize the installation path of Node.js. After the setting is completed, click Next to enter the Node installation option setting interface shown in Figure 3-4.

3. Installation option settings

 Figure 3-5 Node installation option settings

        Node installs the following five basic functions by default.

(1)Node.js runtime

      Node operating environment, which is also the core function of installing Node. Click the + on the left, and one of its subfeatures will appear: event tracing (event tracing, tracking and capturing data of certain events, through which we can do further analysis).

(2)corepack manager

       The universal package manager for Node.js (the universal package manager for Node.js) is also called the manager of package managers. That is, some package managers can use corepack to manage packages and play a universal role.

       The Node.js package manager (package management tool) provides management functions for searching, downloading, installing, and uninstalling Node.js packages. A package can be understood as code that has been written by others. After downloading the package through the package manager and installing it on the computer, we can directly reference the code to avoid reinventing the wheel.

       Commonly used Node.js package managers include npm, yarn, pnpm, cnpm, etc. These package managers can use corepack to exert their functions, which is equivalent to corepack being compatible with npm, yarn, pnpm, cnpm, etc.

        The package manager of Node.js is similar to python's pip, which is the package manager of python.

(3)npm package manager

        The npm (Node Package Manager) package manager is the recommended package manager for the JavaScript runtime environment Node.js.

(4)Online documentation shortcuts

        Online documentation shortcut, create an online documentation shortcut in the Start menu (lower left corner) of the Windows desktop, which can link to Node.js's online documentation and the Node.js website.

(5)Add to PATH

       Added to the Windows environment variables. Click the + on the left, and its subfeatures will appear: Node.js and npm and npm modules (that is, add Node.js, npm, and npm modules to the environment variables. npm modules are modules or libraries installed globally by npm (Can be collectively referred to as packages)).

       An environment variable is an object with a specific name in the operating system that contains information that will be used by one or more applications. When the system is asked to run a program without telling it the full path where the program is located, in addition to looking for the program in the current directory, the system will also go to the path specified in the environment variable path to find runnable variables .

       The system variables in the environment variables are valid for all users, and the user variables are valid for the currently logged-in Windows account. When the system variables cannot be found, they will be searched in the current user variables. In addition, environment variable names are not case-sensitive, for example: PATH and path, the two names are the same.

        When we click the icon of one of the above functions , the menu of the installation settings pops up, with the following options to choose from:

Will be installed on local hard drive

       Install this feature (but not its sub-features) to the local hard drive.

Entire feature will be installed on local hard drive

       Install the entire function (this function and its sub-functions) to the local hard drive.

Will be installed to run from network

       Install this feature (but not its subfeatures) to run from the network.

Entire feature will be installed to run from network

        Install this feature and its sub-features to run from the network.

Feature will be installed when required

        Features will be installed as needed.

Entire feature will be unavailable

        The entire feature (this feature and its sub-features) will not be installed.

       During installation, the installation of the above functions will be installed on local hard drive by default. Therefore, we can use the default settings. There is no installation setting in Figure 3-5. Click Next ( in Figure 3-5 Other Reset is to reset and restore to default, Disk Usage is hard disk usage) Enter the installation of optional tools in Figure 3-6.

4. Installation of optional tools

       When using Node, we use npm to download and install a certain package or module. During installation, this package or module needs to be compiled by C/C++. At this time, python or VS (Visual Studio) needs to be used. Therefore, it needs to be installed on the machine. If these two tools (called Build Tools here) are not installed, an exception will be prompted when installing such packages or modules.

 Figure 3-6 Installation of optional tools

       Check the Automatically install the necessary tools option in Figure 3-6. Python and VS will be automatically downloaded and installed, and the Windows Chocolatey package manager will also be installed.

       After checking, Chocolatey is actually automatically installed first, and then uses the Chocolatey package manager to download and install python and VS. Chocolatey is a NuGet-based package manager tool developed specifically for Windows systems, similar to Node.js's npm and python's pip. Using Chocolatey in Windows can automatically download and install applications suitable for Windows.

     We can also follow the page instructions linked in Figure 3-6 to install python, VS, and Chocolatey according to our own needs. For example: VS is chargeable. We can choose to manually install the community version of VS. If we have already installed python, we do not need to install the latest version of python.

        Considering the installation speed, we can also uncheck the above options, not install these tools temporarily, install it manually after installing Node, or install it as needed in the future.

       If you check the above options, after the Node.js installation is completed, a script running window will pop up prompting you to automatically install python, VS, and Chocolatey. For details, see Figures 3-10, 3-11, and 3 in 5. below . -12.

        Click Next in Figure 3-6 to enter the node install interface in Figure 3-7.

5. Installation

 Figure 3-7 Node ready for installation

        Click Install in Figure 3-7 to enter the Node.js install progress interface shown in Figure 3-8.

 Figure 3-8 Node.js installation progress

          Figure 3-8 After the installation progress is completed, the Node.js installation completion prompt interface shown in Figure 3-8 appears.

 Figure 3-9 Nodejs installation completed

        At this point, the installation of the Node.js installer has actually been completed.

       If the Automatically install the necessary tools option is checked in 4 above and in Figure 3-6, and we click Finsh in Figure 3-9, the script installation in Figures 3-10, 3-11, and 3-12 will appear. In the prompt window for optional tools (Build Tools), if 4 above and 3-6 in the middle picture are not checked, the following installation prompt will not appear.

 Figure 3-10 Script installation prompt window for python and VS

       Figure 3-10 prompts the prompts and introduction of the script to install python and VS (Visual Build). Press any key to display another prompt and introduction of the script to install Chocolatey, as shown in Figure 3-11.

 Figure 3-11 Prompt window for script installation of Chocolatey

      Figure 3-11 Press any key to display the Windows PowerShell command line window shown in Figure 3-12 below, showing the installation status of Chocolatey, python, and VS.

 Figure 3-11 Chocolatey, python, VS installation

      As can be seen from the prompt in Figure 3-11, after Chocolatey is installed, python and VS will be automatically downloaded and installed. When Chocolatey, python, and VS are installed, you can close Figure 3-11.

       At this point, Node.js, Chocolatey, python, and VS have been successfully installed.

       We can use the win+r shortcut key (i.e. Windows+R) to open the run window, enter cmd and confirm, open the cmd command line window, enter node -v in the command line window, prompt the version information of Node, in the command line window Enter npm -v, and the npm version information will be prompted, indicating that the installation is successful.

4. Node.js common configuration (Configuration, settings)

        The following configuration (Configuration) can be selected according to our needs.

1. Modify the global installation path and cache path of npm package

       After Node is installed, use the npm package manager to install modules and libraries (which can be collectively referred to as packages). First, go to the folder of the global cache path to check whether the package exists. If it exists, install the package directly into the package installation path. There is no need to download it again. If it does not exist, download the package to the cache path and install it into the installation path.

       The global installation path and cache path of the package are in the C drive by default. Considering that the C drive is the system disk to avoid taking up space, we can specify the global installation path and cache path.

(1) Default global installation path and cache path

        There are two commonly used command forms for npm to install modules or libraries (can be collectively referred to as packages):

                     Local installation (local): npm install name

                     Global installation (global): npm install name-g

       To uninstall a module or library, just use uninstall. Local installation and global installation not only have different installation methods and installation locations, but also locally installed packages need to be introduced through require(), while globally installed packages can be used directly from the command line. Local installation solves the problem of version dependencies of different projects on different packages.

       We use the win+r shortcut key ( i.e. Windows+R) to open the run window, enter cmd and confirm, open the cmd command line window, and use npm to install the package in the cmd command line window. If you choose to install locally, use the cmd command line Create the node_modules folder under the current path prompted, which stores the installed packages (you can also change the path through the cd command and install it under this path).

 Figure 4-1 Using npm to install packages locally in the cmd command line window

       The command npm install jquery in Figure 4-1 is to install jquery locally in C:\Users\HP\node_modules. The installation path of the locally installed package can be viewed with the command npm root. Also, note that local installations in other terminal windows may have different paths.

        If you choose global installation, create the node_modules folder under the default path of global installation to store the installed packages.

 Figure 4-2 Using npm to globally install packages in the cmd command line window

       The command npm install jquery -g in Figure 4-2 is a global installation. jquery is installed in the default global installation path. The global installation path of the default package here has been set by default after the node.js installation is completed above. We can use the command npm get prefix (or npm config get prefix) to view the global installation path of the package (you can also use the command npm root -g to view), we can use the command npm get cache (or npm config get cache) to view the global cache path. View the global installation path and cache path of the package as shown in Figure 4-3.

   The global installation path C:\Users\HP\AppData\Roaming\npm and the cache path C:\Users\HP\AppData\Local\npm-cache in Figure 4-3 are already defaulted after node.js is installed. Among them, HP is the username folder. This username is the Windows login account. You can view this username in C:\Users.

 Figure 4-3 Global installation path and cache path of the package

(2) Modify the global installation path and cache path of the package

       The global installation path and cache path packaged above are the default paths after node.js is installed. The default path is the c drive. Considering the space occupied, we can specify it to other drives, such as the D drive. Since the C drive is a system drive, in the win11 system, the security permissions of the folders in the C drive can generally be determined based on the needs of the cmd command line operation, but the d drive is not a system drive. We modify it to d on the cmd command line. When using the global installation path and cache path of the disk, it may not be possible to automatically create the d disk folder and modify its security permissions. If you use npm to install the package, you will be prompted with npm ERR! Error: EPERM: operation not permitted, npm ERR! The operation was rejected by your operating system, as shown in Figure 4-4.

 Figure 4-4 After modifying the installation path and cache path, exceptions occur when installing the package

       Therefore, you need to manually create the installation folder and cache folder for saving, and set security permissions to write and modify the contents of the folder.

       The above situation may not occur in other machines or systems. This machine is a win11 system and has only one solid state drive, divided into drive c and drive d. When you directly use commands to modify the global installation path and cache path of the package, you encounter the above situation. The following processing needs to be done.

       In the win11 system, we use the following two commands to modify the global installation path and cache path of the package (npm config set below can also be written as npm set):

                    npm config set prefix "D:\program\node\node_global"

                    npm config set cache "D:\program\node\node_cache"

        We should first manually create the node_global and node_cache folders in the above path, as shown in Figure 4-5.

 Figure 4-5 Manually create node_global and node_cache folders

      Set the security permissions of the node_global and node_cache folders respectively to write and modify the contents of the folders. We right-click the folder → Properties → Security, as shown in Figure 4-6. Among them, the third folder node_modules is It is automatically generated when installing node and is used to store the built-in modules (also called standard packages) that are automatically installed when node.js is installed.

 Figure 4-6 Folder properties

       Figure 4-6 shows the default folder security settings. The security permissions in Figure 4-6 should be added with two permissions to modify and write, so that we can write and modify in the folder. Click the edit button in Figure 4-6 to pop up the setting interface in Figure 4-7.

 Figure 4-7 Folder security permission permission settings

        Check the modification and writing options pointed by the red arrow in Figure 4-7, and then click OK to exit the settings.

        After the node_global and node_cache folders are set up in the above way, we can enter the above two commands on the cmd command line to modify the global installation path and cache path of the package, and then use npm to install the package successfully.

(3) Environment variable settings

       An environment variable is an object with a specific name in the operating system that contains information that will be used by one or more applications. When the system is asked to run a program without telling it the full path where the program is located, in addition to looking for the program in the current directory, the system will also go to the path specified in the environment variable path to find runnable variables.

       The system variables in the environment variables are valid for all users, and the user variables are valid for the currently logged-in Windows account. When the system variables cannot be found, they will be searched in the current user variables. In addition, environment variable names are not case-sensitive, for example: PATH and path, the two names are the same.

       Therefore, in order to facilitate calling the installed package, we need to configure the environment variable (environment variable) and set the installation path of the package to the value of the environment variable to facilitate calling the package.

       Our right-click computer icon is shown in Figure 4-8.

 Figure 4-8 Computer properties

       Click Properties in the menu in Figure 4-8 to enter the control panel system interface in Figure 4-9.

Figure 4-9 Control panel system

         Click Advanced System Settings at the red arrow in Figure 4-9 to enter the system settings interface in Figure 4-10.

 Figure 4-10 System properties

       Click the environment variable at the red arrow in Figure 4-10 to enter the environment variable interface in Figure 4-11.

 Figure 4-11 Environment variables

        When node.js is installed above, the environment variables are set by default, so the value has already been set in the path in the user variable or system variable in Figure 4-11. Here we only need to modify it. Just change the original value to D:\program\node\node_global. For example: the original default is the user variable path. We double-click path and its value is C:\Users\HP\AppData\Roaming\npm. Edit it and change it to D. :\program\node\node_global, as shown in Figure 1-12.

 Figure 4-12 Edit environment variables

       Figure 4-12 After modification, after clicking OK for everything, the environment variables are configured successfully. When calling, when no path is specified, the system will automatically enter the configured location to match the call.

       If the default is a system variable, modify it in the same way.

2. Configure the mirror source ( mirror station)

       The role of the mirror source (Mirror) is to provide users with download, distribution and management services. Use the command npm get registry (or npm config get registry) in the CMD command line window to view the currently used mirror source address. The default mirror source address used by npm is https://registry.npmjs.org/. Use npm to package When downloading, by default it is downloaded from a foreign npm server, which may be slower. We can change it to a domestic mirror source. Taobao has built a server in China to synchronize packages from foreign official servers to domestic servers, and then provide package distribution services in China. Sometimes, considering the download speed, we can change to Taobao mirror source. In the cmd command Enter the command in the line window (npm config set below can also be written as npm set):

                     npm config set registry=https://registry.npmmirror.com/

        After changing the mirror source, we can use npm to download the package of the Taobao mirror source.

       Above we set the image source manually. We can install the image source management tool nrm and switch the image source through nrm. You can enter the command npm install nrm -g in the cmd command line window to install nrm. After installation, you can use the command nrm ls to view the supported image sources, and directly switch the image source through the image source name. For example: command nrm taobao to switch to Using the Taobao image source, you can use the command nrm current to view the name of the currently used image source. The specific command operations are shown in Figure 4-13.

 Figure 4-13 Command operations of the image source management tool nrm

       The image sources listed in the red box in Figure 4-13 are supported by the package management tool, and npm also supports operations on these image sources. The first column in the red box is the image source name, and the second column is the image source address. The red box The following command switches the mirror source and checks the currently used mirror source address or mirror source name.

3. Configuration package management tools

       Above we use the npm package management tool, we can also use other package management tools, such as: cnpm, yarn, etc. We can enter the following commands in the cmd command line window to install the cnpm and yarn package management tools respectively:

                                   npm install cnpm -g

                                   npm install yarn -g

       After the installation is completed, you can use the cnpm -v and yarn -v commands in the cmd window to check the version status. If the version information appears, it means the installation has been successful.

       npm is the official package manager of node.

      cnpm is the Chinese version of npm. Usually when npm cannot be used due to network and other factors, cnpm comes in handy. However, cnpm is generally only used for installation, so we still use npm when creating projects, uninstalling and other operations. Therefore, cnpm cannot be used to uninstall packages.

       Yarn is a new JS package management tool jointly launched by Facebook, Google, Exponent and Tilde. It appears to make up for some of the shortcomings of npm.

       Package management tools such as npm, cnpm, and yarn have their own characteristics. Generally speaking, the image sources listed in the red box in Figure 4-13 can be operated with these package management tools. However, some of their operation commands are different when using them. You should pay attention to the command format when using them. In addition, taking into account network, package and other factors, when one package management tool encounters problems when using it, you can try to use another. A package management tool to implement requirements.

5. Operation of node.js

       Node is a running environment. From the analysis of 1. , we can see that node is equivalent to an environment with a compiler (Compiler). After installing node above, we also need tools to call and run this node environment (that is, it is equivalent to calling and running with compiler). environment of the machine), thereby realizing the execution of JavaScript ("JS") code on the machine.

       We can use cmd.exe or IDE (Integrated Development Environment) to call and run node, that is, call an environment with a compiler (Compiler). The compiler (Compiler) needs cmd.exe or IDE (Integrated Development Environment) to call and run. In fact, it involves the operating system and plays its role (computer resource allocation), so that the program can run smoothly. For the operation of the compiler and its relationship with the development environment, see Parts 1 and 5 of Compiler (Compiler) and C/C++ Compiler Installation (c+ Installation) .

1. cmd.exe runs node

       Run node through the cmd.exe command line of Windows. This method is similar to cmd running the c/c++ compiler. For details, see cmd running the compiler in Windows (cmd runs c/c++, python, etc.) . There, you can call the c/c++ compiler. gcc or g++, here we use node.exe to call the node running environment. As shown in Figure 5-1, we use cmd to run the file jt.js (the code is as follows).

var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello, JavaScript!');
res.end();
});
server.listen(1000);
console.log('Server running at http://localhost:1000/');

 Figure 5-1 cmd runs node

Copy the address http://localhost:1000/        in Figure 5-1 to the browser and you can see the Hello, JavaScript! message returned by the web server.

   I won’t go into details about running node in cmd here. The specific usage can be similar to the cmd running compiler in Windows (cmd runs c/c++, python, etc.) .

       If we want to record the detailed information of the project to facilitate future project version iteration and project migration, we can use the command npm init in cmd to set it up. This operation is also called npm init to initialize the project and generate a package.json file. After this operation, when I pass the project to the other party, I don't need to send the project dependency package to the other party. After the other party receives my project and then executes npm install, all the project dependencies can be downloaded into the project. The package.json file can contain the following options.

package name : project name;

version : project version;

description : Project description to facilitate users to search and understand the project;

keywords : project keywords, providing key words involved in the project so that users can search and understand the project;

git repository : git warehouse address. If the project is uploaded to git, you need to fill in the git warehouse address;

author : project developer;

license : software licensing, such as: open source agreements such as MIT or ISC;

dependencies : The dependency packages required by the project running environment (running phase) are key-value configurations, where key represents the name of the package and value represents its version number, for example:

"dependencies": {
   "boo": "^2.0.1",
   "thr": "3.3.x",
}

     Dependency packages can be classified and distinguished in the package.json file to facilitate targeted downloading by users. In addition to dependencies, there are also classifications such as devDependencies, peerDependencies, peerDependenciesMeta, and bundleDependencies. These classifications of dependency packages are mainly for User-friendly. Some download operations do not distinguish which type of package is downloaded. If you want to download a certain type of dependent package in these categories, specific operation commands are required.

main : The default executable file of the project, the entry point of the project, usually the file used to start the project. If this field is not set, the index.js file in the project root directory is loaded by default.

scripts : The built-in script entry in package.json is a key-value pair configuration. Key is the operation name, and value is the object to run the operation. This operation can be performed through npm run, for example:

"scripts": {
" tt": "node test.js ",
" hello ": "node hello.js ",
}

       npm run tt will execute node test.js. The script here can be understood as calling an already written program through some prescribed code. A script is a sentence-by-sentence, organized, operational, and process-oriented code, similar to an acting script. Actors know what they should perform and what lines they should say based on the script. In some contexts, a script is a programming language that is interpreted and executed at the same time, such as Lua, JavaScript, etc.

       The npm init command will gradually prompt the settings. If you want to exit the settings, select Press ^C at any time to quit., that is, press the ctr+c keys, which is equivalent to the exit command operation in cmd.exe, which means interrupting the execution of the command.

       If you use the command npm init -y, the default initialization project, that is, the default project information settings, is used to generate a default package.json file. The parameter y means yes, which is equivalent to automatically accepting the default project information settings.

       To edit or view the package.json file in cmd.exe, you can use the command notepad package.json to open the package.json file with a text editor, as shown in Figure 5-2 to open the package in C:\Users\HP. json file, after opening it, you can manually set the options as needed and save it.

 Figure 5-2 cmd text editor opens package.json

2. Run node in the integrated development environment

        Run node through the integrated development environment (IDE) (that is, the integrated development environment calls and runs node.exe). Integrated development environment tools come with a complete set of tools that can help us improve language development efficiency (such as code editing, debugging, syntax highlighting, intelligent prompts (intellisense), etc.), which can improve project development and management efficiency.

      We can use development environment tools such as PyCharm, VS, VS Code, etc. to run node. Let's take PyCharm as an example. When we use PyCharm to open an empty folder to create a node.js project, node.exe will be called by default (the calling path is automatically set by default. Of course, different development environments may have different settings), and then we will Create a .js file to run the file, as shown in Figure 5-2 using pycharm to run node.

 Figure 5-2 pycharm runs node

        The code in Figure 5-2 is as follows:

function test(){
    let a = 3;
    let b = 9;
    console.log('a+b='+(a+b));
}
test();

For the installation and configuration of VS Code, please see : https://blog.csdn.net/thefg/article/details/131752996

For the installation and configuration of PyCharm, please see: https://blog.csdn.net/thefg/article/details/128881507

 The latest Python installation detailed tutorial in 2023: https://blog.csdn.net/thefg/article/details/128601410 

Detailed tutorial on Python basics------Make programming easy, please note:

https://blog.csdn.net/thefg/article/details/127846247​​​​​​​

Finally, you are welcome to like, collect and follow!

Guess you like

Origin blog.csdn.net/thefg/article/details/132410794