DEV Community

Shawn Bullock
Shawn Bullock

Posted on

Import any Deno module directly from GitHub or NPM

The creators of Deno dropped us a very welcome goodie today. I started using Deno about a week ago and instantly fell in love with it. I really wished there was a way to directly reference modules from GitHub without having to register the repo with deno.land/x/ first.

I visited deno.land/x/ today and noticed this:

Alt Text

Today that has been solved. They now allow us to reference any arbitrary github repo or NPM package. Here's how:

To import the amqp module from GitHub, for example:

// import from master
import { amqp } from "https://deno.land/x/gh:lenkan:amqp/mod.ts";

// import version
import { amqp } from "https://deno.land/x/gh:lenkan:amqp@v0.9.1/mod.ts";
Enter fullscreen mode Exit fullscreen mode

Likewise, to import the numbers package from NPM just use the following:

// import latest
import { numbers } from "https://deno.land/x/npm:numbers/index.js";

// import version
import { numbers } from "https://deno.land/x/npm:numbers@v0.7.0/index.js";
Enter fullscreen mode Exit fullscreen mode

Top comments (5)

Collapse
 
_mknet_ profile image
Marcel Koch • Edited

The sample with numbers does not work on my side:

import { numbers } from "https://deno.land/x/npm:numbers/index.js";
console.log(numbers);

results in
error: Uncaught SyntaxError: The requested module 'https://deno.land/x/npm:numbers/index.js' does not provide an export named 'numbers'

I guess it's about using JS in TS code. Also tried
import * as numbers from "https://deno.land/x/npm:numbers/index.js";
console.log(numbers);

Did you have a simialar issue? Thank you so much!

Collapse
 
_mknet_ profile image
Marcel Koch

Oh dear, of course ...
I needed to allow JS in the tsconfig.json:
{
"compilerOptions": {
"allowJs": true,
}
}

Now I got the issue described under github.com/denoland/deno/issues/6082

Collapse
 
ievolved profile image
Shawn Bullock

Good catch. I completely forgot about that setting... LOL. Thanks for posting.

Collapse
 
artakan505 profile image
Artakan⚡️

It looks like this feature has been removed (version v1.2.2), am I wrong ?

Collapse
 
ievolved profile image
Shawn Bullock • Edited

I would appear you're correct.