nodemon app crashed - waiting for file changes before starting

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
5 min

banner

# nodemon app crashed - waiting for file changes before starting

The error "[nodemon] app crashed - waiting for file changes before starting..." occurs for multiple reasons:

  • having multiple, stale Node.js processes running in the background.
  • having an error in your code that causes nodemon to crash.
  • specifying an incorrect path to the file you're trying to run when issuing the nodemon command.
  • trying to connect to a database that is offline causes nodemon to crash.

nodemon app crashed waiting for changes before starting

# Try to restart your Nodemon server

The first thing you should do is try to restart your nodemon server.

Focus your terminal and press Ctrl + C (or Cmd + C on macOS) to stop your development server.

Then start your nodemon server by issuing the following command.

shell
npx nodemon index.js

restart nodemon

The code for this article is available on GitHub

Make sure to replace index.js with the name of your specific file.

# Stopping all Node.js processes on macOS and Linux

You might have multiple Node.js processes running in the background that clash and cause the error.

Try to stop all Node.js processes and restart Nodemon.

On macOS and Linux, open bash and issue the following command.

shell
# for macOS and Linux pkill -f node

stop node processes

If you get a permissions error, try to rerun the command prefixed with sudo.

shell
# for macOS and Linux sudo pkill -f node

An alternative approach to stop all Node.js processes is to issue the following command.

shell
# for macOS and Linux killall -9 node # ๐Ÿ‘‡๏ธ or sudo killall -9 node

stop node processes macos linux

The code for this article is available on GitHub

Restart your nodemon development server after stopping all Node.js processes.

shell
npx nodemon index.js

If you have nodemon installed globally, you can also use it directly, without the npx prefix.

shell
nodemon index.js

# Stopping all Node.js processes on Windows

To stop the Node.js processes on Windows:

  1. Click on the search field, type Task Manager and start the application.

search task manager

  1. In the Processes tab, right click on the Node.js JavaScript Runtime processes and select End Task.

end node js process

Alternatively, you can issue a command from your terminal (CMD) to stop the running Node.js processes.

cmd
taskkill /F /IM node.exe

stop node js process using terminal on windows

Restart your nodemon development server after stopping all Node.js processes.

shell
npx nodemon index.js

If you have nodemon installed globally, you can also use it directly, without the npx prefix.

shell
nodemon index.js
The code for this article is available on GitHub

# Having an error in your code that causes nodemon to crash

The error is also caused if you have an error in your code (unrelated to nodemon).

Here is an example.

index.js
const site = 'bobbyhadz.com'; // โ›”๏ธ Example variable is not defined console.log(example);

The example variable is not defined, so trying to access it causes an error.

Now, I'll start my nodemon server by issuing the following command.

shell
npx nodemon index.js

Here is the output in my terminal.

actual error above the nodemon error-message

Notice that there is another error above the nodemon error.

Make sure to scroll up a bit and check if there is another error message above the nodemon crash error.

The error with the undefined variable has caused my nodemon server to crash.

If you have an error in your code, nodemon crashes instantly and is not able to respond to requests.

The only way to resolve the issue is to solve the error and restart nodemon.

In many cases, even after resolving the error in your code, nodemon isn't going to restart itself.

You can press Ctrl + C (or Cmd + C on macOS) to manually stop the server.

After the error is resolved, start nodemon by issuing the following command.

shell
npx nodemon index.js
The code for this article is available on GitHub

A common source of errors is when you forget to initialize environment variables in your code but try to access them.

If you try to access environment variables (process.env.X) that you haven't initialized:

  1. they will be undefined
  2. an error will occur
  3. nodemon will crash

Other common sources of errors are incorrect import statements.

index.js
// โ›”๏ธ Error module is NOT called azios const axios = require('azios');

The example above causes the error because we have misspelled the name of the module.

index.js
// โœ… correct import statement const axios = require('axios');

# Specifying an incorrect path to the file you're trying to run

The error is also caused if you specify an incorrect path to the file you're trying to run.

For example, here is the scripts section of my package.json file.

package.json
"scripts": { "dev": "nodemon index.js", }

The dev script points nodemon to a file called index.js that is located in the same directory as the package.json file.

shell
my-project โ””โ”€โ”€ index.js โ””โ”€โ”€ package.json

However, my index.js file is located in an app directory.

shell
my-project โ””โ”€โ”€ app โ””โ”€โ”€ index.js โ””โ”€โ”€ package.json

index file in app directory

If I try to issue the npm run dev command, the "[nodemon] app crashed" error occurs.

nodemon incorrect path

The code for this article is available on GitHub

Notice that there is another error above the nodemon error.

Nodemon cannot find the index.js file at the specified location.

One way to solve the error is to update the dev command in my package.json file.

package.json
"scripts": { "dev": "nodemon app/index.js" },

Now the command points to an index.js file located in the app directory.

You can also use the ./ prefix when specifying the path.

package.json
"scripts": { "dev": "nodemon ./app/index.js" },

The command assumes the following folder structure.

shell
my-project โ””โ”€โ”€ app โ””โ”€โ”€ index.js โ””โ”€โ”€ package.json

Alternatively, you can move your index.js file to the root directory of your project, right next to your package.json file.

Assuming you have the following folder structure.

shell
my-project โ””โ”€โ”€ index.js โ””โ”€โ”€ package.json

Then your dev command would look as follows.

package.json
"dev": "nodemon index.js",

index file in root directory

The code for this article is available on GitHub

# Trying to connect to a database that is offline

Another common cause of the error is trying to connect to a database (e.g. MongoDB or MySQL) that is offline.

Make sure that your database is up and running before you start your nodemon server, otherwise an error is raised and nodemon crashes.

# Conclusion

To solve the error "[nodemon] app crashed - waiting for file changes before starting...", make sure:

  1. You don't have any errors in your code (e.g. syntax, reference or type errors).
  2. You haven't specified an incorrect path when issuing the nodemon command.
  3. You aren't trying to connect to a database that is offline.

Finally, try to restart your nodemon server.

Focus your terminal and press Ctrl + C (or Cmd + C on macOS) to stop your development server.

Then start your nodemon server by issuing the following command.

shell
npx nodemon index.js

Make sure to replace index.js with the name of your specific file.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev