在当前页面
deno run
,运行一个文件
命令行用法
deno run [OPTIONS] [SCRIPT_ARG]...
Run a JavaScript or TypeScript program, or a task or script.
By default all programs are run in sandbox without access to disk, network or ability to spawn subprocesses.
deno run https://examples.deno.land/hello-world.ts
Grant permission to read from disk and listen to network:
deno run --allow-read --allow-net jsr:@std/http/file-server
Grant permission to read allow-listed files from disk:
deno run --allow-read=/etc jsr:@std/http/file-server
Grant all permissions:
deno run -A jsr:@std/http/file-server
Specifying the filename '-' to read the file from stdin.
curl https://examples.deno.land/hello-world.ts | deno run -
Type checking options Jump to heading
--check
Jump to heading
Enable type-checking. This subcommand does not type-check by default 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.
--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.
--ext
Jump to heading
Set content type of the supplied file.
--location
Jump to heading
Value of globalThis.location used by some web APIs.
--no-code-cache
Jump to heading
Disable V8 code cache feature.
--no-config
Jump to heading
Disable automatic loading of the configuration file.
--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.
File watching options Jump to heading
--hmr
Jump to heading
Watch for file changes and restart process automatically. Local files from entry point module graph are watched by default. Additional paths might be watched by passing them as arguments to this flag.
--no-clear-screen
Jump to heading
Do not clear terminal screen when under watch mode.
--watch
Jump to heading
Watch for file changes and restart process automatically. Local files from entry point module graph are watched by default. Additional paths might be watched by passing them as arguments to this flag.
--watch-exclude
Jump to heading
Exclude provided files/patterns from watch mode.
用法 Jump to heading
要运行 这个文件,请使用:
deno run https://docs.denohub.com/examples/scripts/hello_world.ts
你也可以在本地运行文件。确保你在正确的目录下,并使用:
deno run hello-world.ts
默认情况下,Deno
在沙盒中运行程序,没有访问磁盘、网络或生成子进程的权限。这是因为 Deno 运行时是
默认安全的。你可以使用
--allow-*
和 --deny-*
标志
来授予或拒绝所需的权限。
权限示例 Jump to heading
授予从磁盘读取和监听网络的权限:
deno run --allow-read --allow-net server.ts
授予从磁盘读取允许列表中的文件的权限:
deno run --allow-read=/etc server.ts
授予所有权限 不推荐这样做,仅用于测试:
deno run -A server.ts
如果你的项目需要多个安全标志,你应该考虑使用
deno task
来执行它们。
监听 Jump to heading
要监听文件更改并自动重启进程,请使用 --watch
标志。Deno
内置的应用程序监听器会在文件更改时立即重启你的应用程序。
确保将标志放在文件名之前,例如:
deno run --allow-net --watch server.ts
Deno 的监听器会在控制台中通知你更改,并在你工作时在控制台中警告错误。
运行 package.json 脚本 Jump to heading
package.json
脚本可以使用 deno task
命令执行。
从 stdin 运行代码 Jump to heading
你可以从 stdin 管道代码并立即运行它:
curl https://docs.denohub.com/examples/scripts/hello_world.ts | deno run -
终止运行 Jump to heading
要停止运行命令,请使用 ctrl + c
。