herenickname
herenickname4w ago

Unexpected behavior while importing npm module

Im trying to use dns2 library in my deno project:
import { Packet } from 'npm:dns2'
import { Packet } from 'npm:dns2'
And I get this error:
& deno run -A --watch src/index.ts
error: Uncaught SyntaxError: The requested module 'npm:dns2' does not provide an export named 'Packet'
& deno run -A --watch src/index.ts
error: Uncaught SyntaxError: The requested module 'npm:dns2' does not provide an export named 'Packet'
But if I import this library in a different way, then everything works:
import dns from 'npm:dns2'
console.log(dns.Packet)
import dns from 'npm:dns2'
console.log(dns.Packet)
Output:
[Function: Packet] {
TYPE: {
...
[Function: Packet] {
TYPE: {
...
What am I doing wrong? Deno v1.44.4
2 Replies
marvinh.
marvinh.4w ago
You're doing nothing wrong, it's a problem with the dns2 package. They only provide code in commonJS format and export a single class as the default export.
class DNS {
// ...
}

DNS.Packet = ...
module.exports = DNS
class DNS {
// ...
}

DNS.Packet = ...
module.exports = DNS
That class has a static Packet member, but a class member is very different from a module export. In JavaScript one cannot destructure class members in import statements.
herenickname
herenickname4w ago
Thanks. I forked this package on my github to fix some things. Am I right in thinking that I won't be able to import it locally until I load it into npm? It's pretty hard to adapt the version for deno.