Skip to content

Module not found error: why it happens and how to fix it

Easy 10 min Updated:

Quick answer

The Module not found error means the program cannot find the module or package it is trying to load. The most common causes are a wrong name or path, the module not being installed, the wrong working directory, or a mismatched environment. The solution is to check the import, install the package, and run the project from the correct folder.

How to recognize the problem

  • Message `ModuleNotFoundError: No module named 'x'` in Python
  • Message `Error: Cannot find module 'x'` in Node.js
  • The application crashes immediately after starting
  • The error occurs only on another machine or in a CI environment
  • The import works in the editor but not from the terminal

Most common causes

Module is not installed

The package the code is looking for is not present in the environment. This often happens when `npm install` or `pip install` is not run after downloading the project.

Wrong name or path

The import contains a typo, a wrong relative path, or a missing `__init__.py` in a Python package. Node.js looks for the exact module name in `node_modules`.

Wrong working directory

The command is executed from a folder that is not the project root, so `node_modules` or the virtual environment are not visible. Check which folder you are in before running.

Mismatched environment

You are using the global Python instead of the virtual environment, or a different Node.js version than the one the project expects. Dependencies are then not visible.

Cache or installation issues

A partially installed package, a corrupted `node_modules`, or an outdated `package-lock.json` can cause the module to exist but not be available.

Step-by-step fix

  1. 1

    Read the exact module name from the error

    The terminal message states the name of the missing module. Write it down and compare it with the import line in the code. In Node.js the error usually starts with Error: Cannot find module, and in Python with ModuleNotFoundError.

  2. 2

    Check that you are in the project root

    Run pwd (macOS/Linux), cd in the Windows command prompt, or Get-Location in PowerShell to see the current folder. It should contain package.json for Node.js or requirements.txt/pyproject.toml for Python. If not, navigate to the correct folder with cd path/to/project.

  3. 3

    Install dependencies

    For Node.js, run npm install in the project root. For Python, activate the virtual environment and then run pip install -r requirements.txt. If the module is still missing, install it individually: npm install package-name or pip install package-name.

  4. 4

    Check the import and path

    Make sure the import matches the actual package name and folder structure. In Python, relative imports depend on where the module is run from; in Node.js, check that the path starts with ./ for local files. Pay attention to upper and lower case.

  5. 5

    Clear the cache and reinstall

    If the module exists but is not visible, back up the project or check that it is saved in a Git repository before deleting. Then delete node_modules and package-lock.json and run npm install again; deleting package-lock.json can change dependency versions. In Python, delete __pycache__ folders and reinstall the package. The process may take a while.

  6. 6

    Check the language version and environment

    Run node -v or python --version and compare with the project requirements. In Python, check that the virtual environment is activated (the prompt shows its name). In Node.js, use nvm if you need to switch versions.

  7. 7

    Test in isolation

    Create a minimal script that only imports the module and run it. If it still fails, the problem is in the installation or environment, not the rest of the code. If it works, the error is in the path or how the main application is started.

When nothing helps

If you have gone through all the steps and the error persists, check whether the project uses monorepo or workspace settings, where packages are located in another folder. Also check whether a global installation is masking the lack of a local one. In Node.js, run npm ls package-name to see if the package is present in the dependency tree at all. In Python, pip show package-name gives the installation location. If the error occurs only in the CI system, compare the language versions and installation steps with your local environment. As a last option, create a new virtual environment or a fresh clone of the project and repeat the installation.

How to prevent it

  • Always run `npm install` or `pip install -r requirements.txt` after downloading the project
  • Lock dependency versions (`package-lock.json`, `requirements.txt`) and keep them in the repository
  • Use a virtual environment for Python and `nvm` for Node.js
  • Check that you are in the project root before running commands

When to call a professional

If the error originates from a complex build system, monorepo, or CI configuration, or keeps recurring despite all the steps, contact IT support to check the environment and dependencies together.

Software and product development · Custom apps, SaaS and an MVP in 4 to 8 weeks

FAQ

Why do I get Module not found when the package is installed?

Most often because the command uses a different environment from the one where the package is installed. Check whether the virtual environment is active or whether you are in the correct project folder.

Is it safe to delete node_modules and package-lock.json?

Usually yes, but first back up the project or check that it is saved in a Git repository. Deleting `package-lock.json` can change dependency versions; after deleting, run `npm install` again so dependencies are installed from `package.json`.

How do I know which module is missing if the error is not clear?

Run the application from the terminal and read the last line of the error; the module name is usually there. In Python, you can run `python -v` for a more detailed import output.

Share
Didn't find a fix?The AI IT assistant is available 24/7. Describe the problem or send a screenshot of the error and get instructions for your case.Ask the AI IT assistant