Importing Modules in Deno

Ward Price
3 min readDec 28, 2020

For those looking to use a backend in JavaScript, we used to just have one option: Node.js.

JavaScript was created for the browser and had always lived there until Node.js came along in 2009, freeing the language to work outside the browser. The original vision was to have a small base and let the open-source community build out the rest of the runtime’s functionality. NPM hosted the packages and anyone with Node.js installed could pull and use the code. This created a centralized location for all Node.js packages, but it also became a free-for-all. Multiple packages did the same thing or would stop being maintained randomly. On top of that, Node.js had little security built in. Unless the developer blacklisted access, the computer system that the code was executed on was completely open. This all pushed Ryan Dahl, American software engineer and the original developer of Node.js, to create Deno.

Deno is meant to fix the shortcomings of Node.js. Unless otherwise specified at execution, everything you run is sandboxed. For instance, to run a program that needs access to the internet, you would have to add the --allow-net flag. Similarly, to read from the filesystem, you need to add the--allow-read flag and to write --allow-write.

Deno also does away with the centralized nature of NPM. Instead of installing a third party code via NPM, we can use the more modern ES6 syntax of importing a module––we just import from a web address instead of a file location. This is Deno emulating the same process popularized by Go. Originally that was what Deno was going to be built with, but as the project progressed, there was some things that Go wasn’t suited for so the project’s base was switched to Rust. However, the importing from web addresses stuck.

For example, in Node.js to install lodash you must first install it via NPM…

Then add it to your module’s file like so…

The version of lodash, as well as all other modules you’ve imported, are stored in the node_modules folder. This folder can quickly turn into a huge list that is difficult to version control.

On the other hand, Deno (as stated before) imports files using a modern ES6 syntax, but instead of adding a package from our machine, it takes the web address from where the file is hosted. That’s right, it just takes the raw code from a url.

That’s one line, and in that one line we see: where the repo is located, what version of lodash we are importing, and what functions we are taking from the repo, or in this case, all that is available.

I’m just getting into Deno and things are changing quickly, but check back for more analysis on how to use Deno, and eventually, it’s incorporation with React.

--

--