On this page
deno install
Command line usage
deno install [OPTIONS] [cmd]...
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 jsr:@std/bytes
deno install npm:chalk
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
orcli
, 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
optionDENO_INSTALL_ROOT
environment variable$HOME/.deno
These must be added to the path manually if required.
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.
--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.
--location
Jump to heading
Value of globalThis.location used by some web APIs.
--name
Jump to heading
Short flag: -n
Executable file name.
--no-config
Jump to heading
Disable automatic loading of the configuration file.
--root
Jump to heading
Installation root.
--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]
--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.
示例 Jump to heading
deno install Jump to heading
使用此命令安装 deno.json
和/或 package.json
中定义的所有依赖项。
依赖项将被安装在全局缓存中,但如果您的项目有 package.json
文件,本地的 node_modules
目录也会被创建。
deno install [PACKAGES] Jump to heading
使用此命令安装特定的软件包,并将其添加到 deno.json
或 package.json
中。
$ deno install jsr:@std/testing npm:express
您也可以使用 deno add
,它是 deno install [PACKAGES]
的别名。
如果您的项目有 package.json
文件,来自 npm 的软件包将被添加到 package.json
的 dependencies
中。否则,所有软件包将被添加到 deno.json
中。
deno install --entrypoint [FILES] Jump to heading
使用此命令安装提供文件及其依赖项中使用的所有依赖项。
如果您在代码中使用 jsr:
、npm:
、http:
或 https:
指定符,并希望在部署项目之前缓存所有依赖项,这尤其有用。
import * as colors from "jsr:@std/fmt/colors";
import express from "npm:express";
$ deno install -e main.js
下载 jsr:@std/fmt
下载 npm:express
如果您想设置本地的 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
下载 jsr:@std/http/file-server...
✅ 成功安装 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
原生 Node.js 附加模块 Jump to heading
许多流行的 npm 软件包,如 npm:sqlite3
或 npm:duckdb
,依赖于 "生命周期脚本",例如 preinstall
或 postinstall
脚本。通常,运行这些脚本是软件包正常工作的必要条件。
与 npm 不同,Deno 默认不运行这些脚本,因为它们可能会带来安全漏洞。
您仍然可以通过在运行 deno install
时传递 --allow-scripts=<packages>
选项来运行这些脚本:
deno install --allow-scripts=npm:sqlite3
安装所有依赖项,并允许 npm:sqlite3
软件包运行其生命周期脚本。
卸载 Jump to heading
您可以使用 deno uninstall
命令卸载依赖项或二进制脚本:
$ deno uninstall express
删除 express
$ deno uninstall -g file-server
删除 /Users/deno/.deno/bin/file-server
✅ 成功卸载 file-server
有关更多详细信息,请参见 deno uninstall
页面。