deno.com
在当前页面

deno repl,交互式脚本提示

命令行用法

o repl [选项] [-- [参数]...]

启动一个读取-求值-打印循环,允许你在全局上下文中交互式地构建程序状态。 它对于快速原型设计和检查代码片段特别有用。

支持 TypeScript,但不进行类型检查,仅进行转译。


不稳定选项 Jump to heading

--unstable Jump to heading

--unstable 标志已被弃用。请改用细粒度的 --unstable-* 标志.

--unstable-bare-node-builtins Jump to heading

启用不稳定的裸节点内置功能.

--unstable-broadcast-channel Jump to heading

启用不稳定的 BroadcastChannel API.

--unstable-cron Jump to heading

启用不稳定的 Deno.cron API.

--unstable-detect-cjs Jump to heading

在更多情况下将模棱两可的 .js、.jsx、.ts、.tsx 文件视为 CommonJS 模块.

--unstable-kv Jump to heading

启用不稳定的键值存储 API.

--unstable-net Jump to heading

启用不稳定的网络 API.

--unstable-node-globals Jump to heading

在所有地方暴露 Node 全局变量.

--unstable-sloppy-imports Jump to heading

启用通过扩展探测、.js 到 .ts 的转换以及目录探测来解析模块的不稳定功能.

--unstable-temporal Jump to heading

启用不稳定的 Temporal API.

--unstable-unsafe-proto Jump to heading

启用不安全的 proto 支持。这是一个安全风险。.

--unstable-webgpu Jump to heading

启用不稳定的 WebGPU API.

--unstable-worker-options Jump to heading

启用不稳定的 Web Worker API.

Options Jump to heading

--cert Jump to heading

从 PEM 编码文件加载证书颁发机构.

--config Jump to heading

Short flag: -c

配置 deno 的不同方面,包括 TypeScript、代码检查和代码格式化 通常配置文件将被称为 deno.jsondeno.jsonc 并自动检测;在这种情况下,此标志不是必需的。 文档:https://docs.deno.com/go/config.

--env-file Jump to heading

从本地文件加载环境变量 仅使用第一个具有给定键的环境变量。 现有的进程环境变量不会被覆盖,因此如果环境中已经存在同名的变量,它们的值将被保留。 如果 .env 文件中存在多个相同环境变量的声明,则应用第一个遇到的声明。这由你作为参数传递的文件的顺序决定。.

--eval Jump to heading

在 REPL 启动时评估提供的代码.

--eval-file Jump to heading

在 REPL 启动时将提供的文件作为脚本进行评估。接受文件路径和 URL.

--location Jump to heading

某些 web API 使用的 globalThis.location 的值.

--no-config Jump to heading

禁用自动加载配置文件.

--seed Jump to heading

设置随机数生成器种子.

--v8-flags Jump to heading

要查看所有可用标志的列表,请使用 --v8-flags=--help 标志也可以通过 DENO_V8_FLAGS 环境变量设置。 使用此标志设置的任何标志都会附加在 DENO_V8_FLAGS 环境变量之后.

依赖管理选项 Jump to heading

--cached-only Jump to heading

要求远程依赖项已缓存.

--frozen Jump to heading

如果锁文件过期则报错.

--import-map Jump to heading

从本地文件或远程 URL 加载导入映射文件 文档:https://docs.deno.com/runtime/manual/basics/import_maps.

--lock Jump to heading

检查指定的锁文件。(如果未提供值,则默认为 "./deno.lock").

--no-lock Jump to heading

禁用自动发现锁文件.

--no-npm Jump to heading

不解析 npm 模块.

--no-remote Jump to heading

不解析远程模块.

--node-modules-dir Jump to heading

为 npm 包设置 node 模块管理模式.

--reload Jump to heading

Short flag: -r

重新加载源代码缓存(重新编译 TypeScript) 无值 重新加载所有内容 jsr:@std/http/file-server,jsr:@std/assert/assert-equals 重新加载特定模块 npm: 重新加载所有 npm 模块 npm:chalk 重新加载特定 npm 模块.

--vendor Jump to heading

切换本地 vendor 文件夹用于远程模块和 node_modules 文件夹用于 npm 包.

调试选项 Jump to heading

--inspect Jump to heading

在主机:端口上激活检查器 [默认: 127.0.0.1:9229]

--inspect-brk Jump to heading

在主机:端口上激活检查器,等待调试器连接并在用户脚本开始时中断.

--inspect-wait Jump to heading

在主机:端口上激活检查器,并在运行用户代码之前等待调试器连接.

特殊变量 Jump to heading

REPL 提供了几个特殊变量,它们始终可用:

标识符 描述
_ 返回最后一个求值的表达式
_error 返回最后一个抛出的错误
Deno 1.14.3
使用 ctrl+d 或 close() 退出
> "hello world!"
"hello world!"
> _
"hello world!"
> const foo = "bar";
undefined
> _
undefined

特殊函数 Jump to heading

REPL 在全局作用域中提供了几个函数:

函数 描述
clear() 清除整个终端屏幕
close() 关闭当前的 REPL 会话

--eval 标志 Jump to heading

--eval 标志允许你在进入 REPL 之前在运行时运行一些代码。这对于导入你通常在 REPL 中使用的代码或以某种方式修改运行时非常有用:

$ deno repl --allow-net --eval 'import { assert } from "jsr:@std/assert@1"'
Deno 1.45.3
使用 ctrl+d、ctrl+c 或 close() 退出
> assert(true)
undefined
> assert(false)
Uncaught AssertionError
    at assert (https://jsr.io/@std/assert/1.0.0/assert.ts:21:11)
    at :1:22

--eval-file 标志 Jump to heading

--eval-file 标志允许你在进入 REPL 之前从指定的文件中运行代码。与 --eval 标志类似,这对于导入你通常在 REPL 中使用的代码或以某种方式修改运行时非常有用。

文件可以指定为路径或 URL。URL 文件会被缓存,并且可以通过 --reload 标志重新加载。

如果同时指定了 --eval,则 --eval-file 文件会在 --eval 代码之前运行。

$ deno repl --eval-file=https://docs.denohub.com/examples/welcome.ts,https://docs.denohub.com/examples/local.ts
下载 https://docs.denohub.com/examples/welcome.ts
欢迎使用 Deno!
下载 https://docs.denohub.com/examples/local.ts
Deno 1.45.3
使用 ctrl+d 或 close() 退出
> local // 这个变量在 local.ts 中定义,但未导出
"这是一个在 local.ts 中的局部变量"

相对导入路径解析 Jump to heading

如果 --eval-file 指定的代码文件包含相对导入,则运行时会尝试相对于当前工作目录解析导入。它不会尝试相对于代码文件的位置解析。这在使用 --eval-file 与模块文件时可能会导致“模块未找到”错误:

$ deno repl --eval-file=https://jsr.io/@std/encoding/1.0.0/ascii85.ts
在 --eval-file 文件 https://jsr.io/@std/encoding/1.0.0/ascii85.ts 中出错。Uncaught TypeError: 未找到模块 "file:///home/_validate_binary_like.ts"。
    at async :2:13
Deno 1.45.3
使用 ctrl+d 或 close() 退出
>

Tab 补全 Jump to heading

Tab 补全是 REPL 中快速导航的关键功能。按下 tab 键后,Deno 将显示所有可能的补全列表。

$ deno repl
Deno 1.45.3
使用 ctrl+d 或 close() 退出
> Deno.read
readTextFile      readFile          readDirSync       readLinkSync      readAll           read
readTextFileSync  readFileSync      readDir           readLink          readAllSync       readSync

键盘快捷键 Jump to heading

按键 操作
Ctrl-A, Home 将光标移动到行首
Ctrl-B, Left 将光标向左移动一个字符
Ctrl-C 中断并取消当前编辑
Ctrl-D 如果行为空,则发送行结束信号
Ctrl-D, Del 如果行不为空,则删除光标下的字符
Ctrl-E, End 将光标移动到行尾
Ctrl-F, Right 将光标向右移动一个字符
Ctrl-H, Backspace 删除光标前的字符
Ctrl-I, Tab 下一个补全
Ctrl-J, Ctrl-M, Enter 完成行输入
Ctrl-K 从光标删除到行尾
Ctrl-L 清屏
Ctrl-N, Down 历史记录中的下一个匹配
Ctrl-P, Up 历史记录中的上一个匹配
Ctrl-R 反向搜索历史记录(Ctrl-S 向前,Ctrl-G 取消)
Ctrl-T 交换前一个字符与当前字符
Ctrl-U 从行首删除到光标
Ctrl-V 插入任何特殊字符而不执行其关联的操作
Ctrl-W 删除光标前的单词(使用空格作为单词边界)
Ctrl-X Ctrl-U 撤销
Ctrl-Y 从 Yank 缓冲区粘贴
Ctrl-Y 从 Yank 缓冲区粘贴(Meta-Y 粘贴下一个 Yank)
Ctrl-Z 挂起(仅限 Unix)
Ctrl-_ 撤销
Meta-0, 1, ..., - 指定参数的位数。 开始一个负参数。
Meta < 移动到历史记录中的第一个条目
Meta > 移动到历史记录中的最后一个条目
Meta-B, Alt-Left 将光标移动到前一个单词
Meta-Backspace 从当前单词的开头删除,或者,如果在单词之间,则删除到前一个单词的开头
Meta-C 将当前单词大写
Meta-D 向前删除一个单词
Meta-F, Alt-Right 将光标移动到下一个单词
Meta-L 将下一个单词小写
Meta-T 交换单词
Meta-U 将下一个单词大写
Meta-Y 参见 Ctrl-Y
Ctrl-S 插入新行

DENO_REPL_HISTORY Jump to heading

你可以使用 DENO_REPL_HISTORY 环境变量来控制 Deno 存储 REPL 历史文件的位置。你可以将其设置为空值,Deno 将不会存储历史文件。

你找到需要的内容了吗?

隐私政策