deno

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Deno

Deno

Use Deno as the default runtime/tooling when the repo contains
deno.json
/
deno.jsonc
, uses
deno.lock
, or scripts/documentation reference
deno task
,
deno run
,
deno test
, etc.
当代码仓库包含
deno.json
/
deno.jsonc
、使用
deno.lock
,或者脚本/文档提及
deno task
deno run
deno test
等内容时,将Deno作为默认运行时/工具使用。

Quick decision rules

快速决策规则

  • Prefer
    deno task <name>
    if the repo defines tasks.
  • Use
    deno add
    /
    deno remove
    to manage dependencies (writes to config).
  • Be explicit about permissions for
    deno run
    /
    deno test
    .
  • 如果代码仓库定义了任务,优先使用
    deno task <name>
  • 使用
    deno add
    /
    deno remove
    管理依赖(会写入配置文件)。
  • deno run
    /
    deno test
    明确指定权限。

Common operations

常用操作

Initialize a new project

初始化新项目

bash
deno init
bash
deno init

Add dependencies (JSR and npm)

添加依赖(JSR和npm)

bash
undefined
bash
undefined

JSR (recommended for Deno-first packages)

JSR(推荐优先用于Deno原生包)

deno add jsr:@std/path
deno add jsr:@std/path

npm packages are supported too

也支持npm包

deno add npm:react
deno add npm:react

multiple at once

一次性添加多个

deno add jsr:@std/assert npm:chalk
undefined
deno add jsr:@std/assert npm:chalk
undefined

Remove dependencies

移除依赖

bash
deno remove jsr:@std/path
bash
deno remove jsr:@std/path

Run a script

运行脚本

bash
undefined
bash
undefined

Minimal permissions: only what the program needs

最小权限原则:仅授予程序所需的权限

Examples:

示例:

--allow-net=api.example.com

--allow-net=api.example.com

--allow-read=./data

--allow-read=./data

--allow-env=FOO,BAR

--allow-env=FOO,BAR

deno run --allow-net --allow-read main.ts
undefined
deno run --allow-net --allow-read main.ts
undefined

Run tasks

运行任务

bash
undefined
bash
undefined

list tasks

列出所有任务

deno task
deno task

run a task defined in deno.json/deno.jsonc

运行在deno.json/deno.jsonc中定义的任务

deno task dev
undefined
deno task dev
undefined

Formatting, linting, testing

格式化、代码检查、测试

bash
deno fmt
deno lint
deno test
bash
deno fmt
deno lint
deno test

common permissioned test run

常用带权限的测试运行命令

deno test --allow-net --allow-read
undefined
deno test --allow-net --allow-read
undefined

Install / run CLIs

安装/运行CLI

bash
undefined
bash
undefined

Run a JSR or npm package's CLI without installing globally

无需全局安装即可运行JSR或npm包的CLI

deno x jsr:@std/http/file-server -p 8080
deno x jsr:@std/http/file-server -p 8080

Install globally (requires choosing permissions at install time)

全局安装(安装时需要选择权限)

Prefer the smallest set of permissions; avoid blanket flags unless necessary.

推荐使用最小权限集;除非必要,避免使用笼统的权限标志

deno install -g -N -R jsr:@std/http/file-server -- -p 8080
undefined
deno install -g -N -R jsr:@std/http/file-server -- -p 8080
undefined

Notes / pitfalls

注意事项/常见陷阱

  • Deno is secure-by-default: missing permissions cause runtime errors; add the smallest set of
    --allow-*
    flags needed.
  • Dependency specifiers:
    • jsr:
      for JSR registry packages
    • npm:
      for npm packages
    • URL imports are also supported (and cached)
  • Lockfile:
    deno.lock
    helps ensure reproducible dependency resolution.
  • Deno默认安全:缺少权限会导致运行时错误,请添加所需的最小
    --allow-*
    标志集。
  • 依赖说明符:
    • jsr:
      对应JSR registry的包
    • npm:
      对应npm包
    • 也支持URL导入(且会被缓存)
  • 锁文件:
    deno.lock
    用于确保依赖解析的可复现性。