7th Oct 2020

Injecting node_modules with deno 🦕

Node

Written By, Nikhil Savaliya

Overview

In our previous blog Exploring deno we have discovered a basic HTTP server with deno. In this blog, we will deep dive what are the compatibilities of NPM packages with Deno.

Deno is getting popular day by day among developers. Deno is upgraded to v1.1.3 now.
Lots of new deno packages are getting published over GitHub.

1. But, Do we need new packages or can we still rely on NPM packages?

There is no confirmation yet, but let’s look into how we can use NPM packages with deno and what are the pros and cons of that.

While we are talking about third party modules, Deno can load any third-party modules from any location on the web, like GitHub, a personal web server, or a CDN jspm or pika.

As we know deno provides a Node Compatibility Library, interesting right?
That will allow us to use some of the NPM packages that are not using non-polyfilled Node.js APIs

2. What do we mean by the Node compatibility library?

Simply means Deno node uses a compatibility layer for the NodeJS standard library

So simply if you have explored the hyperlink of the Deno node standard library, we can use those tick marked libraries that are neither deprecated nor not yet stable.

Let’s see how we can achieve,

  1. Make sure we are having Deno 1.0.0+ installed in our machine.
  2. Create a directory and hit npm init -y.
  3. For this we will use esprisma NPM package, the reason is the library is stable and not dependent on any other modules ( If you choose any other modules check either deno supports that library ).
  4. Make sure you are in the right path and run, npm install exprisma.
  5. Create a file index.ts

import-create-img

To require CJS Modules, createRequire(…), that also sets supported globals.

Update index.ts file with,

update-index

Time for execution of the above snippet.

In your terminal,

Before the output, lets breakdown the execution line,

Read more about flags here.

What about the output after successful execution,

terminal-img

Let’s play around a bit more with,

Nodejs Path library,

What about other third parties?

Lodash is already there with deno third-party modules.

But let’s play with the NPM package.

In your directory install lodash dependency and let’s execute below snippet,

npm install lodash

output-name

Output:- { name: “Nikhil” }

Can not wait to see more stuff in action.

Please share your experiments with deno below in the comments, so excited to see innovation and ideas.

Written By,

MEAN stack developer at Yudiz Solutions Pvt. Ltd