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

Deno 2.8

不带前缀的软件包名称默认会被视为 npm 软件包,因此在 CLI 中不再需要 npm: 前缀。deno install express 等同于 deno install npm:express。JSR 软件包仍然需要 jsr: 前缀以避免歧义。npm: 前缀在 import 指定符中仍然是必需的。

Tip

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

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

deno install --os and --arch Jump to heading

从 Deno 2.8 开始,deno install 接受 --os--arch 标志,因此您 可以安装面向与当前运行平台不同的平台的 npm 软件包。这对于预先安装带有原生二进制文件的 软件包最有用——例如,在 macOS 开发机上构建一个部署产物, 最终将运行在 Linux/arm64 上。

这些标志接受与 Node.js 兼容的值,也就是 process.platformprocess.arch 生成的相同字符串。

>_
# 为 linux/arm64 安装 npm 软件包
deno install --os linux --arch arm64

# 为 windows/x64 安装
deno install --os win32 --arch x64

# 仅覆盖架构;--os 默认为当前系统
deno install --arch x64

--os--arch 仅适用于本地安装,并且与 --global 冲突。

deno install --package-json Jump to heading

默认情况下,Deno 会根据哪个配置文件离当前工作目录更近来选择要写入的配置文件(deno.jsonpackage.json)。从 Deno 2.8 开始,--package-json 会强制将依赖项写入 package.json,而不管附近是否存在 deno.json。如果还不存在 package.json ,则会创建一个。

>_
deno install --package-json npm:express jsr:@std/path

使用 --package-json 添加的 JSR 软件包会以其与 npm 兼容的 形式(npm:@jsr/...)写入。相同的标志也适用于 deno adddeno removedeno uninstall

要在不每次都传递该标志的情况下将此行为设为默认值,请在 deno.json 中设置 "preferPackageJson": true

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'、modindexcli 这样的通用名称,并且路径没有父级,则采用父路径的文件名。否则,采用通用名称。
  • 如果结果名称有 @... 后缀,则去掉它。

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

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

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

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

可执行文件会放置在安装根目录的 bin 子目录中,除非根路径本身已经以 bin 结尾,在这种情况下将直接使用该路径。 如果需要,生成的目录必须手动添加到 path 中。

>_
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 行为相同)。

deno install --prod Jump to heading

使用此命令仅安装生产依赖项,跳过 package.json 中的 devDependencies

>_
deno install --prod

这在部署应用程序时非常有用,因为此时并不需要测试框架或构建工具之类的开发依赖项。

--prod 标志与 --global--dev 冲突。

在 CI 环境中,建议使用 deno ci --prod,它 还会强制使用冻结的锁文件,并在安装前移除任何已存在的 node_modules

--skip-types Jump to heading

当与 --prod 结合使用时,--skip-types 标志还会跳过 @types/* 软件包,这些软件包既包括 package.json 依赖项中的,也包括 deno.json 导入中的:

>_
deno install --prod --skip-types

Caution

--skip-types 标志通过检查软件包名称是否 以 @types/ 开头来识别类型包。这种启发式方法可能无法覆盖所有仅类型包。

--prod with --entrypoint Jump to heading

--prod--entrypoint 结合使用时,模块图会以 “仅代码”方式构建,这将排除仅类型依赖项:

>_
deno install --prod --entrypoint main.ts

这提供了最精确的生产安装——只有指定入口点(以及其传递导入)在运行时实际导入的 依赖项才会被安装。

Native Node.js Add-ons Jump to heading

A lot of popular npm packages like npm:sqlite3 or npm:duckdb depend on "lifecycle scripts", eg. preinstall or postinstall scripts. Most often running these scripts is required for a package to work correctly.

Unlike npm, Deno does not run these scripts by default, because they may introduce security vulnerabilities.

You can still run these scripts by passing the --allow-scripts=<packages> option when running deno install:

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

Install all dependencies and allow the npm:sqlite3 package to run its lifecycle scripts.

--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 页面

Command line usage:
deno install [OPTIONS] [cmd]... [-- [SCRIPT_ARG]...]

Installs dependencies either in the local project or globally to a bin directory.

Local installation Jump to heading

Add dependencies to the local project's configuration (deno.json / package.json) and installs them in the package cache. If no dependency is specified, installs all dependencies listed in the config file. If the --entrypoint flag is passed, installs the dependencies of the specified entrypoint(s).

deno install
deno install express
deno install jsr:@std/bytes
deno install --entrypoint entry1.ts entry2.ts

Global installation Jump to heading

If the --global flag is set, installs a script as an executable in the installation root's bin directory.

deno install --global --allow-net --allow-read jsr:@std/http/file-server
deno install -g https://examples.deno.land/color-logging.ts

To change the executable name, use -n/--name:

deno install -g --allow-net --allow-read -n serve jsr:@std/http/file-server

The executable name is inferred by default:

  • Attempt to take the file stem of the URL path. The above example would become file_server.
  • If the file stem is something generic like main, mod, index or cli, and the path has no parent, take the file name of the parent path. Otherwise settle with the generic name.
  • If the resulting name has an @... suffix, strip it.

To change the installation root, use --root:

deno install -g --allow-net --allow-read --root /usr/local jsr:@std/http/file-server

The installation root is determined, in order of precedence:

  • --root option
  • DENO_INSTALL_ROOT environment variable
  • $HOME/.deno

These must be added to the path manually if required.

Type checking options Jump to heading

--check<CHECK_TYPE>optional
Jump to heading

Set type-checking behavior. This subcommand type-checks local modules by default, so passing --check is redundant; pass --check=all to also type-check remote modules. Alternatively, use the 'deno check' subcommand.

--no-check<NO_CHECK_TYPE>optional
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<BOOLEAN>optional
Jump to heading

Error out if lockfile is out of date.

Load import map file from local file or remote URL.

--lock<FILE>optional
Jump to heading

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

Disable auto discovery of the lock file.

Do not resolve npm modules.

--no-remote
Jump to heading

Do not resolve remote modules.

--node-modules-dir<MODE>optional
Jump to heading

Selects the node_modules directory mode for npm packages (not a path). One of: auto (create a local node_modules directory and install npm packages into it), manual (use the existing local node_modules directory, do not modify it), none (do not use a local node_modules directory; resolve npm packages from the global cache). Defaults to auto when the flag is passed without a value.

--node-modules-linker<MODE>
Jump to heading

Sets the linker mode for npm packages (isolated or hoisted).

--reload, -r<CACHE_BLOCKLIST>optional
Jump to heading

Reload source code cache (recompile TypeScript). With no value, reloads everything. Pass a comma-separated list of specifiers to reload only those modules; npm: reloads all npm modules; npm:chalk reloads a single npm module; jsr:@std/http/file-server,jsr:@std/assert/assert-equals reloads specific modules.

--vendor<vendor>optional
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<PACKAGE>optional
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).

--arch<arch>
Jump to heading

Target architecture for npm package installation (e.g., x64, arm64).

--cert<FILE>
Jump to heading

Load certificate authority from PEM encoded file.

Install the script as a compiled executable.

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

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.

Add the package as a dev dependency (under devDependencies). Note: this only applies when adding to a package.json file.

--entrypoint, -e
Jump to heading

Install dependents of the specified entrypoint(s).

--env-file<FILE>optional
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, -f
Jump to heading

Forcefully overwrite existing installation.

--global, -g
Jump to heading

Install a package or script as a globally available executable.

assume unprefixed package names are jsr packages.

--location<HREF>
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<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, -n<name>
Jump to heading

Executable file name.

--no-config
Jump to heading

Disable automatic loading of the configuration file.

assume unprefixed package names are npm packages (default).

Target OS for npm package installation (e.g., linux, darwin, win32).

--package-json
Jump to heading

Force using package.json for dependency management instead of deno.json.

--preload<FILE>
Jump to heading

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

Only install production dependencies (excludes devDependencies).

--require<FILE>
Jump to heading

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

--root<root>
Jump to heading

Installation root.

--save-exact
Jump to heading

Save exact version without the caret (^).

--seed<NUMBER>
Jump to heading

Set the random number generator seed.

--skip-types
Jump to heading

Exclude @types/* packages from installation. Be careful, as it uses a name-based heuristic and may skip packages that ship runtime code.

--v8-flags<V8_FLAGS>optional
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<HOST_PORT>optional
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<HOST_PORT>optional
Jump to heading

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

--inspect-wait<HOST_PORT>optional
Jump to heading

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

Last updated on

Did you find what you needed?

编辑此页面
Privacy policy