PositiveGuy
PositiveGuy

Reputation: 20182

Imported module undefined during Debug

I know this code is working. But for whatever reason at times I'm not able to evaluate variables with WebStorm.

PgMem.ts

import { newDb } from 'pg-mem';
import fs from 'fs';

const inMemoryDb = newDb();
inMemoryDb.public.none(fs.readFileSync('src/DB/pgMem/migrations/001-initial.sql', 'utf8'));
export default inMemoryDb;

DBConnection.ts

import inMemoryDb from './pgMem/PgMem';
...

if (queryTypeId === queryType.MANY) {
  res = await inMemoryDb.public.many(query);
}

If I set a breakpoint on the line with res and it hits that breakpoint, inMemoryDB shows undefined during WebStorm debug. I know that this code works without debugging so why would it say undefined if I try to use the evaluate tool or watch on that?

I thought when you import a module like that, it runs the code in the module that's imported so it already would have run newDb(); and have an instance to work with which apparently it does, but just when I debug in WebStorm for this particular scenario, it's always undefined

Upvotes: 0

Views: 640

Answers (2)

Vicky_4899
Vicky_4899

Reputation: 1

I've had similar scenarios. See if this helps

import {fs as fs} from 'fs';

something like this should avoid this issue while debugging

Upvotes: 0

lena
lena

Reputation: 93738

Undefined variables while debugging must be caused by wrong/missing name mappings in sourcemaps: if the variable is renamed while transpiling/obfuscating, and no appropriate name mapping is provided, the debugger won't be able to match variable in source code with the one in VM.

For example, import somefunction from './somefunc' is usually compiled to

Object.defineProperty(exports, "__esModule", { value: true });
var somefunc_1 = require("./somefunc");
somefunc_1.default();

and no name mappings are generated ("names":[] in generated sourcemap), so the debugger can't match the variable in .ts file with the code in runtime and shows undefined

Tickets that can be related: https://github.com/microsoft/TypeScript/issues/9627, https://github.com/webpack/webpack/issues/3957, https://github.com/babel/babel/issues/1468

Upvotes: 1

Related Questions