繁体   English   中英

错误 TS2307:找不到模块(外部、私有模块)

[英]error TS2307: Cannot find module (external, private module)

我读过类似的问题,包括tsc throws `TS2307: Cannot find module` for a local file

我有一个 privat 外部模块(在本地 git 服务器上),我尝试将它包含到我的应用程序中 - 这有效。

  • phpStorm 可以完美解析类!
  • 该应用程序抛出错误 TS2307 但运行完美

我想知道为什么转译器会抛出这个错误,尽管一切看起来都很好,我需要这个错误消失才能稳定部署。

模块 tsconfig.json 看起来像:

{
    "compilerOptions": {
        "module": "commonjs",
        "moduleResolution": "node",
        "target": "ES6",
        "typeRoots": [
            "node_modules/@types"
        ],
        "sourceMap": false,
        "noImplicitAny": false,
        "declaration": true,
        "noResolve": false
    },
    "version": "2.0.0"
}

我尝试了上述几种组合但没有成功。

我在模块中有一个Main.ts

export {default as Logger}  from "./Logger";
export {ILogger as ILogger} from './ILogger';

类和接口在哪里

export interface ILogger { }
export default class Logger implements ILogger { }

在我使用的应用程序中:

import {Logger, ILogger} from "logging";

当我尝试转译应用程序而不是模块时会发生错误。 同样,通过节点运行应用程序和在 phpStorm 中编码时一切正常,但在转译时我得到:

src/Main.ts(9,39): error TS2307: Cannot find module 'logging'.

更新:

看起来像tsc的错误: https : //github.com/Microsoft/TypeScript/issues/14332

我们只需要设置

"noResolve": false

true ,它终于奏效了。

在我们的项目中,错误是通过在 include 键中包含打字稿文件和定义来解决的,如下所示

  "include": ["global.d.ts", "./app/**/*.tsx", "./app/**/*.ts"],

app 包含打字稿文件, global.d.ts 包含本地包的声明如下

declare module 'uicomponents-react-native';
declare module '@styles';
declare module '@styles/*';

declare module '@common';

declare module '@constants';
declare module '@constants/*';

declare module '@utils/*';
declare module '@utils';

declare module '@helper/*';
declare module '@helper';

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM