Is Deno the New Node.js?

Original author: Daniel Ramos Acosta
  • Transfer

In our project, the IDE for working with the TestMace API , TypeScript is used as the main language, so we are actively interested in projects related to this language. Today, your attention is a translation of an article about the Deno-runtime tool for TypeScript (but this is not its only feature). Enjoy reading.



About three months ago, Ryan Dahl (creator of Node.js) made a presentation at the conference, “ 10 Things I Sorry About Node.js About ”, where he talked about some of his unsuccessful design decisions for Node.js. He devoted almost half of his speech to the experimental prototype Deno, whose task was to eliminate the shortcomings of Node.js.


Deno has already reached version v0.1 ( translator's note - already v0.3.8 ) and is on the right track towards his goal of replacing Node.js in the future.


What is wrong with Node.js?


Any program can write data to a file system or network


This can result in a security risk, especially when installing unverified npm packages. For example, as in incident with crossenv . If crossenv did not have write permission, this would not have happened.


Fast Departing Asynchronous APIs


Promises appeared in Node.js in 2009, and in February 2010 they were gone. However, most libraries still use callbacks to work with asynchronous code.


Build System (GYP)


To assemble a module for connecting libraries to C through GYP is a torment. To get an adequate developer experience, you have to use node-gyp (a layer on top of GYP) and, possibly, other layers (like nan ). I personally tried all of the above in my small project , and I’m ready to agree, it made me sweat.


Module system and npm


The main problem is that the module system is not compatible with browsers, which means the code is not completely isomorphic. There are mainly two reasons for this: storing dependencies in node_modules and the presence of package.json.


What is Deno?


“Deno is a TypeScript runtime engine based on the V8 engine,
Ryan Dahl


Given that Typescript is an extended version of Javascript, Deno is also a runtime environment for Javascript.


Deno is a new project of Ryan Dahl (creator of Node.js) designed to fix the Node.js design errors discussed above.


Key features of Deno


Security


By default, Deno prohibits sensitive actions such as reading environment variables or writing to the file system.


The Deno process starts in unprivileged mode, and in order to access data like environment variables, special flags must be passed.


Permission to write to the file system, as well as permissions related to the environment and the network, are disabled. To enable these actions, it is necessary to call Deno with arguments --allow-writeand --allow-net.


All interaction between the privileged Deno process and v8 comes down to messaging (previously written in Go, now ported to Rust). This allows you to create a single point to check all messages.


Module system


Forget package.json and node_modules. When importing source files, you can specify either a relative or absolute path, or their full URL:


import { test } from "https://unpkg.com/deno_testing@0.0.5/testing.ts"
import { log } from "./util.ts"

By default, all source files are cached. You can use the --reload argument to update dependencies. It works like an F5 key in a browser.


TypeScript support out of the box


TypeScript is supported by Deno by default. Here it is. Without any buts. No configurations.


Deno v0.1.4 in action


To get started, download the Deno binary file:


$ mkdir deno-test && cd deno-test
$ wget https://github.com/denoland/deno/releases/download/v0.1.4/deno_linux_x64.gz
$ gunzip -c deno_linux_x64.gz > deno
$ chmod u+x deno
$ ./deno --version
deno: 0.1.4
v8: 7.0.247-deno

Now create a typescript file and execute it:



function hello(place: string): string {
  return `Hello ${place}`
}
console.log(hello('world'))

$ ./deno myscript.ts
Hello world

You can also try importing via URL. The only requirement is that the file at the end of the URL must have a .ts extension.


import { factorial } from "https://gist.githubusercontent.com/DanielRamosAcosta/ad514503b1c7cf8290dadb96a5fddee9/raw/4733e267f05d20110ba962c4418bab5e98abfe93/factorial.ts"
console.log(factorial(10))

$ ./deno myimport.ts
Downloading https://gist.githubusercontent.com/DanielRamosAcosta/ad514503b1c7cf8290dadb96a5fddee9/raw/4733e267f05d20110ba962c4418bab5e98abfe93/factorial.ts
3628800

During the execution of the script, the module will load and add to the cache. To update the cache, you can call Deno using the --reload argument, which is equivalent to the action of the F5 or Ctrl + R keys.


Below is a more complex example using the axios library:


import axios from "https://gist.githubusercontent.com/DanielRamosAcosta/2f773d815f5434f185c59aec1bab418c/raw/a442cdd8699e39ab9855cbaa571a79049a7b67d4/axios.ts"
// Make a request for a user with a given ID
axios.get('http://jsonplaceholder.typicode.com/users/1')
  .then(response => {
    // handle success
    console.log("User name:", response.data.name);
  })
  .catch(error => {
    // handle error
    console.error("error:", error);
  })

The only catch is that VSCode cannot load typings remotely, so the following error will appear in the editor:


An import path cannot end with a '.ts' extension.


But the code still works and produces the correct result:


./deno --allow-net axios-test.ts
User name: Leanne Graham

Conclusion


Deno will need a lot of time to grow into a full-fledged working tool, but, I believe, it is developing in the right direction, and it has every chance of becoming a more elaborate Javascript runtime than Node.js.


Thank you for attention!


Also popular now: