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

发布TypeScript SDK模块后遇模块找不到错误,求排查

问题

首次基于第三方工具(tomchen/example-typescript-package模板、odata2ts)构建并发布了API的TypeScript SDK包,Node版本18.18。安装后无法正常使用:

  • 从@namespace/module导入类时,报错:Cannot find module '@namespace/module' or its corresponding type declarations.
  • 改为@namespace/module/dist/types/src/index导入时,类型错误消失,但编译时出现:Module not found: Error: Can't resolve '@namespace/module/dist/types/src/index'

相关配置及目录结构如下:

package.json

{
  "name": "@namespace/module",
  "version": "1.0.0",
  "description": "",
  "main": "dist/cjs/index.js",
  "module": "dist/esm/index.js",
  "umd:main": "dist/umd/index.js",
  "types": "dist/types/index.d.js",
  "scripts": {
    "postinstall": "husky install",
    "prepublishOnly": "pinst --disable",
    "postpublish": "pinst --enable",
    "gen-odata": "odata2ts",
    "build": "npm run gen-odata && npm run build:cjs && npm run build:esm && npm run build:umd && npm run build:types",
    "build:cjs": "node tools/cleanup cjs && tsc -p config/tsconfig.cjs.json",
    "build:esm": "node tools/cleanup esm && tsc -p config/tsconfig.esm.json",
    "build:umd": "node tools/cleanup umd && webpack --config config/webpack.config.js",
    "build:types": "node tools/cleanup types && tsc -p config/tsconfig.types.json",
    "clean": "node tools/cleanup",
    "package": "npm run build && npm pack",
    "test": "jest --no-cache --runInBand",
    "test:cov": "jest --coverage --no-cache --runInBand",
    "addscope": "node tools/packagejson name @namespace/module"
  },
  "keywords": [],
  "publishConfig": {
    "access": "public"
  },
  "files": [
    "dist"
  ],
  "author": "",
  "license": "MIT",
  "homepage": "",
  "repository": {
    "type": "git",
    "url": "git@github.com:namespace/module.git"
  },
  "bugs": {
    "url": ""
  },
  "devDependencies": {
    "@commitlint/cli": "^13.1.0",
    "@commitlint/config-conventional": "^13.1.0",
    "@odata2ts/odata2ts": "^0.31.1",
    "@types/jest": "^29.5.5",
    "@typescript-eslint/eslint-plugin": "^4.31.1",
    "@typescript-eslint/parser": "^4.31.1",
    "eslint": "^7.32.0",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "husky": "^7.0.2",
    "jest": "^29.7.0",
    "pinst": "^2.1.6",
    "prettier": "^2.4.0",
    "ts-jest": "^29.1.1",
    "ts-loader": "^9.2.5",
    "typescript": "^4.4.3",
    "webpack": "^5.89.0",
    "webpack-cli": "^4.8.0"
  },
  "dependencies": {
    "@odata2ts/http-client-fetch": "^0.6.2",
    "@odata2ts/odata-service": "^0.17.1"
  }
}

dist目录结构

dist
  cjs
    odata2ts.config.js
    src
      index.js
      build
        namespace
          QNamespace.js
          NamespaceModel.js
          NamespaceService.js
  esm
    odata2ts.config.js
    src
      index.js
      build
        namespace
          QNamespace.js
          NamespaceModel.js
          NamespaceService.js
  types
    odata2ts.config.d.ts
    src
      index.d.ts
      build
        namespace
          QNamespace.d.ts
          NamespaceModel.d.ts
          NamespaceService.d.ts
  umd
    index.js
解决方案

问题根源是package.json中的入口路径与实际dist目录结构不匹配,具体修正步骤如下:

  1. 修正package.json的入口字段
    将main、module、types字段的路径补充src子目录,因为实际编译后的入口文件都在dist/*/src下:

    {
      "main": "dist/cjs/src/index.js",
      "module": "dist/esm/src/index.js",
      "types": "dist/types/src/index.d.ts"
    }
    
    • 原types字段指向dist/types/index.d.js,但实际类型文件是dist/types/src/index.d.ts(注意是.d.ts而非.d.js)
    • 原main/module字段未包含src,导致Node无法找到对应的JS文件
  2. 验证TypeScript编译配置(可选)
    检查config/tsconfig.cjs.json、config/tsconfig.esm.json、config/tsconfig.types.json中的outDir和rootDir配置:

    • 若希望编译后直接将文件输出到dist/cjs而非dist/cjs/src,可将rootDir设为项目根目录的src文件夹,避免生成额外的src子目录
    • 确保declaration: true仅在tsconfig.types.json或需要生成类型的配置中开启,避免冗余类型文件
  3. 重新构建并发布
    运行npm run build确认dist目录结构与修正后的路径匹配,然后重新发布包:

    npm run build
    npm publish
    

完成以上步骤后,即可正常从@namespace/module导入类,类型提示和运行时都能正常工作。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 12:23:11