#5 Node 包管理器

2021-12-14

Node 包都存储在 Registry 中,官方 Registry 是 npmjs.org

切换源

npm config set registry https://registry.npm.taobao.org

或者直接修改 ~/.npmrc, 加入:

registry = https://registry.npm.taobao.org

下载的时候也可以指定:

npm info lodash --registry https://registry.npm.taobao.org

包管理器

  • npm download
  • yarn download
    Facebook 出的, 对 npm 做了一些优化,npm 后面也做了类似的优化
  • cnpm download
    淘宝出的,主要是默认使用淘宝自己维护的国内镜像
  • pnpm stars downloads
  • entropic stars downloads
    据说是 NPM 前 CTO 成立的新项目,目标是去中心化

包依赖的语法

  • ~ 匹配最近的小版本, eg: ~1.2.3 会匹配 1.2.x
  • ^ 匹配最新的大版本,eg: ^1.2.3 会匹配所有 1.x.x
  • * 永远最新版本

参考资料与拓展阅读

#4 Node Socket 编程 EMFILE 错误的一种情况

2021-01-31

本月,听了同事做的一次技术分享,觉得很有意思的问题,我在这里隐去业务细节,只保留技术部分,做个总结归纳,最后用一个业务无关的脚本来模拟一下这个过程。
同事的分享就好比是先逐步实验发现一个现象,然后研究出他背后的原理是什么。我在这里作为事后的归纳总结,就直接冲着背后的原理说了。

#3 npm 的 update-notifier

2019-04-17

线上某些基于 nodejs 的服务时不时看到这种进程,而且一直都在:

root     25997     1  0 Mar25 ?        00:00:00 /root/.nvm/versions/node/v10.14.2/bin/node /root/.nvm/versions/node/v10.14.2/lib/node_modules/npm/node_modules/update-notifier/check.js {"pkg":{"name":"npm","version":"6.4.1"}}

经过检查判断,这是 npm 带来的一个依赖模块,用来做版本检测。

#2 配置 Node 环境

2018-04-27
sudo apt install -y nodejs npm
# 已经不需要这句了:
# sudo ln -s `which nodejs` /usr/bin/node

# node -v
# npm -v

npm config set registry=https://registry.npm.taobao.org
sudo npm upgrade -g npm

# sudo npm install -g yarn --registry=https://registry.npm.taobao.org
curl --compressed -o- -L https://yarnpkg.com/install.sh | bash

yarn config set registry https://registry.npm.taobao.org

配置

echo registry=https://registry.npm.taobao.org > ~/.npmrc

npm config get registry
https://registry.npmjs.org/
npm config set registry=https://registry.npm.taobao.org

yarn config get registry
https://registry.yarnpkg.com
yarn config set registry https://registry.npm.taobao.org

#1 JS: split 方法

2017-02-12
"ni wo ta".split(" ");
// [ 'ni', 'wo', 'ta' ]

"ni wo ta".split(" ", 1);
// [ 'ni' ]
"ni wo ta".split(" ", 2);
// [ 'ni', 'wo' ]
"ni wo ta".split(" ", 3);
// [ 'ni', 'wo', 'ta' ]
"ni wo ta".split(" ", 4);
// [ 'ni', 'wo', 'ta' ]

"ni wo ta".split(":");
// [ 'ni wo ta' ]
"ni wo ta".split(":", 1);
// [ 'ni wo ta' ]
"ni wo ta".split(":", 2);
// [ 'ni wo ta' ]

如果要一刀将字符串切两半:

var line = "a : b : c";
var part1 = line.split(":", 1)[0];
if (a !== line) {
  var a = part1.trim();
  var b = line.substr(part1.length + 1).trim();
  console.log([a, b]);
}
var line = "a : b : c";
var index = line.indexOf(":");
if (index != -1) {
  var a = line.substr(0, index).trim();
  var b = line.substr(index + 1).trim();
  console.log([a, b]);
}

参考资料与拓展阅读