Node.js allows vendors and organizations to create different modules to make the developer’s life easier and bring automation. These modules can be installed globally in your system or the user can install it locally inside a specific project directory via “npm” according to requirements. One of these modules, the most popular module, is “nodemon”. This module helps automate the development process for Node.js applications and also offers an error-finding feature.
This guide explains the fixes for the “[nodemon] app crashed – waiting for file changes before starting” error:
- What is “[nodemon] app crashed – waiting for file changes before starting” Error
- Prerequisite
- How to Fix “[nodemon] app crashed – waiting for file changes before starting” Error?
- Conclusion
What is “[nodemon] app crashed – waiting for file changes before starting” Error
The “nodemon” module compiles the whole code at first and then performs the execution cycles. After that, it watches for any changes in the file and performs automatic compilation whenever a change happens. The “ error arises if the nodemon finds any syntax, logical, or arithmetic errors during the compilation process. The breakdown of the mentioned error is stated below:
- The “nodemon” is our node.js module that monitors the changes happening in the current project directory and automatically performs execution whenever the changes happen.
- The “app crashed” means that during the compilation phase, there is an occurrence of some logical or syntax error that leads to the runtime error. Due to this error, the nodemon is unavailable to complete its execution cycle, and the app has now crashed.
- The “waiting for file changes” part shows that nodemon is still working, and it allows you to modify the code in order to resolve the arisen error.
- The “before starting” part of the error, illustrates that the “nodemon” is still working, and it is in the waiting state. It will restart the execution process whenever the file gets modified and saved by the user.
Prerequisite
As a prerequisite, you should have set the script for your specific file that is going to be executed by the “nodemon”. The file is set by traversing to the “package.json” file and inside its “scripts{}” section declare your nodemon script. For instance, the “start” named script is set to execute the “app.js” using “nodemon”:
"start": "nodemon app.js"
},
Now, to start the nodemon feature the developer needs to execute the “npm start” command. The codes that are going to be demonstrated in the below section are going to be inserted inside the “app.js” file in our case.
How to Fix “[nodemon] app crashed – waiting for file changes before starting” Error?
There are multiple solutions to resolve the stated error depending on the specific scenario. This error can be generated due to various causes and for each cause there is a different solution. These causes and their relative solutions are demonstrated below:
- Cause 1: Syntax Error
- Solution 1: Fix the Syntax Error
- Cause 2: Importing Uninstalled Modules
- Solution 2: Install the Modules
- Cause 3: Missing Modules or Files
- Solution 3: Check the Imported Files and Module
- Cause 4: Configuration Issue
- Solution 4: Debug Your Source Code
- Cause 5: Wrong Script for Nodemon
- Solution 5: Checking the Nodemon Script
- Cause 6: Utilization of Large Data Structures
- Solution 6: Write Optimized Code
- Solution 7: Restarting the Nodemon Service
- Solution 8: Stopping All Node.js Processes
Cause 1: Syntax Error
If there is any syntax error residing inside the project file code, then this error will be generated:
console.log(message
Execute the file that contains the above code via:
The output shows that the “[nodemon] app crashed – waiting for file changes before starting…” error is now generated:
Solution 1: Fix the Syntax Error
In order to resolve the generated error, re-visit the code and solve the syntax issue. In our case, the syntax error is raised due to missing the “)” token which is resolved as:
console.log(message);
After the modification restart the “nodemon” or simply save the file if the “nodemon” service is not canceled and the output looks like this:
Cause 2: Importing Uninstalled Modules
The same error gets generated if the user tries to import the module that is already uninstalled or the one that is not yet installed:
Solution 2: Install the Modules
To fix the mentioned error that arises due to not available modules. Install the module globally or locally on which the error is occurring like the “single” module needs to be installed in our case. The command to install any package is shown below:
In our case, the “single” module needs to be installed, so the above command modifies like this:
The generated output shows, the installation of a “single” module in the node.js project:
Now, restart the “nodemon” service and the raised error will be resolved like this:
Cause 3: Missing Modules or Files
The error may also be generated while importing the wrong-named, invalid module or uncreated file inside the targeted file. In the below code snippet, the imported module name is written wrong and the file that is being imported does not even exist:
const invalidFile = require('../models/constraints');
Now, the “[nodemon] app crashed – waiting for file changes before starting” error will be generated when the nodemon server gets started via:
The below GIF shows the imported module and file do not exist and the error gets raised when the “nodemon” service gets enabled:
Solution 3: Check the Imported Files and Module
The user can traverse through the provided file manually to confirm the existence of the provided file. To verify the validity of modules, open the “package.json” file and inside the “dependencies” section confirm whether the module that is being imported exists or not and their spelling as well.
Cause 4: Configuration Issue
If there is a wrong assignment of values at the configuration time, like the listening of an “express” app on the invalid port number or on the already occupied port number. This raised our above-discussed error:
const expApp1 = expObj();
const expApp2 = expObj();
const port = 3000;
expApp1.listen(port, () => {
console.log(`Server is started on http://localhost:${port}`);
});
expApp2.listen(port, () => {
console.log(`Server is started on http://localhost:${port}`);
});
In the above code, two express applications named “expApp1” and “expApp2” are created and the “port” variable is defined as having the value of “3000”. Next, “listen” or call both applications over the local host having the same port number of “3000” and this will generate our above-discussed error:
Solution 4: Debug Your Source Code
The better solution to this issue is the utilization of the load balancing concept and rechecking your source code multiple times. In our case, the same port number is used multiple times so the better way is to use proxies or try setting the server at different port numbers.
Cause 5: Wrong Script for Nodemon
To start the “nodemon” a separate script is needed to be set in the package.json file. This script runs the provided file under the nodemon service and this service watches or executes the provided only when the specific scripts will be executed via “npm”. The “[nodemon] app crashed – waiting for file changes before starting” error will also invoke if the provided path of the file to be executed under the service is invalid like:
"start": "nodemon invalidApp.js"
},
Now, run the nodemon service by executing its custom-defined script which is written below:
The output shows that the discussed error has been generated:
Solution 5: Checking the Nodemon Script
The most efficient approach to resolve this issue is by traversing to the “scripts” option in the “package.json”. Then, ensure that the “nodemon” script is written well, and it is pointing to the targeted valid file. In our case, the nodemon service should be pointing or executing the “app.js” file, so the code in the “scripts” section modifies like this:
"start": "nodemon app.js"
},
Now, restart the service by executing the same script:
The generated output shows that the occurred error has been resolved and the targeted “app.js” file is executed successfully:
Cause 6: Utilization of Large Data Structures
If there is the utilization of a large data structure that leads to heap or memory issues, then the “[nodemon] app crashed – waiting for file changes before starting” gets raised. For instance, the empty array is created then the “for” loop is used to append large numbers of data in the array which causes the array size to exceed the limit. This exceeds of heap memory leads to our “nodemon -app crashed-” error:
for (let k = 0; k < 99999999999999; k++) {
largeData.push(k);
}
After executing, the nodemon script, the error arises like this:
Solution 6: Write Optimized Code
Write more optimized and structured code, and divide the single code into multiple chunks to enable a low coupling feature. There should be no infinite loop or unhandled exception in the code.
Solution 7: Restarting the Nodemon Service
If the mentioned error still persists after applying all fixes. Then simply cancel the service by pressing the “Ctrl +C” key from the keyboard and restart the service by executing the specified script:
Solution 8: Stopping All Node.js Processes
Another alternative approach to fixing the “[nodemon] app crashed – waiting for file changes before starting” error is to stop all the Node.js processes and restart them. This will refresh the “nodemon” service and repair system fixes on its own. The processes are stopped or killed by executing the below-stated command:
The generated output shows that all currently executing “node.js” processes are terminated:
That’s all about the fixing of the “[nodemon] app crashed – waiting for file changes before starting” error.
Conclusion
To fix the “[nodemon] app crashed – waiting for file changes before starting” error, the developer must check their code for syntax errors, logical errors, unhandled exceptions, executed files, imported module names, and module paths. Or the user can simply delete all running node.js processes by executing the “taskkill -f -im node*” command. This guide has explained the fixes for the “[nodemon] app crashed – waiting for file changes before starting” error.