articles

How to Use Deno’s Built-in Tools

Deno is a simple runtime for JS and Typescript. It comes with a lot of useful built-in tools to execute your scripts. This guide is all about how to use those tools. Before you go on with these tools, take note that this runtime is comparatively new so use it carefully. Although it keeps on getting updated, it is still a long way from perfection. Keep in mind that some of the tools may be unstable and produce unintended results. It is best to isolate Deno and run it from a separate directory to prevent it from making unintended changes to your project. So here is how to use Deno tools in detail.

 

Installing Deno

 

Installing Deno on all supported platforms is pretty easy. For installing it on Windows, execute the following command.

 

iwr https://Deno.land/x/install/install.ps1 -useb | iex 

 

On a Mac execute the following command:

 

iwr https://Deno.land/x/install/install.ps1 -useb | iex 

 

After the installation, confirm if Deno is installed by checking the version. Enter the following command:

 

Deno --version 

 

this command will display the version for the Compiler, JS engine, and Deno. If you already have Deno installed, you can upgrade Deno by commanding the console with Deno upgrade.

 

Deno Tools

 

The Deno tools can be revealed through the Deno help command. This command will give a different list of tools and other options to work with Deno.

 

1.    Read-Eval-Print Loop (REPL)

 

The REPL is similar to Node.js. It is an expression evaluation console and you can access it by launching Deno in your windows PowerShell or your terminal. To scroll through the previous expressions, you can use the arrow navigation keys. Each expression entered can either give a result or end up being undefined.

 

2.    Linter (Syntax Checker)

 

Deno linter is essentially a Javascript and Typescript validation tool. This tool validates your JS and TS code by checking for syntax errors. Do remember to put the unstable flag because validation is a bit unstable. Although it will not change any of your files, the flag is required.

 

When there are long lines of code, it becomes often difficult to debug because some errors are so unobvious that it is really hard to reach them. Lint ensures that your code is consistent throughout your team and adheres to the accepted standards. You may separately use lint by the ESlint tool from your terminal but Deno also has another option to check for syntax in any environment where you have installed Deno to check for all the scripts. You can also separately select one or multiple files for Deno to check for only them.

 

3.    Test Runner

 

Deno also comes with a built-in code tester. This is used for prototyping. The test runner will test for all the Js and TS functions. You can define the filename ending with a script extension to test them. Make a call to Deno.test. After passing the test name string with a testing function, you can use many utilities to check and evaluate your results. The functions can be both synchronous and asynchronous.

 

To test it, create a new testing folder with anyname.js and test your script by putting them in that directory. You can also check scripts from all directories. To do that enter Deno test mode and run tests by specifying the directory after the Deno test command. It will separately give results for each script and will specify how many scripts passed, ignored, measured, failed or filtered. Speaking of which you can also use the Deno test filter command to limit tests for a specific string. For instance:

 

>$ Deno test --filter "hello" ./test 

running 1 tests

test lib/hello tests ... ok (4ms)
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 3 filtered out (5ms)

 

You can also interrupt the testing at a specific point in time by deploying the failfast command.

 

4.    Dependency Inspector

 

The dependency inspector can show a tree of all the dependencies. You can launch this tool by entering the command 

 

Deno info <URL or path for an entry script>

 

Take for example the script sample library.js with hello and sum functions that are exported.

 

// sample library: lib.js

/**
* return "Hello <name>!" string
* @module lib
* @param {string} name
* @returns {string} Hello <name>!
*/
export function hello(name = 'Anonymous') { return `Hello ${ name.trim() }!`; }; /**
* Returns total of all arguments
* @module lib
* @param {...*} args
* @returns {*} total
*/
export function sum(...args) { return [...args].reduce((a, b) => a + b); }

 

You can use the same from the main script in the same directory. Consider this Index.js script

 

// main entry script: index.js

// import lib.js modules

import { hello, sum } from './lib.js'; const
spr = sum('Site', 'Point', '.com', ' ', 'reader'),
add = sum(1, 2, 3); // output
console.log( hello(spr) );
console.log( 'total:', add );

 

When you run Deno, it can result in the following result:

 

Deno run ./index.js

Hello SitePoint.com reader!

total: 6

 

Again, to inspect for dependencies, you can examine through the dependency inspector. Type the command

 

Deno info ./index.js:

 

It will return in the following

 

$ Deno info ./index.js 

local: /home/Deno/testing/index.js
type: JavaScript
deps:
file:///home/Deno/testing/index.js
└── file:///home/Deno/testing/lib.js

 

You can inspect for any dependencies that are required by the URL of any of the modules. Do note that when you do that, the module will be automatically downloaded and will be cached. You will need to remove it manually. For more information, you can read the Dependency inspector user manual.

 

5.    V8 Debugger

 

As with all debuggers, Deno has the latest V8 debugger. This works similar to node.js. You can easily debug using the chrome browser or the VS code itself and then check for changes in objects. It is very simple to launch the debugger. Just run your targeted script with --inspect command. You can also connect the debugger from another device on your network by specifying its IP address and its port. For example, 

 

Deno run --inspect-brk=192.168.1.2:9229 ./index.js

 

When you open a new tab in your browser, the script will show as a “remote target”. But you cannot connect the Devtools for Node.js even though they are similar to the Deno debugger. Use the inspect link to go to the Devtools. If you are familiar with client-side debugging, this will be easier for you. 

 

6.    Code Formatter

 

The built-in code formatter automatically formats the JS and TS code that is very similar to what you can achieve with Prettier. Currently, you cannot configure the options for the formatter so no customization is available here for power coders. Just use the command Deno fmt to format every file in all the directories and subdirectories. You can also use only one or multiple selected files by specifying the file name. For example,

 

Deno fmt ./script.js

 

will select the script .js file and format that one only.

 

Another useful command can be used to ignore files by adding a comment

 

// Deno-fmt-ignore-file

 

on the top. We don’t recommend using auto-formatting too much as it can affect the JSdocs comments.

 

7.    Documentation Generator

 

Deno can also generate documentation from the comments within the source code. This documentation helps define different parameters, return values, and explain the purpose of a specific function. Only modules having the export functions can be called in for generating documentation. You can also add the JSON flag to generate the documentation in JSON format. 

 

8.    Script Installer


Deno can install scripts globally so it is easy to run them from anywhere within the file system. It also has parallels with Node.js but is comparatively easy to use. To start, you must first execute the Deno install command. To install, check if any runtime permission flags or optional scripts are required. Also, double-check the root directory. Set it with --root <path> otherwise it will be installed in the default directory. Lastly, specify the module path or URL if working over the network. Deno makes it easy for the users to install your application from a URL if you specify the URL. For example:

 

Deno install --name sampleapp https://xxx.com/sampleapp.js

sampleapp

 

Currently, Deno does not have any commands to remove the scripts. The only way you can do that is to locate the bin directory and delete it from there.  If you have installed it to any other directory, you have to navigate to that directory to delete that file. More information in the user manual.

 

9.    Script Bundler

 

You can confine all your scripts with their dependencies in one file. Just execute the following command

 

Deno bundle <main-script> <output-script>

 

where the main script is the input script and the output script is the output file. Immediately after doing that you can execute the script by executing the run command. This is useful when you have to distribute the final version of scripts to your end-users or when you are deploying your code on a live server. Do note that there is a known issue with Deno. The await calls can end up failing during bundling. To get around this, use the async wrapper. For troubleshooting, refer to the Deno bundling manual.

Conclusion

 

Deno tools are in their alpha stage. But it offers a much easier approach to manage your scripts. Deno does not provide the option to use third-party options. But this is deliberate as the whole purpose of Deno is to limit the hazard of solution switching. Deno is slowly developing and a major overhaul of its tools and functions is expected.

 

Questions about Deno's tools? Reach out for a consultation and clear insights.

Facebook Linkedin