2. Deno is a new runtime for JavaScript and TypeScript .
Developed by Ryan Dahl - the creator of Node.js.
Since 2018, the creator of Node.js has been working on a new
JavaScript runtime called Deno that addresses some of the pain
points he had identified. Deno’s features include:
1. An improved security model
2. Decentralized package management
3. A standard library
4. Built in tooling
Deno 1.0 was just released. What better time to dive in? In this
post we’ll get started with Deno by building a small project.
What is Deno ?
3. Why we use Deno instead of Node
Deno aims to fix Node.js design mistakes and offers a new modern
development environment.
Deno is a new platform for writing applications using JavaScript and
TypeScript. Both platforms share the same philosophy – event-
driven architecture and asynchronous non-blocking tools to build
web servers and services. The author of Deno is Ryan Dahl, original
creator of Node.js. In 2018, he gave the famous talk “10 Things I
Regret About Node.js“ and announced his new project – Deno.
Deno aims to fix Node.js design mistakes and offers a new modern
development environment.
Both platforms serve the same purpose, but use different
mechanisms. Deno uses ES Modules as the default module system,
whereas Node.js uses CommonJS. External dependencies are loaded
using URLs, similar to browsers.
4. What are its main features?
Security (permissions)
Among the most important of Deno’s features is its focus on security.
As opposed to Node, Deno by default executes the code in a sandbox, which means that
runtime has no access to:
1. The file system
2. The network
3. Execution of other scripts
4. The environment variables
Let’s take a look at how the permission system works.
(async () => {
const encoder = new TextEncoder();
const data = encoder.encode('Hello worldn');
await Deno.writeFile('hello.txt', data);
await Deno.writeFile('hello2.txt', data);
})();
When we run it by executing:
deno run write-hello.ts
5. Modules
Deno, just like browsers, loads modules by URLs. Many people got confused at first when they saw an
import statement with a URL on the server side, but it actually makes sense — just bear with me:
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
What’s the big deal with importing packages by their URLs, you may ask? The answer is simple: by
using URLs, Deno packages can be distributed without a centralized registry such as npm, which
recently has had a lot of problems, all of them explained
By importing code via URL, we make it possible for package creators to host their code wherever they
see fit — decentralization at its finest.
No more package.json and node_modules.
Do I have to import it by the URL all the time?
Constantly typing URLs would be very tedious. Thankfully, Deno presents us with two options to avoid
doing that.
The first option is to re-export the imported module from a local file, like so:
export { test, assertEquals } from "https://deno.land/std/testing/mod.ts";
Let’s say the file above is called local-test-utils.ts. Now, if we want to again make use of
either test or assertEquals functions, we can just reference it like this:
import { test, assertEquals } from './local-test-utils.ts';So it doesn’t really matter if it’s loaded from a
URL or not
6. Browser compatibility
Deno aims to be browser-compatible. Technically speaking, when using the ES modules,
we don’t have to use any build tools like webpack to make our application ready to use in a
browser.
However, tools like Babel will transpile the code to the ES5 version of JavaScript, and as a
result, the code can be run even in older browsers that don’t support all the newest
features of the language. But that also comes at the price of including a lot of unnecessary
code in the final file and bloating the output file.
It is up to us to decide what our main goal is and choose accordingly.
TypeScript support out of the box
Deno makes it easy to use TypeScript without the need for any config files. Still, it is
possible to write programs in plain JavaScript and execute them with Deno without any
trouble.
7. Summary
Deno, the new runtime for TypeScript and JavaScript, is an interesting project that
has been steadily growing for quite some time now. But it still has a long way to go
before it’s considered production-ready.
With it’s decentralized approach, it takes the necessary step of freeing the
JavaScript ecosystem from the centralized package registry that is npm.
Dahl says that he expects to release version 1.0 by the end of the summer, so if you
are interested in Deno’s future developments, star its repository.
Reference :
https://blog.logrocket.com/what-is-deno/
https://www.twilio.com/blog/getting-started-deno