@chanand here’s the best solution we have, currently, for debugging in Node. It requires that you have a Chrome browser.
First, add this script to the project’s package.json
file:
"scripts": {
"debug": "node --inspect $(npm root -g)/8base-cli/dist/index.js "
}
Essentially, it runs node --inspect, and then finds the global npm 8base-cli
package on your machine, building a path to the executable. Whether you’re using yarn or npm won’t matter.
To run a debugger with the 8base command, you then can now use:
npm run debug invoke-local FUNCTION_NAME -p /path/to/mock.json
// or
yarn debug invoke-local FUNCTION_NAME -p /path/to/mock.json
However, you’ll need to open Chrome and go to chrome://inspect
. Click the Open dedicated DevTools for Node link.
The window where you’re debugger
will be stopped! Now if you put a debugger in your code, it will pause execution at the appropriate line!
export default async (event: any, ctx: any) : Promise<LocalizeResult> => {
// some code
debugger;
// more code.
};
There is one caveat that you’ll need to be aware of here. The code being debugged is the built code, which is why the script being debugged will look pretty unfamiliar.
Know that you have full access to the same scope, but any imported modules or function arguments might be assigned different names. If you want to inspect a module, it’s better to assign it to a variable so that the name will be the same. For example;
import AWS from 'aws-sdk';
export default async (event: any, ctx: any) : Promise<LocalizeResult> => {
// Want to inspect the AWS module?
const aws = AWS;
// Now in the debugger you can be sure aws variable is available.
debugger;
// more code
};
Let me know any questions about this!