2022-06-27

N_Node.md

Node

简单的说 Node.js 就是运行在服务端的 JavaScript; Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台; Node.js是一个事件驱动I/O服务端 JavaScript 环境, 基于 Google 的V8引擎, V8引擎执行Javascript的速度非常快, 性能非常好;

in short 提供本地环境封装, 给JavaScript访问

官中文页 API文档 以往的版本

MVM

nvm(Node Version Manager)是一个用于管理多个 Node.js 版本的工具,支持在不同项目或环境中快速切换 Node.js 版本。以下是 nvm 的常见用法:

https://github.com/nvm-sh/nvm

https://github.com/coreybutler/nvm-windows/releases

基础命令​

命令说明
nvm --version查看 nvm 版本
nvm install <version>安装指定版本的 Node.js(如 nvm install 18.16.0
nvm uninstall <version>卸载指定版本
nvm use <version>切换到指定版本(临时生效,仅当前终端)
nvm ls列出所有已安装的版本
nvm ls-remote列出所有可远程安装的版本
nvm current显示当前使用的版本
nvm alias <name> <version>创建别名(如 nvm alias default 18.16.0
nvm unalias <name>删除别名
nvm run <version> <app.js>在指定版本下运行脚本

安装Node

# 安装最新 LTS 版本
nvm install --lts
 
# 安装特定版本
nvm install 22.19.0
 
# 切换到指定版本
nvm use 16.20.2
 
# 设置默认版本(新终端自动生效)
nvm alias default 16.20.2

多版本管理​​


# 查看已安装版本
nvm ls

# 切换回默认版本
nvm use default

# 卸载旧版本
nvm uninstall 14.17.0

NPM

NPM 是随同 NodeJS 一起安装的包管理工具, 能解决 NodeJS 代码部署上的很多问题, 常见的使用场景有以下几种:

允许用户从NPM服务器下载别人编写的第三方包到本地使用; 允许用户从NPM服务器下载并安装别人编写的命令行程序到本地使用; 允许用户将自己编写的包或命令行程序上传到NPM服务器供别人使用;

in short 就是类似JS版 Maven, 管理依赖工具

官方文档

NPM VS NPX

npx 是 npm5.2 之后发布的一个命令。官网说它是“execute npm package binaries”,就是执行npm依赖包的二进制文件,简而言之,就是我们可以使用npx来执行各种命令。使用npx可以在命令行直接执行本地已安装的依赖包命令,不用在scripts脚本写入命令,也不用麻烦的去找本地脚本。使用npx,可以在不全局安装依赖包的情况下,运行命令,而且运行后不会污染全局环境。

  • npx 侧重于执行命令的,执行某个模块命令。虽然会自动安装模块,但是重在执行某个命令。
  • npm 侧重于安装或者卸载某个模块的。重在安装,并不具备执行某个模块的功能。

配置

https://docs.npmjs.com/cli/v6/using-npm/config

Default Configs Run npm config ls -l to see a set of configuration parameters that are internal to npm, and are defaults if nothing else is specified.

全局安装路径

https://docs.npmjs.com/cli/v6/using-npm/config#prefix

npm prefix -g
# 可以通过命令来更改路径
npm config set prefix e:\node_local_repository

prefix Default: see folders Type: path The location to install global items. If set on the command line, then it forces non-global commands to run in the specified folder.

nodejs 查找模块是在自带变量module.paths列表去寻找

console.log("find paths = "+ module.paths);
//输出: find paths =  E:\content-for-work\2022-02院考核\node_modules,E:\content-for-work\node_modules,E:\node_modules

这个变量默认会包含环境变量 NODE_PATH, so 要想node 项目在哪都能 require(全局库) require('THR LIB');, 通过 npm prefix -g 找到全局安装的路径, 然后添加到环境变量 NODE_PATH 中

配置代理

设置代理

npm config set proxy=http://127.0.0.1:3223
npm config set proxy=http://127.0.0.1:10809

取消代理

npm config delete proxy
npm config delete https-proxy

清理缓存

npm cache clear —force //清缓存 npm i —legacy-peer-deps //解决依赖冲突

安装依赖

安装卸载依赖

本地项目安装 npm install grunt 本地安装 grunt npm uninstall grunt 卸载 grunt

npm install -g grunt-cli 全局安装 grunt-cli npm uninstall -g grunt-cli 卸载全局安装的 grunt-cli

更新最新依赖

升级 element-ui 需要先卸载原先的版本 npm uninstall element-ui npm install element-ui -g grunt-cli

MCU

//ncu
npm install -g npm-check-updates
//列出更新预览
ncu --upgrade
//更新到 package.json文件. 不会实际更新
ncu -u to upgrade package.json
//真正更新
npm install

npm 淘宝源

官方: https://registry.npmjs.org 中国: https://registry.npmjs.com.cn

  • 通过 npm config set registry 修改后所有 npm 操作都会默认使用该镜像源:
# 1.  
# npm 官方源
npm config set registry https://registry.npmjs.org/

# 淘宝源
npm config set registry https://registry.npmmirror.com/

# 2. 验证是否修改成功(查看当前 registry 配置)
npm config get registry
  • 通过配置文件手动修改

npm 的配置文件通常在用户目录下(如 Windows 是 C:\Users\你的用户名\.npmrc,Mac/Linux 是 ~/.npmrc),你也可以直接编辑这个文件:

  1. 打开 .npmrc 文件(没有则新建);
  2. 添加 / 修改一行:registry=https://registry.npmjs.org/
  3. 保存文件后,重启终端即可生效。

安装 cnpm:

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

一些常用库

类似maven 的中央仓库

lodash

lodash 是高性能的 JavaScript 实用工具库。

npm i —save lodash

https://www.lodashjs.com/docs/lodash.pad

一维码生成库 jsbarcode

npm install --save jsbarcode https://www.npmjs.com/package/jsbarcode

First create an canvas (or image)
 
 
<svg id="barcode"></svg>
<!-- or -->
<canvas id="canvas"></canvas>
<!-- or -->
<img id="barcode"/>
 
 
//Example with options:
 
import JsBarcode  from 'jsbarcode';
JsBarcode("#barcode", "1234", {
  format: "pharmacode",
  lineColor: "#0aa",
  width:4,
  height:40,
  displayValue: false
});
 
 
//More advanced use case:
import JsBarcode  from 'jsbarcode';
JsBarcode("#barcode")
  .options({font: "OCR-B"}) // Will affect all barcodes
  .EAN13("1234567890128", {fontSize: 18, textMargin: 0})
  .blank(20) // Create space between the barcodes
  .EAN5("12345", {height: 85, textPosition: "top", fontSize: 16, marginTop: 15})
  .render();
 

二维码生成库 qrcode

https://www.npmjs.com/package/qrcode

npm install --save qrcode

<!-- index.html -->
<html>
  <body>
    <canvas id="canvas"></canvas>
    <script src="bundle.js"></script> 
  </body>
</html>
// index.js -> bundle.js
var QRCode = require('qrcode')
var canvas = document.getElementById('canvas')
 
QRCode.toCanvas(canvas, 'sample text', function (error) {
  if (error) console.error(error)
  console.log('success!');
})
 

前端处理 Excel?

mpmjs -xlsx

import FileSaver from 'file-saver'
import XLSX from 'xlsx'
 
const filetName = this.$store.getters.user.deptName + "返乡重点人员'两本账'情况.xlsx"
var wb = XLSX.utils.table_to_book(document.querySelector('#out-table'))
 
var wbout = XLSX.write(wb, {
  bookType: 'xlsx',
  bookSST: true,
  type: 'array'
})
try {
  FileSaver.saveAs(
    new Blob([wbout], { type: 'application/octet-stream' }),
    filetName
  )
} catch (e) {
  if (typeof console !== 'undefined') console.error(e, wbout)
}

中文转拼音库

npm install --save pinyin

mpmjs -pinyin

 
var pinyin = require("pinyin");
 
console.log(pinyin("中心"));    // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
  heteronym: true               // 启用多音字模式
}));                            // [ [ 'zhōng', 'zhòng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
  heteronym: true,              // 启用多音字模式
  segment: true                 // 启用分词, 以解决多音字问题; 
}));                            // [ [ 'zhōng' ], [ 'xīn' ] ]
console.log(pinyin("中心", {
  style: pinyin.STYLE_INITIALS, // 设置拼音风格
  heteronym: true
}));  

Day.js 一个轻量日期库

Day.js

Moment.js 文档

https://momentjs.cn/docs/

Vue.Draggable 一个基于vue的拖拽

Vue.Draggable

Echarts for npm

官页 官页 使用教程

配置图表内容与边缘之间的距离!

grid: {
    left: '3%',
    right: '4%',
    bottom: '3%',
    containLabel: true
},

ECharts and zrender installed by npm will be placed under node_modules. You can obtain echarts directly in project with require(‘echarts’).

var echarts = require('echarts');
 
// initialize echarts instance with prepared DOM
var myChart = echarts.init(document.getElementById('main'));
myChart.setOption({...})

rimraf (删node_modules)

npm install rimraf -g rimraf node_modules

YARN

• 速度快:Yarn缓存了每个下载过的包,再次使用时无需重复下载。同时它是并行的,因此安装速度更快。 • 安全:在执行代码之前,Yarn会通过算法校验每个安装包的完整性。 • 可靠:使用详细简洁的锁文件格式和明确的安装算法,使Yarn能够在不同系统上保证无差异的工作

可以同时使用,yarn对npm进行了安装和提升,实现了npm大部分的命令和功能,两者都遵循package.json文件,所以不用担心兼容性问题,例如我们可以使用yarn安装或删除依赖,使用npm运行启动命令,打包命令等。