DC娱乐网

2025 年 Deno 终于官宣 pnpm 和 Yarn 可使用 JSR?

大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进

大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发!

2025 年 4 月 25 日 Deno 正式官宣,开发者可以使用 pnpm 和 Yarn 直接安装 JSR(JavaScript Registry)上的包,同时也支持安装任何带有 JSR 依赖项的 npm 包,且开发者也可以将带有 JSR 依赖项的包发布到 npm。

本文将带着大家一起了解下 JSR。

1.什么是 JSR

JSR 是现代 JavaScript 和 TypeScript 的开源软件包注册表。

The open-source package registry for modern JavaScript and TypeScript

JSR 的典型特征包括:

专为 TypeScript 和 ESM 打造:开发者可以发布 TypeScript 源代码,JSR 负责生成 API 文档、.d.ts 文件,并转译代码以实现跨运行时兼容性。JSR 软件包以符合 Web 标准的 ECMAScript 模块形式分发。基于 npm 构建 JSR: JSR 并非 npm 仓库的替代品,而是 npm 的超集。 JSR 模块可以与任何 JavaScript 包管理器一起使用,也可以在任何包含 node_modules 文件夹的项目中使用兼容任何运行时:JSR 模块可用于 Node.js、Deno、Bun、Cloudflare Workers 等,模块作者可以依赖强类型模块提供的强大编辑器支持,无需手动转译和分发类型2.在 pnpm 中使用 JSR

从 pnpm v10.9 开始,已经原生支持安装 JSR 包:

pnpm add jsr:<scope>/<pkg_name>// Or with version rangepnpm add jsr:<scope>/<pkg_name>@<range>

该命令将自动将以下内容添加到 package.json 中:

{ "dependencies": { "@<scope>/<pkg_name>": "jsr:^0.1.2" }}3.在 Yarn 中使用 JSR

从 Yarn v4.9.0 开始,开发者可以使用以下命令安装 JSR 包:

yarn add jsr:<scope>/<pkg_name>@<version>// Or with version rangeyarn add jsr:<scope>/<pkg_name>@<range>

与 pnpm 类似,此命令将自动将下面内容更新到 package.json:

{ "dependencies": { "@<scope>/<pkg_name>": "jsr:^0.1.2" }}4.在 Deno 中使用 JSRdeno add jsr:@luca/cases

添加包后,开发者可以用下面在 ES 模块中导入和使用:

import { camelCase } from "@luca/cases";camelCase("hello world"); // "helloWorld"

在 Deno 中,开发者可以选择使用 JSR 包,而无需使用 jsr: 说明符和 Deno 对 JSR 的原生支持进行安装。

import { camelCase } from "jsr:@luca/cases@1";camelCase("hello world"); // "helloWorld"5.发布 JSR 包

JSR 包使用 jsr publish / deno publish 命令发布,开发者可以选择从本机或 CI 中发布包。JSR 包使用 JavaScript 或 TypeScript 编写,并以 ES 模块的形式发布。

// mod.ts/** * Greet a person. * * @param name The name of the person to greet. */export function greet(name: string) { console.log(`Hello, ${name}!`);}

接着向包添加配置文件,包括:包的元数据,例如名称、版本和入口点。exports 字段告诉 JSR 哪些模块应该被包的用户导入。

// jsr.json / deno.json(c){ "name": "@luca/greet", "version": "1.0.0", "exports": "./mod.ts"}

最后运行 npx jsr publish 或 deno publish 来发布包。系统将提示使用 JSR 进行身份验证,通过后包就会被正常发布。

$ npx jsr publishVisit https://jsr.io/auth?code=ABCD-EFGH to authorize publishing of @luca/greetWaiting...Authorization successful. Authenticated as Luca CasonatoPublishing @luca/greet@1.0.0 ...Successfully published @luca/greet@1.0.0Visit https://jsr.io/@luca/greet@1.0.0 for details参考资料

https://github.com/jsr-io/jsr

https://jsr.io/docs/introduction