nodeJS environment with

1. Node.js is what
1.1 Node.js is based Chrome V8 engine [JavaScript runtime environment]. Node.js uses an event-driven, non-blocking I / O model.
1.2 Node.js is a platform to make JavaScript running in the development server, it allows JavaScript to be with PHP, Python, Perl, Ruby and other server-side scripting language on an equal footing

Note 1: Node.js -> JavaScript runtime environment, development languages: JavaScript
the J2EE -> the Java runtime environment, development of language is java
Note 2: Node.js v10.15.3 Document address: http: //nodejs.cn/ api /


2. npm what
npm actually Node.js package management tool (package manager).

Why we need a package management tool? Because we develop on Node.js, we will use a lot of JavaScript code written by someone else.
If we want to use a package written by someone else, always searching by name at the official website, download the code, extract, re-use, very cumbersome.
So a centralized management tool emerged: put npm official website after everyone had put their own developed modules package, if you want to use,
directly through npm installation can be directly used, there is no managed code which should be where to download.

More importantly, if we want to use modules A, and module A turn depends on module B, module B in turn depends on module X and module the Y-,
npm according dependencies, all dependent packages are downloaded and manage. Otherwise, manually manage on our own, certainly cumbersome and error-prone.

Note 1: npm == maven somewhat similar


3. Node.js environment to build
because the platform is Node.js JavaScript code running in the back end, so, you must first install the Node environment in this machine.
Think about it, the first step in the development of java: is not installed JDK, and then configure the environment variables java_home / classpath / path

3.1 Download
Download: https: //nodejs.org/zh-cn/download/
select the appropriate version download, used in this chapter are: node-v10.15.3-win-x64.zip

Note 1: Node There are two versions of the line: LTS is a stable version of the long-term maintenance of Current is a new feature version

3.2 extract the
extract files to the specified location (for example: D: \ initPath), and to establish and node_cache node_global two directories under the directory after extracting

Note 1: New directory Description
node_global: npm global installation location
node_cache: npm cache path

Note 2: This tutorial is to extract the files to D: \ initPath directory, behind all this as an example, the actual development to modify their extract the directory
D: \ initPath \ node-v10.15.3 -win-x64

3.3 configuration environment variable
added NODE_HOME, is: D: \ initPath \ node- v10.15.3-win-x64
modify the PATH and finally adding:;% NODE_HOME%;% NODE_HOME % \ node_global;

Note 1: The environment variable View
echo% node_home%
echo% path%

注2:测试安装是否成功:打开cmd窗口,输出如下命令会输出NodeJs和npm的版本号
node -v
npm -v

3.4 配置npm全局模块路径和cache默认安装位置
打开cmd,分开执行如下命令:
npm config set cache "D:\initPath\node-v10.15.3-win-x64\node_cache"
npm config set prefix "D:\initPath\node-v10.15.3-win-x64\node_global"

注1:将步骤一创建的node_global(npm全局安装位置)和node_cache(npm缓存路径)与npm联系起来
注2:如果执行命令卡死,可以删除C:\Users\用户名\.npmrc 后重新执行。(用户名:为当前电脑的用户名)
注3:"D:\initPath\node-v10.15.3-win-x64\node_global",双引号不能少

3.5 修改npm镜像提高下载速度(可以使用 cnpm 或 直接设置 --registry ,推荐设置 --registry)
3.5.1 --registry
## 设置淘宝源
npm config set registry https://registry.npm.taobao.org/
## 查看源,可以看到设置过的所有的源
npm config get registry

注1:其实此步骤的内容就是将以下代码添加到C:\Users\用户名\.npmrc文件中
registry=https://registry.npm.taobao.org
3.5.2 cnpm
npm install -g cnpm --registry=https://registry.npm.taobao.org

注1:cnpm安装完成后,以后就可以用cnpm命令代替npm命令, 此时npm还是会用官方镜像,cnpm会用国内镜像
注2:如果要恢复成原来的设置,执行如下:
npm config set registry https://registry.npmjs.org/

3.6 验证安装结果
3.6.1 版本验证(同步骤3.3,注2)
node -v
npm -v

3.6.2 查看淘宝镜像设置情况
npm get registry

3.6.3 查看npm全局路径设置情况
## 此步骤随便全局安装一个模块就可以测评
npm install webpack -g
## 以上命令执行完毕后,会生成如下文件
%node_home%\node_global\node_modules\webpack
注意:下载过程中出现warn不用管,出现Error,删掉下载的破碎文件重新下载


4. 如何运行下载的Node.js项目
将下载的项目解压到指定目录,本例是解压到:D:\temp\vueproject,后面都以此为例
## 1. 打开命令窗口
cmd

## 2. 切换到d盘
e:

## 3. 进入指定目录
cd E:\temp\vueproject


### 下面的才是关键代码 ###
## 4. 进行依赖安装
## 命令执行完后,你会发现,项目的根目录下多了一个node_modules文件夹,
## 那里面就是从npm远程库里下载的模块,然后“安装”到你的项目中,
## 此步骤,可理解成修改maven的pom文件添加依赖,然后maven再从中央仓库下载依赖
## 那pom文件在哪里呢?其实就是项目中的package.json
npm install

## 5. 启动项目
npm run dev

 

操作cmd命令,正常运行截图:

 

 

 

运行完以上代码第三方跑起来截图:

Guess you like

Origin www.cnblogs.com/bf6rc9qu/p/11406879.html