簡體   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