Deno Module System: A Beginner’s Guide

Here’s a great article from SitePoint

Learn about the Deno module system – the biggest workflow change you’ll encounter if you’re coming from Node.js. Find out how it works and how to use it, how to make use of Node.js packages in Deno, and more.

Node.js is a JavaScript runtime based on Chrome’s V8 engine, developed by Ryan Dahl, and released in 2009.

Deno is a JavaScript runtime based on Chrome’s V8 engine, developed by Ryan Dahl, and released in 2020. It was created with the benefit of a decade’s worth of hindsight. That doesn’t necessarily make it a sequel or superior to Node.js, but it deviates from that path.

See also:

The headline differences: Deno natively supports TypeScript, security, testing, and browser APIs. Module handling receives less attention, but it’s possibly the largest change to how you create JavaScript applications. Before discussing Deno, let me take you back to a simpler time…

Node.js Modules

JavaScript didn’t have a standard module system in 2009. This was partly because of its browser heritage and ES6 / ES2015 was several years away.

It would have been inconceivable for Node.js not to provide modules, so it adopted CommonJS from a choice of community workarounds. This led to the development of the Node Package Manager, or npm, which allowed developers to easily search, use, and publish their own JavaScript modules.

npm usage grew exponentially. It’s become the most popular package manager ever devised and, by mid-2020, hosts almost 1.5 million modules with more than 800 new ones published every day (source: modulecounts.com).

Deno Modules

Deno opts for ES2015 Modules which you import from an absolute or relative URL:

import { something } from 'https://somewhere.com/somehow.js';

The script at that URL must export functions or other values accordingly, e.g.

export function something() {
  console.log('something was executed');
}

Deno uses an identical module system to that implemented in modern web browsers.

Node.js also supports ES2015 modules … but it’s complicated and remains experimental. CommonJS and ES2015 modules look similar, but work in different ways:

Continue reading
Deno Module System: A Beginner’s Guide
on SitePoint.

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *