NPM本地缓存方案

NPM本地缓存方案

如果你被前一篇《centos下搭建npm镜像》搞晕了搞挫败了搞得萎了,毕竟完整镜像npm几十G的内容需要大量的时间,期间可能遇到各种网络问题(由于某些大家都知道的原因),或者像这位哥们吐槽官方教程不给力(https://github.com/npm/npmjs.org/issues/106),你只是想简单架个本地服务而不具备太多系统或程序相关的知识,无法一下子处理各种奇葩的异常,那么你应该继续看下去。

NPM本地缓存?

在天朝网络环境下做nodejs相关开发,在我看来最痛的点就是使用npm的时候各种依赖包下载超级慢和各种异常,所以我关注的几个点:

1,下载依赖速度要快

2,不会因为npm官方镜像挂掉而影响开发

3,私有模块管理

当然,你可以常年挂着VPN,你可以找几个国内靠谱的镜像出现问题的时候切换过去,你可以内部管理模块然后npm link,就可以解决上面的问题,在这之外不妨动手自己架设一个本地服务。

找了一圈,我发现了两个cache模块:https://github.com/mixu/npm_lazyhttps://github.com/rlidwka/sinopia,这两个模块的实现彻底消除了之前完整镜像npm官方的痛,几乎是零配置,他们基本的思路基本一致:在本地运行一个服务器实例,初始化一个空的“仓库”,我们无需关心它是什么样的仓库,当用户向本地服务器发起请求时,先检查本地是否有现成的已更新的包,有则从本地仓库返回,无则从官方或指定的镜像下载请求所需要的包缓存到本地并返回给用户。从某种程度上,该逻辑解决了我们前面提到的两个问题大多数情况,因为如果你使用一个从来没使用过的模块就还是有问题。关于私有模块,sinopia提供了自己的解决方案,私有模块都建议以“local-”开头进行发布,npm_lazy则没有提及。

npm_lazy的使用

作为演示,我们选择npm_lazy快速的架设一个本地npm cache服务。

测试环境:

Shell

localhost# node -v
10.21
localhost# npm -v
1.3.11
localhost# lsb_release -a
Versionamd64noarchgraphicsamd64graphicsgraphicsnoarchprintingamd64printingprintingnoarch
Distributor RedHatEnterpriseServer
DescriptionEnterprise Linux Server releaseTikanga
Release
CodenameTikanga

和其他npm模块一样,你只需一条命令,全局安装即可:

Shell

installnpm_lazy

然后在终端里执行npm_lazy服务就跑起来了,是不是很简单→_→,有一点需要注意,如果你是要在内网供大家使用的,而不是本地自己用,需要修改一下配置,让它监听所有ip的请求。

Shell

localhost# cd /usr/local/lib/node_modules/npm_lazy
localhost npm_lazy
total
drwxrnobody
nobody config
config.yaml
nobody index
drwxrnobody
nobody Makefile
drwxrnobody node_modules
nobody package.json
nobody readme
nobody server
drwxrstorage
drwxrnobody
nobody verify

修改后的配置文件config.js如下:

JavaScript

require'path'
homePathnormalizeprocessprocessplatform'win32''USERPROFILE''HOME'
moduleexports
// Cache config
// Directory to store cached packages.
// Since any relative path is resolved relative to the current working
// directory when the server is started, you should use a full path.
cacheDirectoryhomePath'/.npm_lazy'
// maximum age before an index is refreshed from remoteUrl
// negative value means no refresh
cacheAge
// Request config
// max milliseconds to wait for each HTTP response
httpTimeout10000
// maximum number of retries per HTTP resource to get
maxRetries
// whether or not HTTPS requests are checked against Node's list of CAs
// set false if you are using your own npm mirror with a self-signed SSL cert
rejectUnauthorized
// Remote and local URL
// external url to npm_lazy, no trailing /
externalUrl'http://x.x.x.x:8888'
// registry url with trailing /
remoteUrl'http://registry.npmjs.org/'
// bind port and host
'x.x.x.x'

你需要修改的配置就是把我示例里是x.x.x.x换成你的服务器的内网ip即可,注意externalUrl和port的端口保持一致,externalUrl最后不要以/结尾,其他字段根据自己的需求调整,比如缓存时间,失败重连次数和外网镜像等。服务跑起来后,你需要设置一下你工作机的npm,将它的镜像地址改成你自己的内网服务器就可以开始使用了。

Shell

config registry

正常使用npm intall安装

npm install

sinopia的安装过程也是一样的,顺便把配置晒一下:

Shell

localhost # cd /usr/local/lib/node_modules/sinopia/lib
localhost
total
nobody
nobody config_def.yaml
nobody config_gen
nobody config
config.yaml
nobody error
nobody 10948index
nobody local
nobody 13305localstorage
nobody logger
nobody middleware
nobody npmsslkeys
nobody status
drwxrstorage
nobody 10621storage
nobody streams
nobody storage
nobody utils
localhost # cat config.yaml
# path to a directory with all packages
storagestorage
# a list of users
users
admin
# crypto.createHash('sha1').update(pass).digest('hex')
password'这里的密码hash是通过上面一行的命令在node控制台获得,将pass改成你的密码明文字符串,执行得到hash'
# a list of other known repositories we can talk to
uplinks
npmjs
httpsregistry.npmjs
# amount of time (in milliseconds) to wait for repository to respond
# before giving up and use the local cached copy
#timeout: 30000
# maximum time (in seconds) in which data is considered up to date
# default is 2 minutes, so server won't request the same data from
# uplink if a similar request was made less than 2 minutes ago
maxage
packages
# uncomment this for packages with "local-" prefix to be available
# for admin only, it's a recommended way of handling private packages
'local-*'
allow_accessadmin
allow_publishadmin
# you can override storage directory for a group of packages this way:
storage'local_storage'
# allow all users to read packages ('all' is a keyword)
# this includes non-authenticated users
allow_access
# allow 'admin' to publish packages
allow_publishadmin
# if package is not available locally, proxy requests to 'npmjs' registry
proxynpmjs
#####################################################################
# Advanced settings
#####################################################################
# if you use nginx with custom path, use this to override links
#url_prefix: https://dev.company.local/sinopia/
# you can specify listen address (or simply a port)
listen0.0.0.0
# type: file | stdout | stderr
# level: trace | debug | info | http (default) | warn | error | fatal
# parameters for file: name is filename
#  {type: 'file', path: 'sinopia.log', level: 'debug'},
# parameters for stdout and stderr: format: json | pretty
#  {type: 'stdout', format: 'pretty', level: 'debug'},
stdoutformatprettylevel
#- {type: file, path: sinopia.log, level: info}
# you can specify proxy used with all requests in wget-like manner here
# (or set up ENV variables with the same name)
#http_proxy: http://something.local/
#https_proxy: https://something.local/
#no_proxy: localhost,127.0.0.1
# maximum size of uploaded json document
# increase it if you have "request entity too large" errors
#max_body_size: 1mb

上述配置config.yaml是通过config_def.yaml复制改名得到的,listen的值改为0.0.0.0:4873用于监听所有ip的请求,其他配置根据需求自己定制。

npm_lazy功能相对简单,关于sinopia更多的配置信息,注意事项,以及其他类似功能的包可以看https://github.com/rlidwka/sinopia

cache, nodejs, npm, server

使用Yeoman定制前端开发项目构建工具 Nginx多重代理: 413 Request Entity Too Large

Comments are currently closed.

此条目发表在未分类分类目录,贴了标签。将固定链接加入收藏夹。