Want some programming help? [close]

How to solve "Error: Cannot find module '*.js'" with Node.js

Introduction

If you are trying to run your Node.js application and you get something like this:

Error: Cannot find module 'C:\Users\Me\my_app.js'

then you are most likely trying to run the wrong file. It is possible you are missing a dependency that is needed from npm install, but if it says it cannot find the main file you are trying to run, then you are trying to run a file that does not exist. It is a common mistake.

How to fix "Error: Cannot find module"

You need to double check you are running the correct file from the correct directory. Here are some steps to diagnose.

General tips

Here are some general things to keep in mind when diagnosing the issue:

  • Make sure you are in the correct directory.
  • Make sure you are trying to run the correct file name.
  • File and directory names are case sensitive.

How to change directories

To change directories, use the cd command in your terminal. For example, if your username was Me on the computer:

# In Windows
cd C:\Users\Me\my_project_directory\

# In Mac OS
cd /Users/Me/my_project_directory/

# In Linux
cd /home/Me/my_project_directory/

How to see what directory you are in

To check what directory you are currently in, use the folowing in your terminal.

# In Linux/Mac
pwd

# In Windows
echo %cd%

How to print the contents of current directory

To see what files and directories exist in your current directory use the following in your terminal:

# In Windows
dir

# In Linux/Mac
ls

Conclusion

After reading this you should have some idea why you get the error Error: Cannot find module and how to diagnose and fix the problem.

Advertisement