On this page
deno add
deno add 命令会将依赖项添加到项目的配置文件中。
它是
deno install [PACKAGES] 的别名。
有关 Deno 如何处理依赖项的更多信息,请参阅
模块和依赖项。
示例 Jump to heading
从 JSR 和 npm 添加包:
deno add npm:express hono zod
deno add jsr:@std/path
未添加前缀的包名默认会被视为 npm 包,因此在 CLI 中不再需要 npm:
前缀。deno add express 等同于
deno add npm:express。JSR 包仍然需要 jsr: 前缀以保持含义明确。npm: 前缀在 import 规范符中仍然是必需的。
默认情况下,依赖项会使用插入符号(^)版本范围添加。使用
--save-exact 可以将其固定到确切版本:
deno add --save-exact @std/path
这会保存没有 ^ 前缀的依赖项(例如,1.0.0 而不是 ^1.0.0)。
依赖项存储在哪里 Jump to heading
如果你的项目有 package.json,npm 包会被添加到
package.json 中的 dependencies。否则,所有包都会添加到
deno.json 中的 imports 字段。
要强制将每个依赖项都写入 package.json(如有需要会创建一个),请传递
--package-json(Deno 2.8+):
deno add --package-json npm:express jsr:@std/path
使用 --package-json 添加的 JSR 包会以其兼容 npm 的
形式(npm:@jsr/...)写入。这个标志同样适用于
deno install,
deno remove, 以及
deno uninstall。
要在不每次传递该标志的情况下将其设为默认值,请在 deno.json 中设置
"preferPackageJson": true。
deno add [OPTIONS] [packages]...Add dependencies to your configuration file.
deno add express
Unprefixed packages default to npm. Use jsr: prefix for jsr packages:
deno add jsr:@std/path
Or multiple dependencies at once:
deno add express jsr:@std/path
Options Jump to heading
--allow-import, -I<IP_OR_HOSTNAME>optionalAllow importing from remote hosts. Optionally specify allowed IP addresses and host names, with ports as necessary. Default value: deno.land:443,jsr.io:443,esm.sh:443,raw.esm.sh:443,cdn.jsdelivr.net:443,raw.githubusercontent.com:443,gist.githubusercontent.com:443.
--allow-scripts<PACKAGE>optionalAllow running npm lifecycle scripts for the given packages
Note: Scripts will only be executed when using a node_modules directory (--node-modules-dir).
--deny-import<IP_OR_HOSTNAME>optionalDeny importing from remote hosts. Optionally specify denied IP addresses and host names, with ports as necessary.
--dev, -DAdd the package as a dev dependency (under devDependencies). Note: this only applies when adding to a package.json file.
--env-file<FILE>optionalLoad 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.
--jsrassume unprefixed package names are jsr packages.
--lockfile-onlyInstall only updating the lockfile.
--npmassume unprefixed package names are npm packages (default).
--package-jsonForce using package.json for dependency management instead of deno.json.
--save-exactSave exact version without the caret (^).
Dependency management options Jump to heading
--frozen<BOOLEAN>optionalError out if lockfile is out of date.
--lock<FILE>optionalCheck the specified lock file. (If value is not provided, defaults to "./deno.lock").
--no-lockDisable auto discovery of the lock file.