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

TypeScript模块报错:无法找到'./api/api.js'或其类型声明

问题:TypeScript 找不到模块 './api/api.js' 且 JS 文件代码未执行

报错信息

Cannot find module './api/api.js' or its corresponding type declarations.ts(2307)

项目结构说明

/src 与 /api 是两个独立的同级文件夹

相关配置与代码

tsconfig.json

{
  "compilerOptions": {
    "target": "es2022",
    "module": "commonjs",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true,
    "outDir": "./dist",
    "rootDir": "./src"
  }
}

package.json

{
  "name": "xmas",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon src/main.ts",
    "build": "tsc -p .",
    "start": "node dist/index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/bcryptjs": "^2.4.2",
    "@types/pouchdb": "^6.4.0",
    "@types/uuid": "^9.0.0",
    "bcryptjs": "^2.4.3",
    "pouchdb": "^8.0.0",
    "uuid": "^9.0.0"
  }
}

src/main.ts

import * as API from './api/api.js';
var newacc = API.default.newacc;

newacc("imnotgay", "moveimggay");

api/api.js

import newacc from './newacc.js';
export default {
    newacc
}

api/newacc.js

import bcrypt from 'bcryptjs';
import db from 'pouchdb';
var accounts = new db('accounts')
import { v4 as uuidv4 } from 'uuid';
export default (uname, pword) => {
        var uname = uname.toLowerCase()
    return new Promise((resolve, reject) => {

        if(pword.length < 3) {
            resolve( {success: false, errormsg: "Password has to be atleast 2 characters"})
        } else {
        if(/^[\w]+([-_\s]{1}[a-z0-9]+)*$/i.test(uname)) {
            if(uname.length > 3 && uname.length <17) {
                bcrypt.hash(pword, 10).then((hash) => {
        
                    accounts.get(uname)
                    .then((doc) => {
                        //account exists
                        resolve( {success: false, errormsg: "This account name is already taken"})
                    })
                    .catch((err) => {
                        if(err.status == 404) {
                       var authorization = uuidv4()
                            accounts.put({
                                _id: uname,
                                pword: hash,
                                authorization: authorization,
                                inv: {},
                                bal: 0
                            }).then(() => {
                            resolve({success: true, authorization: authorization, name: uname})
                            })
                            .catch(() => {
                                resolve({success: false, errormsg: "db error"})
                            })
                            
                        } else {
                            console.log(err)
                            resolve( {success: false, errormsg: "db error"})
                        }
                        
                        
                    })

        
                });
            } else {
                resolve( {success: false, errormsg: "Name has to be bigger than 3 characters, and less than 17"})
            }
        } else {
            resolve( {success: false, errormsg: "Name must only contain Alphanumeric and Space..."})
        }
    }
    })

}
console.log("GOT HERE"); // 未执行

额外问题

  • 无法定位模块找不到的原因
  • api/newacc.js 中的 console.log("GOT HERE") 语句未执行

解决方案

1. 模块找不到问题修复

路径错误纠正

src/main.ts 里的 ./api/api.js 是相对路径,由于 /src 和 /api 是同级文件夹,正确路径应为 ../api/api.js——原路径会去 src/api 下查找文件,自然找不到。

修改后的 src/main.ts:

import * as API from '../api/api.js';
const newacc = API.default.newacc;

newacc("imnotgay", "moveimggay");

TypeScript 类型声明问题

在TS文件中导入JS模块时,TS会提示找不到类型声明,可选两种解决方式:

  • 方式一:为JS模块添加类型声明文件
    在 /api 文件夹下创建 api.d.ts:
declare module '../api/api.js' {
  export interface API {
    newacc: (uname: string, pword: string) => Promise<{
      success: boolean;
      errormsg?: string;
      authorization?: string;
      name?: string;
    }>;
  }
  const defaultExport: API;
  export default defaultExport;
}
  • 方式二:关闭TS对JS模块的类型检查(快速临时解决)
    在 tsconfig.json 的 compilerOptions 中添加:
"allowJs": true,
"noImplicitAny": false

2. console.log 未执行问题修复

ES模块的顶层代码只会在模块首次被成功导入时执行,之前的路径错误导致模块根本没被加载,修复路径问题后,该语句即可正常执行。若仍未执行,可检查:

  • 确认 api/newacc.js 内的导入路径正确(./newacc.js 无误,与 api.js 同级)
  • 重启nodemon,避免旧代码缓存影响

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 06:35:29