我无法导入我发布的打字稿库,出现以下错误:“找不到模块:无法在...中解析'tqt'”

时间:2019-10-12 22:21:05

标签: reactjs typescript npm npm-install npm-publish

我使用此tutorial发布了一个简陋的简单打字稿模块,并通过

安装
npm install --save tqt@1.0.0

当尝试在程序中使用它时,我得到了:

Module not found: Can't resolve 'tqt' in '/home/akiva/Programming/frogcast/src'

从这里:

import React from "react";
import { CA_PoliticalParty } from "tqt";

console.log(CA_PoliticalParty); // Anything that invokes this will error.
class App extends React.Component
{
    constructor() {
        super("");
    }
    render() { return ( <div> </div> )};
};

export default App;

发布的相关代码如下:

TQt.ts

// Canadian Enumerators
export enum CA_PoliticalParty {
    // Canadian Major National Political Parties
    None= "None",
    CPC = "CPC",
    GPC = "GPC",
    LPC = "LPC",
    NDP = "NDP",
    PPC = "PPC",
}

index.ts

export {CA_PoliticalParty} from './TQt'

package.json

{
  "name": "tqt",
  "version": "1.0.0",
  "description": "Typescript Namespaces and Enums",
  "main": "index.js",
  "types": "dist/index.d.ts",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Akiva Avraham <akiva@linux.com>",
  "license": "GPL-2.0",
  "repository": {
    "type": "git",
    "url": "git+ssh://akiva@git.launchpad.net/tqt"
  },
  "devDependencies": {
    "typescript": "^3.6.4"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es6",
    "declaration": true,
    "outDir": "./dist", 
    "lib": ["es5","es6","dom"]
  },
  "include": [
    "src/**/*"
  ]
}

这里有什么明显的我想念的吗?我只以很小的方式来进行设置,没有任何变化,并且完全按照本教程的内容进行了学习。

1 个答案:

答案 0 :(得分:1)

问题是TypeScript outDir与package.json main不匹配。对于您的特定设置,如果将main更改为dist/index.js(这与您对types正确执行的操作相同),它将起作用。

enter image description here