如何修复Node.js中的‘Error: Cannot find module 'supertest'’错误
Hey there, let's work through this supertest module issue you're hitting. I've seen this exact problem a few times, so let's break down what's going on and how to fix it.
First, to recap your situation: you ran npm install to set up dependencies, tried running tests with npm test, but keep getting that "Cannot find module 'supertest'" error. Uninstalling/reinstalling supertest didn't help, and you noticed the supertest folder has no node_modules inside it.
Why this is happening
Quick note first: Supertest doesn't need its own local node_modules folder—its dependencies should be installed in your project's root node_modules directory. The real issue is almost certainly one of these:
- Supertest is listed in your
devDependenciesbut you rannpm installwith the--productionflag (which skips dev dependencies entirely) - Your
package-lock.jsonor rootnode_modulesfolder is corrupted - You're executing
npm testfrom a subdirectory instead of your project's root
Step-by-step fixes
Let's go through these solutions one by one:
Ensure supertest is installed as a dev dependency
Open yourpackage.jsonfile—chances are supertest is underdevDependencies(since it's only used for testing, not production code). If that's the case, run this to install it properly:npm install --save-dev supertestOr if you want to install all dev dependencies at once:
npm install --only=devAvoid using
npm install --productionunless you specifically don't need test tools.Reset your dependencies from scratch
Corruptedpackage-lock.jsonornode_modulesfolders are super common culprits here. Let's wipe and reinstall everything:# Delete the entire node_modules folder rm -rf node_modules # Delete the package-lock.json file rm package-lock.json # Clear npm's cache to remove any corrupted cached packages npm cache clean --force # Reinstall all dependencies fresh npm installThis will ensure every dependency (including supertest and its own required packages) is installed correctly in your root
node_modules.Confirm you're in the project root when running tests
Double-check that your terminal is pointing to the root directory of your Node.js project (the same folder where yourpackage.jsonlives) when you runnpm test. If you're in a subfolder, Node won't look for modules in the right place.Verify supertest is present in root node_modules
After reinstalling, check your project's rootnode_modulesfolder—you should see asupertestfolder there. Inside it, you'll find itspackage.json, but don't worry about missingnode_modulesinside that folder—npm hoists all dependencies to the root to avoid duplication, which is normal behavior.
A quick check of your test file
Looking at the code you shared that uses supertest:
var fs = require('fs') var request = require('supertest') var config = require('./config').getConfig() var url = config.url var caCert if (config.caCertFile) { caCert = fs.readFileSync(config.caCertFile) } var preparedRequest = function () { return caCert ? request.agent(url, { ca: caCert }) : request(url) } module.exports = preparedRequest
This code looks totally fine—there's no issue with how you're requiring or using supertest here. The problem is definitely with the dependency installation, not your code.
Give these steps a shot, and let me know if you still run into trouble!
内容的提问来源于stack exchange,提问作者catch_aag

