我无法导入已发布的打字稿库,出现以下错误:“找不到模块:无法解析 'tqt' in ...”
Posted
技术标签:
【中文标题】我无法导入已发布的打字稿库,出现以下错误:“找不到模块:无法解析 \'tqt\' in ...”【英文标题】:I can not import my published typescript library, with this error: "Module not found: Can't resolve 'tqt' in ..."我无法导入已发布的打字稿库,出现以下错误:“找不到模块:无法解析 'tqt' in ...” 【发布时间】:2020-02-10 00:45:53 【问题描述】:我使用这个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】:问题是 TypeScript outDir
与 package.json main
不匹配。对于您的特定设置,如果您将main
更改为dist/index.js
,它将起作用(这与您为types
所做的正确操作是平行的。)
【讨论】:
以上是关于我无法导入已发布的打字稿库,出现以下错误:“找不到模块:无法解析 'tqt' in ...”的主要内容,如果未能解决你的问题,请参考以下文章