Skip to main content
On this page

deno install

deno install 会为您的项目安装依赖项并缓存它们。有关 Deno 如何处理模块的更多信息,请参阅 模块和依赖项

示例 Jump to heading

deno install Jump to heading

使用此命令安装在 deno.json 和/或 package.json 中定义的所有依赖项。

依赖项将被安装在全局缓存中,但如果您的项目有 package.json 文件,本地的 node_modules 目录也会被创建。

deno install [PACKAGES] Jump to heading

使用此命令安装特定的软件包,并将其添加到 deno.jsonpackage.json 中。

>_
deno install jsr:@std/testing npm:express

Tip

您也可以使用 deno add,它是 deno install [PACKAGES] 的别名。

如果您的项目有 package.json 文件,来自 npm 的软件包将被添加到 package.jsondependencies 中。否则,所有软件包将被添加到 deno.json 中。

deno install --entrypoint [FILES] Jump to heading

使用此命令安装提供文件及其依赖项中使用的所有依赖项。

如果您在代码中使用 jsr:npm:http:https: 指定符,并希望在部署项目之前缓存所有依赖项,这尤其有用。

main.js
import * as colors from "jsr:@std/fmt/colors";
import express from "npm:express";
>_
deno install -e main.js
Download jsr:@std/fmt
Download npm:express

Tip

如果您想设置本地的 node_modules 目录,可以传递 --node-modules-dir=auto 选项。

某些依赖项可能在没有本地 node_modules 目录的情况下无法正确工作。

deno install --global [PACKAGE_OR_URL] Jump to heading

使用此命令安装提供的软件包或脚本,使其作为系统中的全局可用二进制文件。

此命令创建一个瘦的可执行 shell 脚本,该脚本使用指定的 CLI 标志和主模块调用 deno。它被放置在安装根目录中。

示例:

>_
deno install --global --allow-net --allow-read jsr:@std/http/file-server
Download jsr:@std/http/file-server...

✅ Successfully installed file-server.
/Users/deno/.deno/bin/file-server

要更改可执行文件的名称,使用 -n/--name

>_
deno install -g -N -R -n serve jsr:@std/http/file-server

可执行文件名称默认推断:

  • 尝试获取 URL 路径的文件名根。这上面的示例将变为 'file-server'。
  • 如果文件名根是 'main'、'mod'、'index' 或 'cli' 这样的通用名称,并且路径没有父级,则采用父路径的文件名。否则,采用通用名称。
  • 如果结果名称有 '@...' 后缀,则去掉它。

要更改安装根目录,请使用 --root

>_
deno install -g -N -R --root /usr/local/bin jsr:@std/http/file-server

安装根目录的确定顺序如下:

  • --root 选项
  • DENO_INSTALL_ROOT 环境变量
  • $HOME/.deno/bin

如果需要,必须手动将这些添加到路径中。

>_
echo 'export PATH="$HOME/.deno/bin:$PATH"' >> ~/.bashrc

您必须在安装时指定运行脚本所需的权限。

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

上述命令创建一个名为 file_server 的可执行文件,该文件在网络和读取权限下运行,并绑定到端口 8080。

出于良好实践,请使用 import.meta.main 习语在可执行脚本中指定入口点。

示例:

// https://example.com/awesome/cli.ts
async function myAwesomeCli(): Promise<void> {
  // -- 省略 --
}

if (import.meta.main) {
  myAwesomeCli();
}

当您创建可执行脚本时,请确保通过向您的存储库添加示例安装命令来通知用户:

>_
# 使用 deno install 安装

deno install -n awesome_cli https://example.com/awesome/cli.ts

deno install --global --compile [PACKAGE_OR_URL] Jump to heading

使用此命令将软件包或脚本编译为独立的、自包含的二进制文件。生成的可执行文件可以分发并运行,而无需在目标系统上安装 Deno。

>_
deno install --global --compile -A npm:@anthropic-ai/claude-code

这结合了 deno compile 与全局安装的行为 —— 生成一个本地二进制文件,放置在安装根目录中(与不使用 --compile--global 行为相同)。

本机 Node.js 插件 Jump to heading

许多流行的 npm 软件包,如 npm:sqlite3npm:duckdb,依赖于 "生命周期脚本",例如 preinstallpostinstall 脚本。通常,运行这些脚本是软件包正常工作的必要条件。

与 npm 不同,Deno 默认不运行这些脚本,因为它们可能会带来安全漏洞。

您仍然可以通过在运行 deno install 时传递 --allow-scripts=<packages> 选项来运行这些脚本:

>_
deno install --allow-scripts=npm:sqlite3

安装所有依赖项,并允许 npm:sqlite3 软件包运行其生命周期脚本

--quiet 标志 Jump to heading

--quiet 标志在安装依赖项时抑制诊断输出。

deno install 一起使用时,它将隐藏进度指示器、下载信息和成功消息。

>_
deno install --quiet jsr:@std/http/file-server

这对于脚本环境或当您希望在 CI 管道中获得更清晰的输出时非常有用。

卸载 Jump to heading

您可以使用 deno uninstall 命令卸载依赖项或二进制脚本:

>_
deno uninstall express
Removed express
>_
deno uninstall -g file-server
deleted /Users/deno/.deno/bin/file-server
✅ Successfully uninstalled file-server

有关更多详细信息,请参见 deno uninstall 页面

Type checking options Jump to heading

--check Jump to heading

Set type-checking behavior. This subcommand type-checks local modules by default, so adding --check is redundant If the value of "all" is supplied, remote modules will be included. Alternatively, the 'deno check' subcommand can be used.

--no-check Jump to heading

Skip type-checking. If the value of "remote" is supplied, diagnostic errors from remote modules will be ignored.

Dependency management options Jump to heading

--cached-only Jump to heading

Require that remote dependencies are already cached.

--frozen Jump to heading

Error out if lockfile is out of date.

--import-map Jump to heading

Load import map file from local file or remote URL.

--lock Jump to heading

Check the specified lock file. (If value is not provided, defaults to "./deno.lock").

--no-lock Jump to heading

Disable auto discovery of the lock file.

--no-npm Jump to heading

Do not resolve npm modules.

--no-remote Jump to heading

Do not resolve remote modules.

--node-modules-dir Jump to heading

Sets the node modules management mode for npm packages.

--reload Jump to heading

Short flag: -r

Reload source code cache (recompile TypeScript) no value Reload everything jsr:@std/http/file-server,jsr:@std/assert/assert-equals Reloads specific modules npm: Reload all npm modules npm:chalk Reload specific npm module.

--vendor Jump to heading

Toggles local vendor folder usage for remote modules and a node_modules folder for npm packages.

Options Jump to heading

--allow-scripts Jump to heading

Allow running npm lifecycle scripts for the given packages Note: Scripts will only be executed when using a node_modules directory (--node-modules-dir).

--cert Jump to heading

Load certificate authority from PEM encoded file.

--compile Jump to heading

Install the script as a compiled executable.

--conditions Jump to heading

Use this argument to specify custom conditions for npm package exports. You can also use DENO_CONDITIONS env var. .

--config Jump to heading

Short flag: -c

Configure different aspects of deno including TypeScript, linting, and code formatting. Typically the configuration file will be called deno.json or deno.jsonc and automatically detected; in that case this flag is not necessary.

--dev Jump to heading

Short flag: -D

Add the package as a dev dependency. Note: This only applies when adding to a package.json file.

--entrypoint Jump to heading

Short flag: -e

Install dependents of the specified entrypoint(s).

--env-file Jump to heading

Load environment variables from local file Only the first environment variable with a given key is used. Existing process environment variables are not overwritten, so if variables with the same names already exist in the environment, their values will be preserved. Where multiple declarations for the same environment variable exist in your .env file, the first one encountered is applied. This is determined by the order of the files you pass as arguments.

--force Jump to heading

Short flag: -f

Forcefully overwrite existing installation.

--global Jump to heading

Short flag: -g

Install a package or script as a globally available executable.

--jsr Jump to heading

assume unprefixed package names are jsr packages.

--location Jump to heading

Value of globalThis.location used by some web APIs.

--lockfile-only Jump to heading

Install only updating the lockfile.

--minimum-dependency-age Jump to heading

(Unstable) The age in minutes, ISO-8601 duration or RFC3339 absolute timestamp (e.g. '120' for two hours, 'P2D' for two days, '2025-09-16' for cutoff date, '2025-09-16T12:00:00+00:00' for cutoff time, '0' to disable).

--name Jump to heading

Short flag: -n

Executable file name.

--no-config Jump to heading

Disable automatic loading of the configuration file.

--npm Jump to heading

assume unprefixed package names are npm packages.

--preload Jump to heading

A list of files that will be executed before the main module.

--require Jump to heading

A list of CommonJS modules that will be executed before the main module.

--root Jump to heading

Installation root.

--save-exact Jump to heading

Save exact version without the caret (^).

--seed Jump to heading

Set the random number generator seed.

--v8-flags Jump to heading

To see a list of all available flags use --v8-flags=--help Flags can also be set via the DENO_V8_FLAGS environment variable. Any flags set with this flag are appended after the DENO_V8_FLAGS environment variable.

Debugging options Jump to heading

--inspect Jump to heading

Activate inspector on host:port [default: 127.0.0.1:9229]. Host and port are optional. Using port 0 will assign a random free port.

--inspect-brk Jump to heading

Activate inspector on host:port, wait for debugger to connect and break at the start of user script.

--inspect-wait Jump to heading

Activate inspector on host:port and wait for debugger to connect before running user code.

Last updated on

你找到了你需要的东西吗?

编辑此页面
隐私政策