Module not found error for database package despite successful npm installation

I’m working on a Node.js project where I need to connect to a database service API. I installed the required package using npm, but I keep getting a module not found error when trying to import it.

Here’s my import statement:

const DatabaseClient = require('database-client');

When I run my file with node myapp.js, I get this error:

Error: Cannot find module 'database-client'

I already ran the install command:

npm install database-client

The package shows up in my package.json file:

{
  "name": "my-project",
  "version": "1.0.0",
  "main": "server.js",
  "dependencies": {
    "database-client": "^0.8.1",
    "express": "^4.18.0"
  }
}

I expected my server to start on port 3000, but it fails right at the require statement. What could be causing this issue even though the module appears to be properly installed?

I’ve hit this exact issue multiple times. Package shows installed but Node can’t find it.

First - check where you’re running the command. If you’re running node myapp.js from a parent directory, Node won’t find your local node_modules.

Run pwd (or cd on Windows) to make sure you’re in the same directory as your package.json.

Sometimes npm installs fail silently. Try npm install --verbose database-client to catch any hidden warnings or network issues.

Check for .npmrc files that might redirect to a different registry. I once debugged this for hours before finding our corporate proxy was blocking packages.

Last resort: npm cache clean --force, delete node_modules, and reinstall everything. Annoying but works 90% of the time.

Hit the same issue last month - turned out to be case sensitivity on Linux. The package was in package.json fine, but the actual folder name in node_modules didn’t match my import exactly. Check the spelling and caps in your node_modules folder against your require statement. Also got burned by NODE_PATH being set wrong in my environment variables, which messed up the normal module resolution. Run require.resolve('database-client') in a node REPL from your project directory to see where Node’s actually looking for it.

Check if you’re running the right file from the right directory. Your package.json says main: "server.js" but you’re running myapp.js. That mismatch means you might be in a different directory than you think. Also check if the package actually installed - look for node_modules/database-client and make sure it has the right files. I’ve seen this when package installs get interrupted or corrupted, even though they show up in package.json. Run npm ls database-client to see if it’s actually linked properly.

make sure you’re in the right folder. sometimes, if you have multiple node_modules directories, it can get confusing. also, try deleting node_modules and reinstalling everything. that helped me once!

this might sound weird, but check if you’re mixing package managers. i’ve seen this when you use npm but still have a yarn.lock file (or the other way around). the lockfiles conflict and screw up dependency resolution. delete yarn.lock if you’re using npm, or delete package-lock.json if you’re using yarn. then reinstall everything.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.