Typescript scripts in the command line: Using Deno 2.0

This article is part of a series comparing Deno 2.0 and Node.JS's capabilities for running Typescript scripts with external dependencies from the command line. For a broader overview, refer to the first page of the series. This is the Deno 2.0 implementation; stay tuned for the Node.JS implementation of the same script. Implementing the Script Using Deno 2.0 Since the beginning, Deno was able to run Typescript scripts natively, without the need for transpilation or any extra step. What was changed in Deno 2.0 was the ability to import and run NPM dependencies in your script without needing initialization steps, a project file, transpilation, or bundle steps. Environment setup Before starting, make sure you have Deno, at least at version 2.0 installed on your machine. For that, in a terminal window, run deno -v If you get an error, or a version older than 2.0, check this page for instructions on how to install/update Deno VS Code If you are using VS Code, which I recommend, you may want to install the official VS Code Deno extension Creating the script In a folder of your choice create a file called sayHello.ts and open it in your editor. Importing external dependencies To use external dependencies, such as NPM modules, in Deno, all you need to do is import it in your script file, following the syntax registry_type:package_name@package_version as illustrated below. Add chalk, yosay and inquirer from NPM // sayHello.ts import chalk from 'npm:chalk@5.3.0'; import yosay from 'npm:yosay@3.0.0'; import inquirer, { Answers } from 'npm:inquirer@12.0.0'; If you are using VS Code, hover over the red error lines, click on Quick fix, and choose Cache all dependencies of this module Implementing the script body Add the following lines to sayHello.ts after the import block // sayHello.ts // ... console.log(chalk.blue('\nHello visitor, please enter you details:\n')); const questions: Answers = [ { type: 'input', name: 'name', message: "What's your name?", required: true, }, { type: 'list', name: 'salutation', message: 'How should I refer to you?', choices: ['Mr.', 'Mrs.', 'none'], } ]; inquirer.prompt(questions).then((answers) => { const { salutation, name } = answers ; console.log(`\n${chalk.green('[SUCCESS]:')} information entered was correct`); const person: string[] = [ name ]; if(salutation !== 'none') person.unshift(salutation); console.log(yosay(`Hallo ${person.join(' ')}!`)); }); Running the script Method 1: Using deno command Open a terminal window and run deno -A sayHello.ts The -A option gives Deno all the permissions needed to download the external dependencies and run the code locally. Method 2: Making the script executable (Linux/MacOS) Add this line at the top of sayHello.ts #!/usr/bin/env -S deno -A // sayHello.ts // ... open a terminal window and run chmod +x sayHello.ts and then, every time you need to run the script, in the script folder, do ./sayHello.ts Run your script using the desired method, answer the questions, and check the output. Wrapping up That's it. That's all you need to do to run a Typescript script with external dependencies on the command line using Deno 2.0 You can check the complete source code of the Deno version of the script here. In the next article in this series, we will explore how to achieve the same functionality with Node.JS instead of Deno. Stay tuned.

Jan 19, 2025 - 21:41
Typescript scripts in the command line: Using Deno 2.0

This article is part of a series comparing Deno 2.0 and Node.JS's capabilities for running Typescript scripts with external dependencies from the command line. For a broader overview, refer to the first page of the series. This is the Deno 2.0 implementation; stay tuned for the Node.JS implementation of the same script.

Implementing the Script Using Deno 2.0

Since the beginning, Deno was able to run Typescript scripts natively, without the need for transpilation or any extra step. What was changed in Deno 2.0 was the ability to import and run NPM dependencies in your script without needing initialization steps, a project file, transpilation, or bundle steps.

Environment setup

Before starting, make sure you have Deno, at least at version 2.0 installed on your machine. For that, in a terminal window, run

deno -v

If you get an error, or a version older than 2.0, check this page for instructions on how to install/update Deno

VS Code

If you are using VS Code, which I recommend, you may want to install the official VS Code Deno extension

Creating the script

In a folder of your choice create a file called sayHello.ts and open it in your editor.

Importing external dependencies

To use external dependencies, such as NPM modules, in Deno, all you need to do is import it in your script file, following the syntax registry_type:package_name@package_version as illustrated below.

Add chalk, yosay and inquirer from NPM

// sayHello.ts

import chalk from 'npm:chalk@5.3.0';
import yosay from 'npm:yosay@3.0.0';
import inquirer, { Answers } from 'npm:inquirer@12.0.0';

If you are using VS Code, hover over the red error lines, click on Quick fix, and choose Cache all dependencies of this module

Implementing the script body

Add the following lines to sayHello.ts after the import block

// sayHello.ts
// ...

console.log(chalk.blue('\nHello visitor, please enter you details:\n'));

const questions: Answers = [
  {
    type: 'input',
    name: 'name',
    message: "What's your name?",
    required: true,
  },
  {
    type: 'list',
    name: 'salutation',
    message: 'How should I refer to you?',
    choices: ['Mr.', 'Mrs.', 'none'],
  }
];

inquirer.prompt(questions).then((answers) => {
  const { salutation, name } = answers ;

  console.log(`\n${chalk.green('[SUCCESS]:')} information entered was correct`);

  const person: string[] = [ name ];
  if(salutation !== 'none') person.unshift(salutation);

  console.log(yosay(`Hallo ${person.join(' ')}!`));
});

Running the script

Method 1: Using deno command

Open a terminal window and run

deno -A sayHello.ts

The -A option gives Deno all the permissions needed to download the external dependencies and run the code locally.

Method 2: Making the script executable (Linux/MacOS)

Add this line at the top of sayHello.ts

#!/usr/bin/env -S deno -A

// sayHello.ts
// ...

open a terminal window and run

chmod +x sayHello.ts

and then, every time you need to run the script, in the script folder, do

./sayHello.ts

Run your script using the desired method, answer the questions, and check the output.

Wrapping up

That's it. That's all you need to do to run a Typescript script with external dependencies on the command line using Deno 2.0

You can check the complete source code of the Deno version of the script here.

In the next article in this series, we will explore how to achieve the same functionality with Node.JS instead of Deno. Stay tuned.