我阅读了 TypeScript 模块解析 的工作原理。
我有以下存储库: @ts-stack/di 。编译后的目录结构如下:
├── dist
│ ├── annotations.d.ts
│ ├── annotations.js
│ ├── index.d.ts
│ ├── index.js
│ ├── injector.d.ts
│ ├── injector.js
│ ├── profiler.d.ts
│ ├── profiler.js
│ ├── providers.d.ts
│ ├── providers.js
│ ├── util.d.ts
│ └── util.js
├── LICENSE
├── package.json
├── README.md
├── src
│ ├── annotations.ts
│ ├── index.ts
│ ├── injector.ts
│ ├── profiler.ts
│ ├── providers.ts
│ └── util.ts
└── tsconfig.json
在我的 package.json 中,我写了 "main": "dist/index.js"
。
在 Node.js 中一切正常,但 TypeScript:
import {Injector} from '@ts-stack/di';
找不到模块“@ts-stack/di”的声明文件。 ‘/path/to/node_modules/@ts-stack/di/dist/index.js’ 隐含地具有 ‘any’ 类型。
然而,如果我按如下方式导入,那么一切正常:
import {Injector} from '/path/to/node_modules/@ts-stack/di/dist/index.js';
我究竟做错了什么?
原文由 ktretyak 发布,翻译遵循 CC BY-SA 4.0 许可协议
对于您正在安装自己的 npm 包的情况
如果您使用的是第三方软件包,请参阅 下面的答案。
从
.js
"main": "dist/index.js"
中删除 ---package.json
。还要 根据 TypeScript 文档在
package.json
--- 中添加typings
:文件夹
dist
是 TS 编译器存储模块文件的地方。