4

Node + NPM install dependencies in node_modules, so if I want to debug a dependency, I can just go to node_modules/some-pkg/some-file.js and add debugger statements, console-logs, and the likes.

In Deno, things get trickier, because dependencies are abstracted from the user - it's not plain local JS files anymore! For example, If I have the following code:

// main.js
import oaClient from 'https://cdn.pika.dev/oa-client';
const { createClient } = oaClient;
createClient();

It'll output the stacktrace:

nino@hp:~/learning/deno(master)$ deno run main.js 
error: Uncaught TypeError: Cannot read property 'paths' of undefined
  for (var path in specs.paths) {
                         ^
    at _default (https://cdn.pika.dev/-/oa-client@v0.7.1-hYTMbsju6JnnnXlCWvdN/dist=es2019/oa-client.js:428:26)
    at file:///home/nino/learning/deno/main.js:3:1

and although I can read the code online at https://cdn.pika.dev/-/oa-client@v0.7.1-hYTMbsju6JnnnXlCWvdN/dist=es2019/oa-client.js, I can't insert console logs or debugger statements.

Thanks to deno info <the URL above> I can get the local path of the cached copy of the dependency, but I don't think that editing files in ~/.cache/deno is the right way.

So, what's the Deno way to debug dependencies?

Nino Filiu
  • 16,660
  • 11
  • 54
  • 84

2 Answers2

4

You can try deno debugger


An alternative to modifying the global cached dependencies, that will affect all your projects, is to download the package inside your project directory using $DENO_DIR environment variable.

DENO_DIR=$PWD/modules deno run main.js

Now you can modify the contents inside $DENO_DIR/deps without affecting other projects, after you're done you can use: --reload to download all dependencies again.

To find out the file name, just do:

DENO_DIR=$PWD/modules deno info {package-url}
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
1

I found one way of doing that using the $DENO_DIR variable where your dependencies will be downloaded alongside your project.

This will allow you to set breakpoints on the dependencies, but they are being converted to JavaScript. It worked different in this example.

Set DENO_DIR to your project root directory:

export DENO_DIR=$PWD

Run your script.

deno run --allow-net app.ts

app.ts

import { serve } from "https://deno.land/std/http/server.ts";
const s = serve({ port: 8000 });
console.log("http://localhost:8000/");
for await (const req of s) {
  req.respond({ body: "Hello World\n" });
}

In this example the dependency file for the http lib will be created here:

$DENO_DIR/gen/https/deno.land/std/http/server.ts.js`

You can now create your breakpoints using VS Code.


If you need to configure debug on VS Code for Deno follow my other answer here:

https://stackoverflow.com/a/61856774/3231778

Evandro Pomatti
  • 13,341
  • 16
  • 97
  • 165
  • Nice, thanks! Note that it works for your example, but in the case of the question code (in full JS), the gen dir stays empty, because no compilation step is needed. – Nino Filiu May 20 '20 at 17:31
  • @NinoFiliu I see, did it help or you're still unable to debug the dependencies? – Evandro Pomatti May 21 '20 at 01:14