npm6分析

npm版本:6.12.1
缓存目录默认是~/.npm/_cacache, 有index-v5、content-v2、tmp子目录

以安装mkdirp为例分析, 找到下载的源码实际存储位置

  • 获取tarball地址

npm v mkdirp

dist
.tarball: http://npmprivate.xxx.com/mkdirp/-/mkdirp-0.5.1.tgz
.shasum: 30057438eac6cf7f8c4767f38648d6697d75c903
  • 在index-v5中查找,可以看到一些信息,但还无法定位到存储位置,有网友说根据_shasum去找,前4位是目录名,实际有些包对应不上

grep -r "http://npmprivate.xxx.com/mkdirp/-/mkdirp-0.5.1.tgz" ~/.npm/_cacache/index-v5

/Users/xuezhijie/.npm/_cacache/index-v5/9a/31/0d6e1c20753f4caddf9a0f3255ae29844dd5720b8033df6b095199c7da57:52a4f41b14df35db86640b933fda71742ec6d4e2 {"key":"make-fetch-happen:request-cache:h
  • content-v2存储了下载的包,可以通过cacache包去查看, 写个工具脚本
const cacache = require('cacache')
const fs = require('fs')

const cachePath = '/Users/xuezhijie/.npm/_cacache'

;(async function () {
  let data = await cacache.ls(cachePath)
  console.log(data)
  fs.writeFileSync('./cache.json', JSON.stringify(data))

  // await cacache.rm.all(cachePath)
  // console.log('removed');
})();

在生成的json文件找查找mkdirp

"make-fetch-happen:request-cache:http://npmprivate.xxx.com/mkdirp/-/mkdirp-0.5.1.tgz": {
    "key": "make-fetch-happen:request-cache:http://npmprivate.xxx.com/mkdirp/-/mkdirp-0.5.1.tgz",
    "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=",
    "path": "/Users/xuezhijie/.npm/_cacache/content-v2/sha1/30/05/7438eac6cf7f8c4767f38648d6697d75c903",
    "size": 4991,
    "time": 1576660727569,

path属性就是实际存储位置,验证下格式,发现是gzip,重名为tar.gz后解压就得到源码了

file /Users/xuezhijie/.npm/_cacache/content-v2/sha1/30/05/7438eac6cf7f8c4767f38648d6697d75c903

/Users/xuezhijie/.npm/_cacache/content-v2/sha1/30/05/7438eac6cf7f8c4767f38648d6697d75c903: gzip compressed data, from Unix, original size 38912

你可能感兴趣的:(npm6分析)