Resolve “Non-std C++ Exception” in React Native with the “lsof” Command

React Native is a powerful framework for building cross-platform mobile applications. However, like any technology, a developer has to work through occasional errors. One such error is the “Non-std C++ Exception,” often related to multiple Metro bundlers running simultaneously. In this blog post, we’ll explore this error and its solution, focusing on the “lsof” command and its various parameters: -t,-i, and xargs kill.

Understanding the Solution: Killing a Metro Server

The solution to the “Non-std C++ Exception” error involves gracefully terminating the Metro server for the specific port (usually 8081) and then relaunching the application. To accomplish this, we employ a series of commands, starting with ‘lsof,’ which stands for “list open files.” Let’s dissect each part of the solution:

sudo lsof -i :8081 | xargs kill -9

  • sudo: In some cases, you might need superuser privileges to terminate certain processes. Using ‘sudo’ grants the necessary permissions.

  • lsof: The ‘lsof’ command helps us identify the process currently using port 8081.

  • -i :8081: With the ‘-i’ option, we specify the type of network connections to include in the output. In this case, ‘-i’ is followed by ‘:8081,’ indicating that we want to target the Internet address using port 8081. This narrows down the ‘lsof’ search to the process relevant to our React Native development server.

  • |: The pipe symbol (‘|’) is used to pass the output of the ‘lsof’ command to the next command, ‘xargs kill -9.’ This allows us to target and terminate the identified process.

  • xargs kill -9: Once we have identified the PID of the Metro server using port 8081, ‘xargs’ reads the PID and passes it as an argument to the ‘kill’ command. ‘-9’ is the signal indicating a forceful termination, which might be necessary in cases where the server is unresponsive.

The “Non-std C++ Exception” Error

Here’s why multiple metro servers can cause the “Non-std C++ Exception” error. A Metro server, also known as the “Metro Bundler,” is a critical component in the React Native development process. When you run your React Native project, the Metro server compiles your code, bundles it, and serves it over a specified port, usually 8081, to your mobile app. Running multiple Metro servers can lead to conflicts because these servers compete for the same port, causing the “Non-std C++ Exception” error.

How to Find Running Ports

To identify running ports and processes on your system, you can use the following command:

sudo lsof -i -P -n | grep LISTEN

  • sudo lsof: This initiates the ‘lsof’ command with superuser privileges.

  • -i: It lists all network files, which includes open ports.

  • -P: Prevents the translation of port numbers to service names.

  • -n: Avoids hostname resolution, which can speed up the command.

  • | grep LISTEN: Filters the output to display only the ports that are in a listening state.

This command provides you with a list of open ports and the processes associated with them. This information can help you determine which ports are running and which ones may need to be terminated if you encounter the “Non-std C++ Exception” error.

Resolving the “Non-std C++ Exception” Error

React Native development is filled with its share of challenges, but understanding how to resolve common issues is part of the journey. The “Non-std C++ Exception” error can be a roadblock, but with the ‘lsof’ command and its options, you can efficiently pinpoint and terminate the problematic Metro server, getting your development environment back on track.

Conversation

Join the conversation

Your email address will not be published. Required fields are marked *