Nodejs installation and environment variable configuration (modify the global installation dependency tool package, cache folder and npm image source)

Local environment: win11 home Chinese version

1. Official website download

 2. Installation

 

 

 

 

 

 

 3. View nodejs and npm version numbers

1. Check the node version number

node -v

2. Check the NPM version number (npm is automatically installed when installing nodejs)

npm -v

 4. Configure npm global download toolkit and cache directory

1. View the installation directory

 Create the node_global and node_cache directories in this directory. Dependent tool packages installed through npm in the future will be installed in the node_global folder to avoid installation in the default C drive.

 2. The npm command sets the global download and installation tool package and cache directory.

npm config set prefix "D:\Nodejs\node-v18.16.0\node_global" //设置依赖工具包全局安装目录,通过npm下载的工具包都会下载到本文件夹下

npm config set cache "D:\Nodejs\node-v18.16.0\node_cache"  //设置缓存目录

 3. Write node_global and node_cacche to the environment variables.

3.1. Add a NODE_HOME variable in the system environment, the value is the directory where nodejs is installed.

3.2. Add 2 new entries in path and write the two folder paths node_global and node_cacche.

 3.3. After the environment variable configuration is completed, set the Taobao image as the source of npm, otherwise it will be extremely slow when downloading and installing the toolkit (unless you are scientific on the Internet)

npm config set registry https://registry.npm.taobao.org  //npm设置淘宝镜像源

 

Use Taobao NPM mirror

Since it is very slow to directly use the official npm image in China, it is recommended to use the Taobao NPM image.

The Taobao NPM image is a complete npmjs.org image. You can use this instead of the official version (read-only). The synchronization frequency is currently once every 10 minutes to ensure that it is as synchronized with the official service as possible.

You can use Taobao's customized cnpm (gzip compression support) command line tool instead of the default npm:

$ npm install -g cnpm --registry=https://registry.npmmirror.com

This way you can use the cnpm command to install the module:

$ cnpm install [name]

3.4. View npm configuration list

 5. Test

1. Check that the node_global directory is empty

 

2. Use the npm command to install the module

The syntax of npm installing Node.js modules is as follows:

$ npm install <Module Name>

In the following example, we use the npm command to install the commonly used Node.js web framework module  express :

$ npm install express

After installation, the express package is placed in the node_modules directory under the project directory, so you only need to use require('express') in the code   without specifying the third-party package path.

var express = require('express');

Global installation vs. local installation

npm package installation is divided into two types: local installation (local) and global installation (global). Judging from the command line, the difference is only whether there is -g, such as

npm install express # Local installation 
npm install express -g # Global installation

If the following error occurs:

npm err! Error: connect ECONNREFUSED 127.0.0.1:8087 

The solution is:

$ npm config set proxy null

Local installation

  • 1. Place the installation package under ./node_modules (the directory where the npm command is run). If there is no node_modules directory, the node_modules directory will be generated in the directory where the npm command is currently executed.
  • 2. Locally installed packages can be introduced through require().

Global installation

  • 1. Place the installation package in /usr/local or the installation directory of your node.
  • 2. Can be used directly in the command line.

If you want the functionality of both, you'll need to install it in both places or use  npm link .

Next we install express globally

$ npm install express -g

The installation process outputs the following content. The first line outputs the module version number and installation location.

[email protected] node_modules/express
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected]
├── [email protected] ([email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected])
├── [email protected] ([email protected], [email protected])
├── [email protected] ([email protected], [email protected])
└── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])

View installation information

You can use the following command to view all globally installed modules:

$ npm list -g

├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
│ ├─┬ [email protected]
│ │ └── [email protected]
……

If you want to check the version number of a module, you can use the following command:

$ npm list grunt

projectName@projectVersion /path/to/project/folder
└── [email protected]

Using package.json

package.json is located in the directory of the module and is used to define the properties of the package. Next, let's take a look at the package.json file of the express package, located in node_modules/express/package.json:

Package.json 属性说明
name - 包名。

version - 包的版本号。

description - 包的描述。

homepage - 包的官网 url 。

author - 包的作者姓名。

contributors - 包的其他贡献者姓名。

dependencies - 依赖包列表。如果依赖包没有安装,npm 会自动将依赖包安装在 node_module 目录下。

repository - 包代码存放的地方的类型,可以是 git 或 svn,git 可在 Github 上。

main - main 字段指定了程序的主入口文件,require('moduleName') 就会加载这个文件。这个字段的默认值是模块根目录下面的 index.js。

keywords - 关键字

3. Install common module express globally

npm install -g express  //全局安装express模块

 Common npm commands:

npm -v:查看npm安装的版本。
npm init:会引导你建立一个package.json文件,包括名称、版本、作者等信息。
npm list:查看当前目录下已安装的node包。
npm ls:查看当前目录下已安装的node包。
npm install moduleNames:安装Node模块到本地目录node_modules下。
npm install < name > -g:将包安装到全局环境中。
npm install < name > --save:安装的同时,将信息写入package.json中,项目路径中若是有package.json文件时,直接使用npm install方法就能够根据dependencies配置安装全部的依赖包,这样代码提交到git时,就不用提交node_modules这个文件夹了。
npm install < name> --save-dev:安装的同时,将信息写入package.json中项目路径中若是有package.json文件时,直接使用npm install方法就能够根据devDependencies配置安装全部的依赖包,这样代码提交到git时,就不用提交node_modules这个文件夹了。
npm uninstall moudleName:卸载node模块。

6. Install angular-cli and vue-cli globally. These are the two front-end development frameworks I need.

1. Install angular

npm  install -g  @angular/cli  //全局安装angular脚手架

 2. Install vue

npm install -g @vue/cli   //全局安装vue脚手架vue -V     //查看vue版本

Guess you like

Origin blog.csdn.net/lovedingd/article/details/132169073