使用节点模块导出第三方输入

时间:2019-11-18 20:16:05

标签: node.js typescript npm

我有一个名为module_core的模块,该模块具有以下目录结构:

/src
   /company
      /webservices
         /service-one
         ...
         /service-two
         ...
         /service-new // my service
           index.ts
           constants.ts
           ServiceClient.ts
/Typings

/ServiceClient.ts

export class ServiceClient {
   public constructor() {}...
   public async methodOne() {}...
}
/Typings/@third/party-lib/index.d.ts

...
export class Client {
    constructor(config: object);
    public getUser(userId: string): Promise<User>;
    ...
}
...

我已将第三方输入内容放入/Typings中。我想导出ServiceClient.ts和相应的类型以供其他服务使用,例如

index.ts

export * from "../service-new/ServiceClient";
export * from "../../../../Typings/@third/party-lib/index";

但是,这不起作用,或者至少在其他服务中使用该模块时出现错误。有关如何使用此文件夹结构正确执行此操作的任何建议?

我确实对tsconfig.json进行了以下更改,以确保Typings文件夹是构建过程的一部分:

    "paths": {
      "*": ["Typings/*", "node_modules/*"]
    },

正在使用该模块的服务会吐出以下错误:

error TS2307: Cannot find module '@third/party-lib'.

1 import { User } from "@third/party-lib";
          ~~~~~~~~~~~~~~~~~

我认为此错误是由于找不到类型所致。

2 个答案:

答案 0 :(得分:0)

您是否尝试过像这样删除“ ../”之一-

index.ts

export * from "../service-new/ServiceClient";
export * from "../../../Typings/@third/party-lib/index";

答案 1 :(得分:0)

事实证明,问题是我的index.ts试图导出/Typings文件夹,但不应该这样做。我已经将它们复制到使用该模块的服务中,因此无需像以前一样导出它们:

index.ts

export * from "../service-new/ServiceClient";
export * from "../../../../Typings/@third/party-lib/index";

一旦我将其更改为以下内容,它就会正常工作而不会出现错误:

index.ts

export * from "../service-new/ServiceClient";