如何解决此打字稿文件“不是模块”错误?

时间:2020-06-12 19:45:27

标签: typescript npm

我刚刚将我的第一个库软件包发布到NPM,并且正在尝试在应用程序项目中使用它。

它是用打字稿编写的,项目可以正常运行,并已发布到NPM。但是随后尝试使用它失败,因为它显然不是有效的模块。

设置模块导出(afaik)的index.ts文件中的代码为:

const initByClass = (widgetBindings: WidgetClassBinding[], h: any, render: any) => {
  for (const widgetBinding of widgetBindings) {
    setupWidget(widgetBinding, h, render);
  }
};

module.exports = {
  initByClass
};

该库中的tsconfig.json文件为:

{
  "compilerOptions": {
    "esModuleInterop": true,
    "target": "ES6",
    "module": "ES6",
    "moduleResolution": "node",
    "declaration": true,
    "outDir": "./lib",
    "strict": true
  },
  "include": ["src"],
  "exclude": ["node_modules", "**/__tests__/*"]
}

在我的应用程序代码中,我已将该包添加到package.json(并运行npm update),并尝试使用以下方式将其导入应用程序入口文件中:

import { initByClass } from "widgety";

但是它给我一个错误:

TS2306:文件'/var/app/app/app/node_modules/widgety/src/index.ts'不是 模块。

为了使代码可导入到另一个打字稿项目中,我需要更改什么?

如果使用了它们,则所有项目文件:https://github.com/Danack/widgety/ NPM软件包条目:https://www.npmjs.com/package/widgety

1 个答案:

答案 0 :(得分:1)

使用export关键字导出值时,文件被视为模块。

我相信用此行替换module.exports =应该可以修复错误:

export default initByClass;
相关问题