How to integrate a legacy JavaScript project with Deno TypeScript.
I'm attempting to use this package (https://github.com/nodertc/stun) that supports the STUN protocol, which is used by VoIP systems to handle NATs and firewalls. I can use it just fine as a JavaScript package under Node, but I am tearing my hair out trying to figure out how to import all the dependencies into my Deno project. (First time I've tried to use Deno, by the way.).
There are no TypeScript type annotations for this particular module. I've moved all code that interfaces with the legacy package into is own module, like this:
import stun from 'stun';
export async function getPublicIPAddressViaStun(url) {
try {
const result = await stun.request(url);
const ipAddress = result.getXorAddress().address;
return [ipAddress, undefined]
}
catch (error) {
return [undefined, error.message];
}
}
And I can use it from my main code thus:
import { getPublicIPAddressViaStun } from "./stun.js";
const [ipAddress, error] = await getPublicIPAddressViaStun('stun.l.google.com:19302');
if (ipAddress) {
console.log('Your ip address is', ipAddress);
} else {
console.error(error);
}
Now, when I try to use similar code in my Deno project, I get an error message when Deno tries to load dependencies of the STUN package, buried in a lib file:
error: Uncaught (in promise) Error: Cannot find module 'message/request'
Require stack:
- /Users/rwelbourn/Library/Caches/deno/npm/registry.npmjs.org/stun/2.1.0/src/index.js
- /Users/rwelbourn/Library/Caches/deno/npm/registry.npmjs.org/stun/2.1.0/src/index.js
...
Can anyone think of a way around this?
GitHub
GitHub - nodertc/stun: Low-level Session Traversal Utilities for NA...
Low-level Session Traversal Utilities for NAT (STUN) client and server - nodertc/stun
2 Replies
That package is using
require
so you need a node_modules dir. Try adding "nodeModulesDir": "auto"
to deno.json
The error message should include a hint that suggests you do just that, but it looks like it's missing that hint. I'll open an issue for itThank you, Nathan! That did the trick.