node.js - 文件 types.d.ts 不是模块
问题描述
我正在创建一个 npm 包,并且我已经完成了源代码。在发布之前,我一直在本地对其进行测试,并且打字稿出现两个错误。File '<path>/McStatus.d.ts' is not a module.
和Cannot find name 'McServer'
。
我的包裹有一个src
文件夹,其中包含index.ts
和types.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)
解决方案
推荐阅读
- ssas - Adventureworks 以外的 OLAP 数据集?
- python - 颤振 PUT 请求
- angular - Angular:从 .subscribe() 内部返回的字符串会导致无限的 xhr 请求
- java - Selenium 文件上传错误-> org.openqa.selenium.ElementNotInteractableException:元素不可交互
- css - 定位整个文档中的奇数元素,而不仅仅是兄弟姐妹
- php - 我希望我的日期格式为 09:54:24
- javascript - 错误:在我的 NodeJS 应用程序中找不到模块“../../models/blog”
- c++ - 计算彼此之间数字的平均值 C++
- python - 使用 telnetlib 在 telnet 期间从 tn.write() 获取输出
- react-native - 无法从 api 的响应中设置状态