Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 解决意外加载TypeScript声明文件.d.ts报错问题 #89

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Beiluola
Copy link

您好,我使用egg-swagger-doc,版本2.3.2
问题是这样的。
我使用eggjstypescript模板开发项目,安装了egg-swagger-doc插件,
npm run start报错,错误原因部分提示如下:

2020-07-23 23:07:51,521 ERROR 11668 [-/127.0.0.1/-/1ms GET /] nodejs.MODULE_NOT_FOUNDError: Cannot find module 'E:\personal\glacier\test\fixtures\apps\glacier-demo-ts\app\contract\format.d'
Require stack:
- E:\personal\glacier\node_modules\egg-swagger-doc\lib\contract\index.js
- E:\personal\glacier\node_modules\egg-swagger-doc\app\extend\context.js

然后我找到了异常代码:

// egg-swagger-doc\lib\contract\index.js
// 第50、51行
if (stat.isFile() && [ '.js', '.ts' ].indexOf(path.extname(filepath)) !== -1) {
  const def = require(filepath.split(/\.(js|ts)/)[0]);

异常的原因是,我的tsconfig.json文件声明了"declaration":true,在使用tsc命令编译app/contract目录下的ts文件时会生成.js文件和.d.ts声明文件。

您的代码中会把匹配.ts后缀的.d.ts文件作为模块使用require('.d')加载,而这个文件并不存在,所以报错。

解决方法是跳过.d.ts类型的声明文件。
这是我的修复代码。
请尽快修复,祝好。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant