Deno 2 is the latest iteration of Deno, a modern JavaScript/TypeScript runtime built on V8. This release introduces significant improvements, including zero-config Node compatibility, a built-in package manager, and enhanced CLI capabilities. This tutorial will guide you through the new features and how to get started with Deno 2.
Before diving into Deno 2, you need to install it. Use the following command to install Deno:
curl -fsSL https://deno.land/install.sh | sh
Once installed, verify the installation by running:
deno --version
This should display Deno 2.x or higher.
Deno 2 is now zero-config, Node-compatible and supports package.json, node_modules, and npm workspaces. This allows you to run Node.js packages seamlessly:
{
"name": "deno2-project",
"version": "1.0.0",
"scripts": {
"start": "deno run mod.ts"
},
"dependencies": {
"@types/node": "^20.11.0",
"express": "^4.18.2"
}
}
You can install and run npm packages directly:
npm install
npm start
Deno 2 includes a built-in package manager with commands like deno install/add/remove. These commands respect semver and cache packages globally:
deno install lodash
Deno 2 is backwards compatible with Deno 1.x, making it safe to adopt incrementally. You can drop existing Deno tools (fmt, lint, test) into Node repositories:
deno fmt --recursive
deno lint --unstable
deno test --file-lock-ignore
The JSR (JavaScript Registry) is a TypeScript-first ESM registry for cross-runtime libraries. It provides high-quality, TypeScript-compatible packages:
deno install typescript@latest
Deno 2 introduces several CLI enhancements:
deno compile main.ts
deno serve --unstable
deno jupyter https://deno.land/x/jupyter@latest
Deno 2 introduces an LTS (Long-Term Support) channel, providing predictable support windows for production workloads.
Create a new directory for your project and navigate into it:
mkdir deno2-project
cd deno2-project
Create a simple "Hello, World!" script:
// hello.ts
console.log("Hello, World!");
Run it using Deno:
deno run hello.ts
Install external modules using the Deno package manager:
deno install https://deno.land/x/[email protected]/mod.ts
// script.ts
import _ from 'https://deno.land/x/[email protected]/mod.ts';
console.log(_.chunk(['a', 'b', 'c'], 2)); // Output: ['a', 'c']
Run the script:
deno run script.ts
You can run Deno tasks using:
deno task build
deno task watch
Create a simple package:
// main.ts
export function greet(name: string) {
return `Hello, ${name}!`;
}
Create a package.json file:
{
"name": "deno-greet",
"version": "1.0.0",
"description": "A simple greeting package",
"main": "main.ts",
"scripts": {
"test": "deno test"
},
"keywords": [],
"author": "Your Name",
"license": "MIT"
}
Build your package and publish it to npm:
deno build main.ts
npm publish
Install your package in another project:
npm install deno-greet
Share your package with others. They can import it as:
import { greet } from 'deno-greet';
Deno 2 offers a powerful, modern way to build JavaScript and TypeScript applications. With its Node compatibility, built-in package manager, and enhanced CLI capabilities, Deno 2 is the perfect choice for developers looking for a secure and efficient runtime environment. By following this tutorial, you should now have a solid understanding of how to get started with Deno 2 and leverage its features to build robust applications.