-
Notifications
You must be signed in to change notification settings - Fork 115
Debugging with Chrome DevTools
Table of Contents
- The Chrome DevTools Debugger
- Debugging functions in
childprocess
isolation mode - Debugging functions in
inprocess
isolation mode - Additional Chrome DevTools reading
The Chrome DevTools are a set of web authoring and debugging tools built into Google Chrome. As of Node.js v6.3.1 you can use the DevTools to debug and profile your Node.js code.
In childprocess
isolation mode, functions are invoked in their own processes, separate from the main Emulator process. This is more secure and safer for the Emulator, but it means the debugger is started on function invocation.
-
Switch to
childprocess
isolation mode.functions config set isolation childprocess
-
Enable debugging:
functions config set inspect true
-
Restart the Emulator:
functions restart
You should see something like the following printed to the console:
Inspect mode is enabled for the Supervisor. During function execution the debugger will listen on port 9229 and the Chrome debugging URL will be printed to the console.
-
Deploy your function, if you haven't already.
-
You should see something like the following printed to the console:
Function execution paused. Connect to the debugger on port 9229 (e.g. using the "node2" launch type in VSCode), or open the following URL in Chrome: chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=localhost:9229/fae86ac1-2e59-46b8-9b08-3e997104662c
-
Open the printed
chrome-devtools
URI into a new tab in Chrome. -
Script execution will be paused on the first line of the Emulator's
worker.js
file, seen below: -
At this point your function code has not yet been loaded into the worker.
Click "Resume script execution" once to load your function into the debugger prior to invoking your function. The script will pause again right before the line that would invoke your function, seen below.
Take this opportunity to find your source code files in the left pane's file navigator in order to set breakpoints within your code.
-
Click "Resume script execution" again to invoke your function and move the debugger into your code, seen below:
-
Close the Chrome tab when your function exits to close down the debugger and release your terminal, which will print any function output.
In inprocess
isolation mode, functions are invoked within the main Emulator process, which is possibly dangerous to the Emulator, but allows a single debugger instance to handle multiple function invocations.
-
Switch to
inprocess
isolation mode.functions config set isolation inprocess
-
Enable debugging:
functions config set inspect true
-
Restart the Emulator:
functions restart
You should see something like the following printed to the console:
Started in inspect mode. Connect to the debugger on port 9229 (e.g. using the "node2" launch type in VSCode), or open the following URL in Chrome: chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=localhost:9229/87d62bf0-e12e-43f5-96ec-e253e45ebf34
-
Open the printed
chrome-devtools
URI into a new tab in Chrome. -
Deploy your function, if you haven't already.
-
You should see something like the following printed to the console:
Function execution paused. Connect to the debugger on port 9229 (e.g. using the "node2" launch type in VSCode).
-
Script execution will be paused right before the line that would invoke your function, seen below.
Take this opportunity to find your source code files in the left pane's file navigator in order to set breakpoints within your code.
-
Click "Resume script execution" again to invoke your function and move the debugger into your code, seen below:
- https://nodejs.org/api/debugger.html#debugger_v8_inspector_integration_for_node_js
- https://medium.com/@paul_irish/debugging-node-js-nightlies-with-chrome-devtools-7c4a1b95ae27
- https://mattdesl.svbtle.com/debugging-nodejs-in-chrome-devtools
- https://blog.hospodarets.com/nodejs-debugging-in-chrome-devtools
Disclaimer: This is not an official Google product.
@google-cloud/functions-emulator is currently in pre-1.0.0 development. Before the 1.0.0 release, backwards compatible changes and bug fixes will bump the patch version number and breaking changes will bump the minor version number.