Install and configure NodeJS under Windows


1. Install NodeJS
    download address:
        http://nodejs.cn/download/ (Chinese address)
        https://nodejs.org/en/download/  → For example, download the file: https://nodejs.org/dist/v12. Install 18.2/node-v12.18.2-x64.msi

        
    after the download is complete, as here, our installation path is D:\Net_Program\Net_NodeJS


    
    After installation, you can view its version information:
        run CMD as an administrator, and then enter the commands node -v and npm -v to view its version information


        
    Note: The new version of Node.js has its own npm, and it will be installed when you install Node.js. The role of npm is to manage the packages that Node.js depends on. It can also be understood as the things
        
that need to be installed/uninstalled by Node.js. 2. Configure the global installation path of npm
        . Add node_global (used to store NodeJS dependencies) in the NodeJS installation directory. Package file) and node_cache (used to store files when downloading dependent packages) folder
        Run CMD as an administrator, locate the installation directory of NodeJS, and enter the following commands respectively:

npm config set prefix "D:\Net_Program\Net_NodeJS\node_global"
npm config set cache "D:\Net_Program\Net_NodeJS\node_cache"


        


        Right-click the computer, click Properties, click Advanced System Settings, click Environment Variables,
            System Variables, New, the variable name is NODE_PATH , the variable value is D:\Net_Program\Net_NodeJS\node_global\node_modules
            user variable, Path edit, change C:\Users\ Modify quber\AppData\Roaming\npm to D:\Net_Program\Net_NodeJS\node_global
            system variable, Edit Path, add D:\Net_Program\Net_NodeJS\node_global , if it already exists, you don’t need to add it


3. Test
    Open CMD and enter node to enter the development mode. At
        this time, you can enter console.log('Hello NodeJS!') and press Enter, the Hello NodeJS

        
    express module will be displayed in the window.
        After the test configuration is completed, install a module Under the test, we install the most commonly used express module, open the CMD window, locate the NodeJS installation directory, enter the following command to install the module globally (after the installation is complete, we will find that there are more corresponding in the previously defined global folder node_global Dependent packages):

npm install express -g

-g means global installation. After



            
        installing the express module, open a new CMD window and locate the installation directory, then enter node to enter the development mode, and then enter the require('express') command and press Enter. If you can list a few The information indicates that the express module has been successfully installed.

        
        In addition, we can also create a js file to test the express module. We create a test.js file in the root directory of the D drive, with the following content:

var express = require('express'); 
var app = express(); 
app.get('/', function(req, res){
    res.send("Hello World 8888 ");  
    console.log("Hello World 8888 ");  
}); 
app.listen('8888');
console.log("nodejs start listen 8888 port!");

        Then open the CMD window, locate the root directory of the D disk , and enter the command node test.js , and then we enter http://127.0.0.1:8888/ in the browser to visit and we will see the content output in the js file. At the same time, every time the browser is refreshed, the CMD window will print Hello World 8888.


        

Guess you like

Origin blog.csdn.net/qubernet/article/details/107346737