1

I am new to node.js. I am debugging one of the server startup script written using typescript backed by various node.js npm modules. IDE I am using VSCode. Is there any way to get all the function execution log. Server startup functions are mostly async functions with a callback. That's why I would like to get all the functions in the order they were executed and which thread is executing the functions.

Could you please let me know is there any way to achieve this instead of writing console.log in every function or putting breakpoints in every functions?

Follwoing is the code used to import and use the tracing library. I have all 3 first line to resolve the issue.

const { njsTrace} = require("NJSTrace");
import * as njsTrace  from 'NJSTrace';
var njsTrace = require('njsTrace.js');

njsTrace.inject();

I have added following to package.json

"dependencies": {
    "@apollo/gateway": "latest",
    "apollo-server": "^2.5.1-alpha.1",
    "graphql": "latest",
    "njstrace": "^1.1.2"
  },
  "typeRoots": [
    "./node_modules/@types",
    "./types"
  ]

I have added below index.d.ts in node_modules/@types/njsTrace

export = NJSTrace;
export as namespace NJSTrace;

declare namespace NJSTrace {}
declare class NJSTrace{
    inject(config: any): NJSTrace;
}

Now I am getting below error

/usr/local/bin/node --inspect-brk=31111 gateway.js 
Debugger listening on ws://127.0.0.1:31111/83e82a13-81df-42bb-9a86-87407cd4b15b
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.
(node:5094) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'inject' of undefined
    at /Users/Debopam/Documents/To Delete/federation-demo-master/gateway.js:19:12
    at Object.<anonymous> (/Users/Debopam/Documents/To Delete/federation-demo-master/gateway.js:28:3)
    at Module._compile (internal/modules/cjs/loader.js:773:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
warning.js:18
(node:5094) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
warning.js:18
(node:5094) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Debopam
  • 3,198
  • 6
  • 41
  • 72
  • I realize it's not *exactly* what you want, but in Javascript you can [override a function while referencing the original one](https://stackoverflow.com/questions/296667/overriding-a-javascript-function-while-referencing-the-original). You could change the API functions you're calling to log when they're called, arguments, return values, etc but otherwise run as normal. You could even construct an error and grab its stack trace to get the call stack. Better than having to write `console.log` at every call site. – Jared Smith Sep 15 '19 at 11:50
  • I was looking at https://www.npmjs.com/package/njstrace, also I followed the instruction to install. but it shows error ..... Could not find a declaration file for module 'njstrace'. '/Users/Debopam/Documents/To Delete/federation-demo-master/node_modules/njstrace/njsTrace.js' implicitly has an 'any' type. Try `npm install @types/njstrace` if it exists or add a new declaration (.d.ts) file containing `declare module 'njstrace';` Could you please help me to get past this error? – Debopam Sep 15 '19 at 20:16
  • If they have a type file on DefinitelyTyped you can install it `npm install --save @types/njstrace`. If that fails you'll have to [write a .d.ts file yourself](https://medium.com/@chris_72272/migrating-to-typescript-write-a-declaration-file-for-a-third-party-npm-module-b1f75808ed2). – Jared Smith Sep 15 '19 at 20:19
  • Still, I am missing something. Added new configuration and index.d.ts and new Error. – Debopam Sep 16 '19 at 01:08
  • You shouldn't need to `require`. Just do `import { inject } from 'njstrace';` and ts-node or whatever should convert it appropriately as long as your tsconfig is correct. – Jared Smith Sep 16 '19 at 01:26

0 Answers0