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

启动Express服务器时Node.js持续抛出模块找不到错误的求助

Fixing "Cannot find module" Errors in Your Express/TypeScript Server

Let's break down what's causing these errors and how to fix them quickly:

The Core Issue: Module Resolution Rules

Node.js and TypeScript handle module imports differently based on the path format:

  • Relative paths (starting with ./ or ../) tell Node to look for your local files/folders starting from the current file's directory.
  • Bare module names (like routes or database) make Node search for third-party packages in your node_modules folder, not your project's source code. That's why your first error popped up—Node was trying to find a npm package named routes instead of your local routes folder.

Quick Fix: Use Correct Relative Paths

Update all your local module imports to use relative paths that match your project structure:

  1. In src/index.ts:
    Change import routes from "routes"; to:

    import routes from "./routes";
    

    (This works if your routes folder is in the same src directory as index.ts.)

  2. In src/models/User.ts:
    Your error shows it's trying to import database—adjust this path based on where your database files live. For example:

    • If database is a folder in src/ (same level as models), use:
      import database from "../database";
      
    • If database is a file inside src/models/, use:
      import database from "./database";
      

Optional: Set Up Non-Relative Imports (For Large Projects)

If you want to avoid messy relative paths like ../../database, you can configure TypeScript to resolve bare module names to your local source files:

  1. Update tsconfig.json:
    Add these settings to the compilerOptions section:

    {
      "compilerOptions": {
        "baseUrl": "./src", // Set your src folder as the root for module resolution
        "paths": {
          "*": ["*"] // Map all bare module names to files in src
        },
        // Keep your existing compiler options here
      }
    }
    
  2. Install tsconfig-paths:
    This package lets Node.js recognize TypeScript's path configuration:

    npm install tsconfig-paths --save-dev
    
  3. Adjust your start command:
    Modify your package.json scripts to use the path register:

    {
      "scripts": {
        "start": "ts-node -r tsconfig-paths/register src/index.ts"
      }
    }
    

    Now you can use import routes from "routes" and import database from "database" without relative paths.


Final Checks

  • Double-check that the files/folders you're importing actually exist (e.g., make sure routes/index.ts exists—TypeScript automatically imports the index.ts file from a folder).
  • Ensure filename casing matches exactly. Windows ignores case, but Node.js might throw errors if you import Routes.ts instead of routes.ts.

内容的提问来源于stack exchange,提问作者Ahmad ELZohairy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 21:57:36