首页 > 解决方案 > 文件 types.d.ts 不是模块

问题描述

我正在创建一个 npm 包,并且我已经完成了源代码。在发布之前,我一直在本地对其进行测试,并且打字稿出现两个错误。File '<path>/McStatus.d.ts' is not a module.Cannot find name 'McServer'

我的包裹有一个src文件夹,其中包含index.tstypes.d.ts。在我拥有的包的根目录中McStatus.d.ts

代码片段:

包.json:

"main": "dist/index.js",
"types": "McStatus.d.ts",

tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "sourceMap": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "esModuleInterop": true
  }
}

src/index.ts

export function checkStatus(server: McServer): Promise<Status> {
  ...
}

McStatus.d.ts

declare namespace McStatus {
  function checkStatus(server: McServer): Promise<Status>

  interface Status {
    ping?: number
    version?: string
    motd?: string
    players?: number
    max_players?: number
  }

  interface McServer {
    host: string
    port: number
  }
}

测试包:

我创建了一个测试目录,我已经尝试过npm install <path to package>,,npm link甚至npm pack安装.tgz文件。

我的测试文件是错误的来源

import checkStatus from 'mcstatus'
// File '/home/stphn/git/mcstatus/McStatus.d.ts' is not a module.

const options: McServer = {
// Cannot find name 'McServer'
  host: "mc.stephenduvall.me",
  port: 25565,
}

checkStatus(options).then(console.log).catch(console.log)

标签: node.jstypescriptnpm

解决方案


推荐阅读