Skip to main content

快速开始

写一个配置

🌐 Write a config

pnpr 通过声明的注册表路由软件包:每个来源——本地托管的注册表,像 npmjs 这样的上游——都被明确声明,并声明它提供的软件包名称,路由将每个名称解析到其中的唯一一个。将此保存为 pnpr.yaml

🌐 pnpr routes packages through declared registries: every origin — a locally-hosted registry, an upstream like npmjs — is declared explicitly and claims the package names it serves, and a router resolves each name to exactly one of them. Save this as pnpr.yaml:

storage: ./storage

auth:
htpasswd:
file: ./htpasswd
# Allow one user to self-register, for the publish step below.
max_users: 1

registries:
# Packages published to this server. The `packages:` map is this
# registry's namespace: only these names can live here.
local:
type: hosted
packages:
'@mycompany/*':
publish: $authenticated

# The public npm registry. No `packages:` map — it serves every name,
# so it must be the last source in the router below.
npmjs:
type: upstream
url: https://registry.npmjs.org/
public: true

# A package resolves to the first source that claims its name:
# your scope goes to the local registry, everything else to npm.
main:
type: router
sources: [local, npmjs]

# The bare server URL serves the router.
defaultRegistry: main

@mycompany 替换为你自己的作用域。注册表之间没有跳转:未在本地发布的 @mycompany/* 包将是明确的 404,而不会向 npmjs 发出请求——这正是防止依赖混淆的关键。

🌐 Replace @mycompany with your own scope. There is no fall-through between registries: a @mycompany/* package that isn't published locally is a definitive 404, never a request to npmjs — which is exactly what closes dependency confusion.

启动服务器

🌐 Start the server

pnpr -c ./pnpr.yaml

它监听 127.0.0.1:7677。(直接运行 pnpr 不带配置也可以,但打包的默认配置是为 pnpm 自己的测试注册表设计的——对于任何真实用途,请使用你自己的配置。)

🌐 It listens on 127.0.0.1:7677. (Running plain pnpr with no config works too, but the bundled default config is shaped for pnpm's own test registry — for anything real, pass your own config.)

指向客户

🌐 Point a client at it

将 pnpm(或 npm/yarn)配置为使用本地服务器作为其注册表:

🌐 Configure pnpm (or npm/yarn) to use the local server as its registry:

pnpm config set registry http://127.0.0.1:7677/

现在 pnpm install 通过 pnpr 获取软件包。来自上游代理的软件包会被缓存,因此后续安装将从本地提供。

🌐 Now pnpm install fetches packages through pnpr. Packages proxied from the upstream are cached, so subsequent installs are served locally.

每个注册表也可以直接在 http://127.0.0.1:7677/~<name>/ 访问 —— 例如,你可以仅将你的作用域指向托管注册表,而不是使用路由。

发布一个包

🌐 Publish a package

创建一个用户并发布:

🌐 Create a user and publish:

pnpm login --registry http://127.0.0.1:7677/
pnpm publish --registry http://127.0.0.1:7677/

发布通过路由路由到 local 托管的注册表——发布一个解析到上游的包,或其名称没有任何注册表认领的包,将被拒绝。

🌐 The publish routes through the router to the local hosted registry — publishing a package that resolves to an upstream, or whose name no registry claims, is rejected.

接下来去哪里

🌐 Where to go next