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,
- Make sure we are having Deno 1.0.0+ installed in our machine.
- Create a directory and hit `npm init -y`.
- 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 ).
- Make sure you are in the right path and run, npm install exprisma.
- Create a file index.ts
To require CJS Modules, createRequire(…), that also sets supported globals.
Update index.ts file with,
Time for execution of the above snippet.
In your terminal,
deno run --allow-read --unstable --allow-env server.ts
Before the output, lets breakdown the execution line,
--allow-read - Flag will allow deno to read the filesystem. --unstable - Deno features that are not yet released, that are locked behind this command --allow-env - Allow environment access for things like getting and setting of environment variables.
Read more about flags From denoland.
What about the output after successful execution,
Let’s play around a bit more with,
Nodejs Path library,
// Loads native module polyfill or NPM packages that With Deno node Compatibility const path = require("path"); console.log(path.join('/foo', 'bar', 'baz/asdf', 'quux', '..')) console.log(path.parse('/home/user/dir/file.txt'))
const querystring = require("querystring"); const URL = querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' }); console.log(URL);
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: “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.