local-npm

Local and offline-first npm mirror
授权协议 Apache-2.0 License
开发语言 JavaScript
所属分类 Web应用开发、 常用JavaScript包
软件类型 开源软件
地区 不详
投 递 者 田硕
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

local-npm is a Node server that acts as a local npm registry. It serves modules, caches them, and updates them whenever they change. Basically it's a local mirror, but without having to replicate the entire npm registry.

This allows your npm install commands to (mostly) work offline. Also, they get faster and faster over time, as commonly-installed modules are aggressively cached.

Introduction

local-npm acts as a proxy between you and the main npm registry. You run npm install commands like normal, but under the hood, all requests are sent through the local server.

When you first npm install a module, it'll be fetched from the main npm registry. After that, the module and all its dependencies (at that version) are stored in a local database, so you can expect subsequent installs to be much faster.

The server will also listen for changes from the remote registry, so you can expect updates to a module's metadata to be replicated within seconds of being published. (I.e. you won't get stuck with old versions.)

If you're organizing a conference/meetup/whatever, you can also share this local server with multiple people. So if your teammates are constantly installing the same modules over and over again, this can save a lot of time in the long run.

local-npm is also a good way to make npm install work offline. Assuming new versions of a package haven't been published since you last installed, subsequent npm installs will all serve from the cache, without ever hitting a remote server.

Addy Osmani has a nice post comparing local-npm to other options.

Usage

If you're using OS X, take a look at local-npm-launch-agent, a one-liner that sets everything up automatically. Otherwise:

$ npm install -g local-npm

Then

$ local-npm

to start the server. (Note that it will write files in whatever directory you run it from.)

Then set npm to point to the local server:

$ npm set registry http://127.0.0.1:5080

To switch back, you can do:

$ npm set registry https://registry.npmjs.org

The same rules as for the npm Australia mirror apply here.

Listening for events

to listen to process level events to collecting metrics use the global event emitter to introspect on these - events can be one of the following (info, warn, missed, cached, request)

require('local-npm')({
    port: 5080,
    pouchPort: 6789,
    remote: 'https://registry.npmjs.org',
    remoteSkim: 'https://replicate.npmjs.com',
    url: 'http://127.0.0.1:5080',
    directory: './'
});
process.on('request', (msg) => {
  console.log(msg);
})

Command line options

Usage: local-npm [options]

Options:

	-h, --help                   output usage information
	-V, --version                output the version number
	-p, --port [port]            The port to run local-npm on
	-pp, --pouch-port [port]     The port to run the pouch db server on
	-l, --log-level [level]      The level to log information to the console from local-npm
	-r, --remote [url]           The registry to fallback information gathering and tars on
	-rs, --remote-skim [url]     The remote skimdb to sync couchdb information from
	-u, --url [url]              The default access url that local-npm will be hosted on
	-d, --directory [directory]  directory to store data

Protip: You can replicate from your friend's local-npm to your own local-npm by simply pointing at it:

$ local-npm \
   --remote http://<friends_hostname>:5080 \
   --remote-skim http://<friends_hostname>:16984/skimdb

While your friend does:

$ local-npm \
   --url http://<friends_hostname>:5080

In this way, you can create a daisy chain of awesome.

Protip 2: If you want to set up a single local-npm for multiple people to use, such as for conferences or workplaces, then just daemonize it (e.g. using forever), and then when you run it, specify the URL that clients will use to access the server, e.g.:

$ local-npm \
    --url http://192.168.x.x:5080

This will ensure that clients fetch tarballs from 192.168.x.x instead of 127.0.0.1.

If you want a GUI or don't want to run it from the command-line is an electron app for that! https://github.com/local-npm/local-npm-daemon

local-npm-daemon

Browser UI

A rudimentary npm-like UI that allows you to search modules and see their descriptions can be found at http://localhost:5080/_browse.

main package

If you haven't finished replicating the remote skimdb, then not all the modules will be visible yet.

Switching with npmrc

Features like npm search are currently unsupported. So to avoid having to remember URLs when switching back and forth, you can use npmrc like so (based on the instructions for the Australian mirror of npm):

$ npm install -g npmrc
$ npmrc -c local
$ npm set registry http://127.0.0.1:5080

then to search:

$ npmrc default

and to switch back:

$ npmrc local

Incidentally, though, local-npm does allow you to do npm login and npm publish. These commands will just be proxied to the main npm registry.

Speed test

For a speed test of local-npm versus regular npm, see these results.

How it works

npm is built on top of CouchDB, so local-npm works by replicating the full "skimdb" database to a local PouchDB Server. You can inspect the running database at http://127.0.0.1:16984/_utils. (Don't write to it!)

The entire "skimdb" (metadata) is replicated locally, but for the "fullfatdb" (metadata plus tarballs), only what you npm install is stored. To start from scratch, just delete whatever directory you started the server in.

CouchDB has a changes feed, so local-npm just listens to the skimdb changes to know when it needs to refresh an outdated module. Changes should replicate within a few seconds of being published. (You can watch this happen in realtime by reading the logs, which is kind of fun! An update comes in whenever someone publishes a module.)

Note that new tarballs aren't downloaded until you explicitly npm install them, though. So e.g. if you install v1.0.0 of a package, then v1.0.1 is published, and your range says ^1.0.0, then the next npm install will fail unless you're online and can fetch the new tarball.

Using with Docker

Using local-npm with Docker can help speed up build times during development.The awkward way to use this would be something like:

RUN local-npm && \
    npm set registry http://... && \
    npm set proxy .... && \
    npm install some packages etc ...

Running services inside of a Docker build command isn't really Docker's business. The friendly scenario works better in this case, i.e. you run local-npm on the host machine:

local-npm --url=http://<network host or ip>:5080

And adding the npm registry/proxy commands to your Dockerfile:

RUN npm config set proxy http://<network host or ip>:5080
RUN npm config set proxy http://<network host or ip>:5080
RUN npm config set registry http://<network host or ip>:5080
RUN npm config set strict-ssl false
RUN npm install project dependencies ...
  • 全局安装: npm的包安装分为本地安装(local)、全局安装(global)两种,从敲的命令行来看,差别只是有没有 -g 而已   安装方式:     npm i package-name -g  ||  npm install  package-name -global      参数 -g 代表安装到‘全局环境中   安装位置 :      包安装在Node安装目录下的node_module

  • ref: https://nodejs.dev/an-introduction-to-the-npm-package-manager 基本来自官网,链接如上。为了加深理解和印象稍作整理如下: 1. Node.js 1.1 Difference between Node.js and Browser Can use JS to write backend… In the browser, most

  • 参考:https://www.jianshu.com/p/f19ba506f664 看到 npm audit fix --force //强制安装最新的依赖项大概明白了上面问题的所在。 直接运行 --force 的行为不要学习,对于没能自动修复的问题,说明肯定出现了 SEMVER WARNING 之类的警告,这意味着推荐的修复版本存在让代码出问题的可能,主要发生在依赖包更改了 API 或者升级了大

  • npm ERR! code 1 npm ERR! path x:\xxxx\node_modules\node-sass npm ERR! com 解决方法如下: 第 一 步 : \color{#FF0000}{第一步:} 第一步:npm install -g yarn 全局安装yarn 第 二 步 : \color{#FF0000}{第二步:} 第二步:进入C:\Users\qw删除.yarnr

  • 报错信息1: 'webpack-dev-server' 不是内部或外部命令,也不是可运行的程序或批处理文件。 解决方法: npm install webpack-dev-server -g 报错信息2: npm ERR! cb() never called! c:\Users\lenovo\Desktop\M_pro\user-security>npm i element-ui -S npm WA

  • npm install --save(npm insatll -s) 和 npm install -d (安装包都是放在 ./node_modules 下,就是依赖配置写入package.json文件的位置不同) npm install -D 就是 npm install --save-dev npm insatll -S 就是npm install --save 我们在使用npm install

  • npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ERR! npm ERR! While resolving: shopcar-demo@0.1.0 npm ERR! Found: webpack@5.72.0 npm ERR! node_modules/webpack npm ERR!   peer webpack@"^

  • 模块 地址 备注 前置依赖 CentOS - 安装配置Node.js环境 后置依赖 CentOS - Node.js+Express框架连接MySQL 一、NPM 使用介绍 1. 介绍   NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题,常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用。 允许用户从NPM服务器下载并安装别人编

 相关资料
  • 描述 (Description) 此函数将LIST中的变量设置为当前执行块的本地变量。 如果指定了多个值,则必须使用括号来定义列表。 请注意,local创建变量的本地副本,然后在封闭块终止时超出范围。 无论何时访问本地化值,都会使用本地化值,包括该块期间使用的任何子例程和格式。 语法 (Syntax) 以下是此函数的简单语法 - local LIST 返回值 (Return Value) 此函数

  • 概要 <#local name=value> 或 <#local name1=value1 name2=value2 ... nameN=valueN> 或 <#local name> capture this </#local> 这里: name: 在root中局部对象的名称。它不是一个表达式。但它可以被写作是字符串形式, 如果变量名包含保留字符,这是很有用的,比如 <#local "

  • 设置修改器,将随后的事件导向这样一种情况:即事件数据仅广播给当前节点(当使用了Redis adapter)。 io.local.emit('an event', { some: 'data' });

  • If there are customizations you want to make to your site without distributing an entire extension, you can do so by using local extenders. Each Flarum installation comes with an extend.php file where

  • 根据ISO 8601编码的日期和时间(年,月,日,小时,分钟,秒,分数),没有时区信息。 例子 (Example) <!DOCTYPE HTML> <html> <body> <form action = "/cgi-bin/html5.cgi" method = "get"> Local Date and Time : <input type = "datet

  • We’ve supported local file picking and image uploading for quite some time already. However making these features functional required some effort on the user’s side, while not being immediately obviou