“无法找到我自己的 TypeScript 模块的模块" [英] `Cannot find module` for my own TypeScript module

查看:87
本文介绍了“无法找到我自己的 TypeScript 模块的模块"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚向 NPM 注册表发布了一个新的基于 TypeScript 的模块,ooafs.但是,当我尝试安装它并将其导入另一个 TypeScript 项目时,VSCode 在导入语句中给了我以下错误:Cannot find module 'ooafs'.ts(2307).

此模块的源文件被编译为 JavaScript 到 dist/ 文件夹中,并且还会生成定义 (.d.ts).

这是已发布模块的树(我们在 npm install 时下载的那个):

<预><代码>.├──区│ ├── Entry.d.ts│ ├── EntryFilter.d.ts│ ├── EntryFilter.js│ ├── Entry.js│ ├── EntryType.d.ts│ ├── EntryType.js│ ├── FSTypings.d.ts│ ├── FSTypings.js│ ├── index.d.ts│ └── index.js├── 执照├── package.json└── README.md

package.json 确实包含以下条目:

<代码>{"main": "dist/index.js","types": "dist/index.d.ts",...}

因为该模块在 Runkit(纯 JS)上正常工作,所以我认为我遇到的唯一问题与 TypeScript 相关,并且当缺少声明文件是唯一的问题时,TypeScript 告诉我模块不存在并不是第一次.

我是否遗漏了编译过程中的一个步骤?我的 package.json 属性有误吗?

如果需要看更多代码,Github链接在问题开头,发布的模块结构可以在这里找到:https://unpkg.com/ooafs@0.1.2/dist/.

解决方案

实际上,问题并非来自我的模块 (ooafs).我正在使用该模块的项目的 tsconfig.json 有问题:module 属性显然必须设置为 commonjs.

很晚的另外,我强烈建议将 esModuleInterop 设置为 true,这样您就可以更自然地导入非 es6 模块.

I have just published a new TypeScript-based module to the NPM registry, ooafs. However, when I try to install it and import it in another TypeScript project, VSCode gives me the following error on the import statement: Cannot find module 'ooafs'.ts(2307).

This module's source files are compiled to JavaScript to a dist/ folder and definitions (.d.ts) are also generated.

Here's the tree of the published module (the one we download when we npm install):

.
├── dist
│   ├── Entry.d.ts
│   ├── EntryFilter.d.ts
│   ├── EntryFilter.js
│   ├── Entry.js
│   ├── EntryType.d.ts
│   ├── EntryType.js
│   ├── FSTypings.d.ts
│   ├── FSTypings.js
│   ├── index.d.ts
│   └── index.js
├── LICENSE
├── package.json
└── README.md

The package.json does contain the following entries:

{
    "main": "dist/index.js",
    "types": "dist/index.d.ts",
    ...
}

Because the module works normally on Runkit (pure JS), I assume the only problem I have is related to TypeScript, and it's not the first time TypeScript tells me a module doesn't exist when missing declaration files are the only problem.

Am I missing a step in the compilation process ? Are my package.json properties wrong ?

If you need to see more code, the Github link is at the beginning of the question, and the published module structure can be found here: https://unpkg.com/ooafs@0.1.2/dist/.

解决方案

Actually, the problem didn't come from my module (ooafs). It was a problem with the tsconfig.json of the project I was using the module in: The module property must be set to commonjs apparently.

Very late edit: Also, I highly recommend setting esModuleInterop to true which allows you to import non-es6 modules in a more natural manner.

这篇关于“无法找到我自己的 TypeScript 模块的模块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆