You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修复Node.js中的‘Error: Cannot find module 'supertest'’错误

Troubleshooting "Cannot find module 'supertest'" in Node.js Tests

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 devDependencies but you ran npm install with the --production flag (which skips dev dependencies entirely)
  • Your package-lock.json or root node_modules folder is corrupted
  • You're executing npm test from a subdirectory instead of your project's root

Step-by-step fixes

Let's go through these solutions one by one:

  1. Ensure supertest is installed as a dev dependency
    Open your package.json file—chances are supertest is under devDependencies (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 supertest
    

    Or if you want to install all dev dependencies at once:

    npm install --only=dev
    

    Avoid using npm install --production unless you specifically don't need test tools.

  2. Reset your dependencies from scratch
    Corrupted package-lock.json or node_modules folders 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 install
    

    This will ensure every dependency (including supertest and its own required packages) is installed correctly in your root node_modules.

  3. 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 your package.json lives) when you run npm test. If you're in a subfolder, Node won't look for modules in the right place.

  4. Verify supertest is present in root node_modules
    After reinstalling, check your project's root node_modules folder—you should see a supertest folder there. Inside it, you'll find its package.json, but don't worry about missing node_modules inside 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 04:51:23