Deno is a runtime for JavaScript and TypeScript that is built on the V8 JavaScript engine. It is designed to be secure, simple, and reliable. Here's a cheat sheet to help you get started with Deno:
Installation
Using Shell (Linux/macOS):
curl -fsSL https://deno.land/x/install/install.sh | sh
Using PowerShell (Windows):
iwr https://deno.land/x/install/install.ps1 -useb | iex
Using Chocolatey (Windows):
choco install deno
Basic Commands
- deno run : Run a TypeScript or JavaScript file.
- deno eval "console.log('Hello, Deno!')": Execute JavaScript/TypeScript code directly.
- deno info : Display information about the TypeScript/JavaScript file.
- deno repl: Start an interactive REPL (Read-Eval-Print Loop).
Permissions
Deno has a permission-based model for accessing resources. When you run a script, it may need certain permissions to perform actions like accessing the file system or network. Use the --allow-* flags to grant permissions.
- --allow-read: Read access to files and directories.
- --allow-write: Write access to files and directories.
- --allow-net: Network access.
- --allow-env: Access to environment variables.
- --allow-run: Execute other programs.
Example:
deno run --allow-read myscript.ts
Package Management
Deno uses ES modules for importing dependencies. You can import modules directly from URLs or use a package manager like deno.land/x.
- Import from URL: import { something } from 'https://deno.land/x/something/mod.ts';
Dependency Locking
- Generate a lock.json file: deno cache --lock=lock.json --lock-write deps.ts
- Use the lock.json file to ensure reproducible builds: deno cache --lock=lock.json
Third-Party Tools
Deno has several third-party tools and libraries available:
- Oak: A middleware framework for building web applications. GitHub: https://github.com/oakserver/oak
- DenoDB: A simple and efficient database toolkit for Deno. GitHub: https://github.com/eveningkid/denodb
- Deno Postgres: A PostgreSQL database driver for Deno. GitHub: https://github.com/buildondata/deno-postgres
- oakCors: Middleware for enabling CORS in Oak. GitHub:https://github.com/oakserver/oak/blob/main/middleware/cors.ts
TypeScript Support
Deno has excellent TypeScript support by default. You can use TypeScript features like type checking and interfaces seamlessly.
Deno CLI Flags
- --allow-all: Grants all permissions (use with caution).
- --unstable: Enables unstable APIs.
Upgrade Deno
To upgrade Deno to the latest version, you can use the following command:
deno upgrade
This cheat sheet covers the basics of Deno. For more in-depth information and documentation, please visit the official Deno website: https://deno.land/
Top comments (0)