This article is not a complete tutorial on Deno development. You should have heard of Deno and understood the related concepts of Deno before reading this article.
1. Installation
Deno provides a variety of installation methods.
If you use a Linux/Mac system, you can install it through the shell:
curl -fsSL https://deno.land/x/install/install.sh | sh
If you use a Windows system, you can install it via PowerShell:
iwr https://deno.land/x/install/install.ps1 -useb | iex
You can also install it through other package management tools, such as scoop, brew, choco, etc. Deno is developed using rust, so if you are a rust developer, you can even cargo install deno
install it through .
Run after installation:
deno https://deno.land/std/examples/welcome.ts
If it can output normally, Welcome to Deno ????
then Deno has been installed correctly.
2. Configuration
Most developers use VS Code for development, but VS Code does not provide Deno support out of the box. We need to install the vscode-deno extension that supports Deno first.
After the installation is complete, the Deno indicator area will appear in the lower right corner, followed by the currently installed Deno version, and a more detailed version will be displayed when the mouse moves over.
Create a new .ts file. After we input Deno.
, the plug-in can automatically prompt for functions, parameters, types, etc.
3. Development
Let's write a simple example according to the official tutorial: a cat program. (PS: This program has nothing to do with cats, it is the cat command of linux)
Create a new cat.ts file and enter:
for (let i = 0; i < Deno.args.length; i++) {
let filename = Deno.args[i];
let file = await Deno.open(filename);
await Deno.copy(Deno.stdout, file);
file.close();
}
After you enter these few lines of code, VS Code will await
mark red at the place, and we can ignore this warning directly. The vscode-deno extension is also trying to solve this problem.
If you think this warning is too obtrusive, you can export {};
solve this problem by adding a line manually , then TypeScript will treat this file as a module.
4. Run
We can now use deno to run the .ts file we just wrote:
deno cat.ts
There is no output. Because cat's function is to connect N multiple pieces and output them, this program also needs to pass in 1-n parameters. Then we try to make this program output itself, we pass cat.ts as the 0th parameter:
deno cat.ts cat.ts
An exception was thrown directly:
error: Uncaught PermissionDenied: read access to "/Users/justjavac/deno/cat.ts", run again with the
--allow-read
flag
Because Deno is sandbox safe by default, unless the user explicitly specifies the --allow-read
parameters when running , the deno program cannot read the file.
We run again after adding parameters:
deno --allow-read cat.ts cat.ts
At this point we can see the correct output.
5. Description
deno cat.ts
Is deno run cat.ts
short form. The complete form is:
deno run [OPTIONS] <SCRIPT_ARG>...
OPTIONS
Are the parameters passed to deno. In addition to the previous ones --allow-read
, there are some commonly used ones:
--allow-run
: Allow creation of subroutines--allow-net
: Allow access to the network--allow-env
: Allow access to environment variables--allow-all
: Allow all
In addition to permissions-related parameters, there are other parameters. You can deno run -h
view all parameters by running .
6. Summary
This is the first article in this column. The purpose of this article is to give developers a glimpse of Deno's features. If you are interested in Deno, you can click to read the original text in the lower left corner to follow our Zhihu column [Deno Developer Community].