Skip to main content
Redhat Developers  Logo
  • Products

    Featured

    • Red Hat Enterprise Linux
      Red Hat Enterprise Linux Icon
    • Red Hat OpenShift AI
      Red Hat OpenShift AI
    • Red Hat Enterprise Linux AI
      Linux icon inside of a brain
    • Image mode for Red Hat Enterprise Linux
      RHEL image mode
    • Red Hat OpenShift
      Openshift icon
    • Red Hat Ansible Automation Platform
      Ansible icon
    • Red Hat Developer Hub
      Developer Hub
    • View All Red Hat Products
    • Linux

      • Red Hat Enterprise Linux
      • Image mode for Red Hat Enterprise Linux
      • Red Hat Universal Base Images (UBI)
    • Java runtimes & frameworks

      • JBoss Enterprise Application Platform
      • Red Hat build of OpenJDK
    • Kubernetes

      • Red Hat OpenShift
      • Microsoft Azure Red Hat OpenShift
      • Red Hat OpenShift Virtualization
      • Red Hat OpenShift Lightspeed
    • Integration & App Connectivity

      • Red Hat Build of Apache Camel
      • Red Hat Service Interconnect
      • Red Hat Connectivity Link
    • AI/ML

      • Red Hat OpenShift AI
      • Red Hat Enterprise Linux AI
    • Automation

      • Red Hat Ansible Automation Platform
      • Red Hat Ansible Lightspeed
    • Developer tools

      • Red Hat Trusted Software Supply Chain
      • Podman Desktop
      • Red Hat OpenShift Dev Spaces
    • Developer Sandbox

      Developer Sandbox
      Try Red Hat products and technologies without setup or configuration fees for 30 days with this shared Openshift and Kubernetes cluster.
    • Try at no cost
  • Technologies

    Featured

    • AI/ML
      AI/ML Icon
    • Linux
      Linux Icon
    • Kubernetes
      Cloud icon
    • Automation
      Automation Icon showing arrows moving in a circle around a gear
    • View All Technologies
    • Programming Languages & Frameworks

      • Java
      • Python
      • JavaScript
    • System Design & Architecture

      • Red Hat architecture and design patterns
      • Microservices
      • Event-Driven Architecture
      • Databases
    • Developer Productivity

      • Developer productivity
      • Developer Tools
      • GitOps
    • Secure Development & Architectures

      • Security
      • Secure coding
    • Platform Engineering

      • DevOps
      • DevSecOps
      • Ansible automation for applications and services
    • Automated Data Processing

      • AI/ML
      • Data Science
      • Apache Kafka on Kubernetes
      • View All Technologies
    • Start exploring in the Developer Sandbox for free

      sandbox graphic
      Try Red Hat's products and technologies without setup or configuration.
    • Try at no cost
  • Learn

    Featured

    • Kubernetes & Cloud Native
      Openshift icon
    • Linux
      Rhel icon
    • Automation
      Ansible cloud icon
    • Java
      Java icon
    • AI/ML
      AI/ML Icon
    • View All Learning Resources

    E-Books

    • GitOps Cookbook
    • Podman in Action
    • Kubernetes Operators
    • The Path to GitOps
    • View All E-books

    Cheat Sheets

    • Linux Commands
    • Bash Commands
    • Git
    • systemd Commands
    • View All Cheat Sheets

    Documentation

    • API Catalog
    • Product Documentation
    • Legacy Documentation
    • Red Hat Learning

      Learning image
      Boost your technical skills to expert-level with the help of interactive lessons offered by various Red Hat Learning programs.
    • Explore Red Hat Learning
  • Developer Sandbox

    Developer Sandbox

    • Access Red Hat’s products and technologies without setup or configuration, and start developing quicker than ever before with our new, no-cost sandbox environments.
    • Explore Developer Sandbox

    Featured Developer Sandbox activities

    • Get started with your Developer Sandbox
    • OpenShift virtualization and application modernization using the Developer Sandbox
    • Explore all Developer Sandbox activities

    Ready to start developing apps?

    • Try at no cost
  • Blog
  • Events
  • Videos

Node.js serverless functions on Red Hat OpenShift, Part 3: Debugging on a cluster

December 8, 2021
Lucas Holmquist
Related topics:
ContainersKubernetesNode.jsServerless
Related products:
Red Hat OpenShiftRed Hat OpenShift Container Platform

Share:

    This article is the third in a series about running Node.js applications in containers in an orchestration environment such as Kubernetes or Red Hat OpenShift. The first article focused on control over logging, and the second article on debugging JavaScript programs on your local system. This time, we'll look at how to use the Chrome DevTools inspector to debug a Node.js function that is running inside a container on an OpenShift cluster.

    Prerequisites

    To follow along, you will need an OpenShift cluster with the Serverless Operator installed. See the article, Create your first serverless function with Red Hat OpenShift Serverless Functions to set up this environment.  

    If you need an Openshift cluster to play around with that already has the serverless operators installed and setup, you can easily create an instance of the Openshift Sandbox.  This post won’t go into setting up the sandbox, but  the process is fairly straightforward.

    This article also assumes some familiarity with the Chrome DevTools inspector. For an introduction, please see How to debug your Node.js application on OpenShift with Chrome DevTools.

    Get the example code

    The example that we are going to use can be retrieved from the Node.js HTTP function repository on GitHub. Similar to the example used in our previous article, this function application was scaffolded with the kn-func command-line tool, which we described in that article.

    We'll have to make a few changes before using the code for this article. To start, note that the package.json file for this example defines three scripts:

    "scripts": {
    
        "test": "node test/unit.js && node test/integration.js",
    
        "start": "faas-js-runtime ./index.js",
    
        "debug": "nodemon --inspect ./node_modules/faas-js-runtime/bin/cli.js ./index.js"
    
      }

    These scripts are similar to the ones in the previous article, and we will make the same changes here that we did previously.

    First, simply switch the debug script with the start script. We make this change because the kn-func command can’t specify which script to run. You can name the start script whatever you like.

    Next, make a simple change to the start script. You need to tell it to listen on all available addresses because you are running as localhost (IP address 127.0.0.1) inside the container, which the debugger can’t access by default. Therefore, change the --inspect option in the start script to --inspect=0.0.0.0.

    These changes should produce scripts similar to the following:

    "scripts": {
    
        "test": "node test/unit.js && node test/integration.js",
    
        "debug": "faas-js-runtime ./index.js",
    
        "start": "nodemon --inspect=0.0.0.0 ./node_modules/faas-js-runtime/bin/cli.js ./index.js"
    
      }

    Debugging Node.js functions in a container

    Now, create a container using the kn-func build command. If this is the first time you are building the application, the command prompts you to add a registry and namespace for the containerized function. By default, the registry is Docker Hub. For the namespace, enter your Docker Hub ID.

    Once the image is built, use the docker command to run the container and start debugging your Node.js functions. Since the debugger listens on port 9229, you need to expose that port as well as port 8080, which is the port to access your application:

    $ docker run --rm  -p 8080:8080 -p 9229:9229 lholmquist/debugging-with-functions-on-cluster:latest

    The output should be similar to:

    Debugger listening on ws://0.0.0.0:9229/584eb679-4db1-4a40-9519-5bf5c42275f5
    
    For help, see: https://nodejs.org/en/docs/inspector
    
    The server has started. http://localhost:8080

    Now that the container is running, you can open Chrome DevTools and navigate to the chrome://inspect URL, where you should see a link labeled inspect (Figure 1). Click this link to connect to your running container.

    The Chrome inspector offers an inspection link to let you view and debug your program.
    Figure 1. Use the Chrome inspector to view and debug your program.

    Now you should see the Chrome inspector, showing code similar to Figure 2.

    The Chrome inspector shows the code of your application and allows you to set breakpoints.
    Figure 2. Use the Chrome inspector to set breakpoints.

    Set a breakpoint at some point in the program, then navigate to http://localhost:8080 to run the application. The inspector stops at the breakpoint shown in Figure 3, allowing you to inspect variables and do other debugging tasks.

    The program shows the breakpoint where it stopped.
    Figure 3. The program shows the breakpoint where it stopped.

    Debugging Node.js functions on an OpenShift cluster

    Having debugged your program in your container, you can use a similar process to debug it on an OpenShift cluster. Make the same changes in the npm scripts and use the same command to build the container. To deploy the container to the cluster, use the kn-func command:

    $ kn-func deploy

    Once the container is deployed, navigate to the topology view in the OpenShift console, which should show you something like Figure 4.

    The topology view of a function deployed in OpenShift shows an icon you can click to run the function.
    Figure 4. Deploy the function from the OpenShift topology view.

    Clicking the icon on the top-right corner of the function's box navigates to the application's route. You should then see the JSON output that the function sends when invoked.

    To start debugging, you need the help of the oc port-forward command. This command was described in the article How to debug your Node.js application on OpenShift with Chrome DevTools, so I won't explain here what each part of the command does. For this example, your command should look something like this:

    $ oc port-forward $(oc get po | grep debugging-with-functions | grep Running | awk '{print $1}') 8888:9229

    The command should start forwarding requests to the debugger process. If it is successful, you'll see log messages similar to these:

    Forwarding from 127.0.0.1:8888 -> 9229
    Forwarding from [::1]:8888 -> 9229
    Handling connection for 8888
    Handling connection for 8888

    With forwarding enabled, navigate again in your browser to the chrome://inspect URL and you should see something similar to Figure 5.

    The "inspect" link in Chrome inspector also allows you to debug on a cluster.
    Figure 5. The "inspect" link in Chrome inspector also allows you to debug on a cluster.

    As in the previous example with the container, clicking the inspect link should show the debugger. Again add a breakpoint, then navigate to the route that OpenShift provides to access the application. The debugger should break on the point you added, as shown in Figure 6.

    The program stops at the breakpoint in the cluster.
    Figure 6. The program stops at the breakpoint in the cluster.

    Conclusion

    This article showed how to debug a Node.js application while running inside a container, as well as running on an OpenShift cluster.

    Stay tuned for more posts related to running Node.js applications on Red Hat OpenShift Serverless. You can also check out the latest documentation at the About OpenShift Serverless Functions site.

    If you want to learn more about what Red Hat is up to on the Node.js front, check out our Node.js landing page.

    Last updated: January 12, 2024

    Related Posts

    • Node.js serverless functions on Red Hat OpenShift, Part 1: Logging

    • Node.js serverless functions on Red Hat OpenShift, Part 2: Debugging locally

    • How to Debug Your Node.js Application on OpenShift with Chrome DevTools

    • Create your first serverless function with Red Hat OpenShift Serverless Functions

    Recent Posts

    • Meet the Red Hat Node.js team at PowerUP 2025

    • How to use pipelines for AI/ML automation at the edge

    • What's new in network observability 1.8

    • LLM Compressor: Optimize LLMs for low-latency deployments

    • How to set up NVIDIA NIM on Red Hat OpenShift AI

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Products

    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform

    Build

    • Developer Sandbox
    • Developer Tools
    • Interactive Tutorials
    • API Catalog

    Quicklinks

    • Learning Resources
    • E-books
    • Cheat Sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site Status Dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Report a website issue