简单的说 Node.js 就是运行在服务端的 JavaScript。
Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台。
Node.js是一个事件驱动I/O服务端JavaScript环境,基于Google的V8引擎,V8引擎执行Javascript的速度非常快,性能非常好。
仔细品一下V8引擎,是不是与JVM的运行是环境类似。一个运行js,一个运行class。
注意是js执行引擎,HTML、CSS样式等渲染是不支持的。
那么,一般使用node干什么呢?
web前端开发,HTML、css、js等基础技术已经根深蒂固,但是快速开发比较麻烦,有了node,像Vue等框架就可以使用更加友好的语法,然后通过Vue构建成HTML、js形式。越看越像,
安装
Node 安装配置
1、安装
2、配置npm安装路径:
npm config set prefix “node根路径\node_global”
npm config set cache “node根路径\node_cache”
3、配置环境变量:
NODE_PATH:node根路径\node_global\node_modules
path添加:node根路径\node_global
4、安装CNPM:
npm install -g cnpm --registry=https://registry.npm.taobao.org
5、拉镜像:
docker pull node:latest
6、运行:
docker run -itd --name node-test node
–name node-test:容器名称。
7、进入容器:
docker exec -it node-test /bin/bash
常用命令
NPM
- npm 基础
npm package scope, access level, and visibility
CLI commands - 配置:创建pacakage.json
npm init 创建定制的
npm init -y 创建默认的package.json
- 配置:通过命令设置可选配置
查看配置:npm config ls -l / cnpm config ls -l
官网文档
npm set init.author.email “example-user@example.com”
npm config set a=1
以上命令等价修改配置文件.npmrc 或 .cnpmrc
- 升级NPM
sudo npm install npm -g
window 系统:npm install npm -g
- 使用淘宝镜像
npm install -g cnpm --registry=https://registry.npm.taobao.org
- 安装模块
npm install
如:
npm install express # 本地安装
npm install express -g # 全局安装
使用:
var express = require(‘express’);
常见错误:npm err! Error: connect ECONNREFUSED 127.0.0.1:8087
解决办法:npm config set proxy null
本地安装:
- 将安装包放在 ./node_modules 下(运行 npm 命令时所在的目录),如果没有 node_modules 目录,会在当前执行 npm 命令的目录下生成 node_modules 目录。
- 可以通过 require() 来引入本地安装的包。
全局安装:Installing a package globally allows you to use the code in the package as a set of tools on your local computer.
- 将安装包放在 /usr/local 下或者你 node 的安装目录。
- 可以直接在命令行里使用。
如果希望具备两者功能,则需要在两个地方安装它或使用 npm link。
- 安装目录:
设置安装目录:npm config set cache “D:\nodejs\node_cache”
npm config set prefix “D:\nodejs\node_global”
查看全局安装路径:npm root -g
查看安装路径:npm root
- npm update :把当前目录下node_modules子目录里边的对应模块更新至最新版本。
-g可以把全局安装的对应命令行程序更新至最新版。
-
npm cache clear:可以清空NPM本地缓存,用于对付使用相同版本号发布新版本代码的人。
-
npm unpublish @:可以撤销发布自己发布过的某个版本代码。
-
在package.json所在目录下使用npm install . -g可先在本地安装当前命令行程序,可用于发布前的本地测试。
NRM
nrm(npm registry manager )是npm的镜像源管理工具,有时候国外资源太慢,使用这个就可以快速地在 npm 源间切换。
安装:
npm install -g nrm
查看可选的源,带*的是当前使用的源:
nrm ls
切换到taobao源:
nrm use taobao(数据源名)
添加数据源:
nrm add registry(数据源名) http://registry.npm.frp.trmap.cn/(数据源地址)
删除数据源:
nrm del registry(数据源名)
测试相应源的响应时间
nrm test
YARN
yarn config set registry https://registry.npm.taobao.org -g
yarn config set sass_binary_site http://cdn.npm.taobao.org/dist/node-sass -g
使用yrm工具管理一些npm源
-
安装yrm
yarn global add yrm
npm install -g yrm -
查看可用源
yrm ls -
选择源
yrm use yarn -
快速删除node_modules,手动删除真的很慢:
安装: npm install rimraf -g
使用:rimraf node_modules
rimraf是node的一个包,可以快速删除node_modules -
安装yarn
npm install -g yarn -
查看版本号:
yarn --version -
创建文件夹 yarn
md yarn -
进入yarn文件夹
cd yarn -
初始化项目
yarn init // 同npm init,执行输入信息后,会生成package.json文件 -
yarn的配置项:
yarn config list // 显示所有配置项
yarn config get //显示某配置项
yarn config delete //删除某配置项
yarn config set [-g|–global] //设置配置项 -
安装包:
yarn install //安装package.json里所有包,并将包及它的所有依赖项保存进yarn.lock
yarn install --flat //安装一个包的单一版本
yarn install --force //强制重新下载所有包
yarn install --production //只安装dependencies里的包
yarn install --no-lockfile //不读取或生成yarn.lock
yarn install --pure-lockfile //不生成yarn.lock -
添加包(会更新package.json和yarn.lock):
yarn add [package] // 在当前的项目中添加一个依赖包,会自动更新到package.json和yarn.lock文件中
yarn add [package]@[version] // 安装指定版本,这里指的是主要版本,如果需要精确到小版本,使用-E参数
yarn add [package]@[tag] // 安装某个tag(比如beta,next或者latest)
//不指定依赖类型默认安装到dependencies里,你也可以指定依赖类型:
yarn add --dev/-D // 加到 devDependencies
yarn add --peer/-P // 加到 peerDependencies
yarn add --optional/-O // 加到 optionalDependencies
//默认安装包的主要版本里的最新版本,下面两个命令可以指定版本:
yarn add --exact/-E // 安装包的精确版本。例如yarn add foo@1.2.3会接受1.9.1版,但是yarn add foo@1.2.3 --exact只会接受1.2.3版
yarn add --tilde/-T // 安装包的次要版本里的最新版。例如yarn add foo@1.2.3 --tilde会接受1.2.9,但不接受1.3.0 -
发布包
yarn publish
移除一个包
yarn remove :移除一个包,会自动更新package.json和yarn.lock -
更新一个依赖
yarn upgrade 用于更新包到基于规范范围的最新版本 -
运行脚本
yarn run 用来执行在 package.json 中 scripts 属性下定义的脚本 -
显示某个包的信息
yarn info 可以用来查看某个模块的最新版本信息 -
缓存
yarn cache
yarn cache list # 列出已缓存的每个包 yarn cache dir # 返回 全局缓存位置 yarn cache clean # 清除缓存