Node.js

Fix Node "Cannot find module"

Node can't find a package or file your code asks for. Usually the packages aren't installed yet or the name is wrong.

Fill these in once
  1. Install the project's packages
    Terminal
    npm install

    Run it in the folder that has package.json. A freshly cloned project has no node_modules until you do.

  2. Add the package if the project doesn't list it
    Terminal
    npm install express

    If the name starts with ./ or ../ it's one of your own files, not a package. Check the path and the capital letters.

  3. Still failing? Reinstall from scratch
    Terminal
    rm -rf node_modules && npm install

Check it worked

  1. Check the package is installed
    Terminal
    npm ls express

    It should show a version number, not (empty).

  1. Install the project's packages
    Terminal · Ubuntu / Debian
    npm install

    Run it in the folder that has package.json. A freshly cloned project has no node_modules until you do.

  2. Add the package if the project doesn't list it
    Terminal · Ubuntu / Debian
    npm install express

    If the name starts with ./ or ../ it's one of your own files, not a package. Linux cares about capital letters, so check the file name matches exactly.

  3. Still failing? Reinstall from scratch
    Terminal · Ubuntu / Debian
    rm -rf node_modules && npm install

Check it worked

  1. Check the package is installed
    Terminal · Ubuntu / Debian
    npm ls express

    It should show a version number, not (empty).

  1. Install the project's packages
    PowerShell
    npm install

    Run it in the folder that has package.json. A freshly cloned project has no node_modules until you do.

  2. Add the package if the project doesn't list it
    PowerShell
    npm install express

    If the name starts with ./ or ../ it's one of your own files, not a package. Check the path and the file name.

  3. Still failing? Reinstall from scratch
    PowerShell
    Remove-Item -Recurse -Force node_modules; npm install

Check it worked

  1. Check the package is installed
    PowerShell
    npm ls express

    It should show a version number, not (empty).

New to dependency? I explain it in plain English on Layman Terms.