Problems with npm install command in Nodejs

    I have encountered many pits in the npm package management tool using nodejs, and it took a long time to solve it on the Internet, and now I will summarize it.

  Two installation methods (local installation, global installation)

  1. Global installation (npm install -g moduleName/npm install ----global moduleName)

    Global installation, as the name implies, represents the installation into the global environment. You can view the global installation path with the following command   

    npm root -g
    npm prefix -g

  

    Note that the difference between the two paths is the node_modules directory

    The global installation path can be set by the following command (there is no node_modules, after setting npm install -g will automatically generate a node_modules directory in the directory you set, and the modules you need to install globally will be downloaded into it), the default global installation path of windows is C:\Users\xxx\AppData\Roaming\npm

npm config set prefix "your node global path"

    After global installation, you can directly run the commands supported by the component package in the cmd command window, as shown in the following figure after the global installation directory structure (if the error is not an internal or external command..., directly add the global installation path to the path environment variable)

              

    2. Install npm install modulesName locally

      Local installation refers to installing ./node_modules in the current path of your command line. After local installation, you can directly import modules in the node_modules directory of the project by means of require().

  

     Introducing modules: When introducing modules in js code, node.js will look for modules in NODE_PATH and the node_modules folder under the current js project by default. Therefore, if it is only installed globally, the module cannot be directly referenced by require() ,You need to manually solve the configuration problem of the package path, you can set the NODE_PATH of the environment variable to E:\Program Files\nodejs\node_global\node_modules (the path obtained by npm root -g)

 

    Advantages and disadvantages of local installation and global installation:

      First of all, if you install locally, you can only reference it in the current project. When using each project, you need to reinstall it in the current project. The global installation can be referenced anywhere (provided that the NODE_PATH is correct), and you can use it multiple times after installing it once. , but there is a problem, modules also have versions, if a module is used in multiple projects during global installation, when the global package is updated, it may affect your multiple projects, and the dependencies among them Relationships will be broken, so a local installation is good for independence between different projects.

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326628368&siteId=291194637