导入:编译器未找到的模块,适用于 Visual Code,DefinitelyTyped
Import: module not found by compiler, works with Visual Code, DefinitelyTyped
我是 Typescript 的新手,希望我在这里遗漏了一些简单的东西。我已经为 javascript 库安装了类型定义,但编译器抛出了一个错误,但似乎无法弄清楚发生了什么。我的编辑器是 Visual Studio Code,我正在 Mac 上开发一个 React TypeScript 项目。这是一出戏。
我像这样为 'keen-tracking' 安装了类型定义,
npm install @types/keen-tracking@2.0.0
接下来,我向 'src/vendor.ts' 添加了几行,如下所示,
import KeenTracking from 'keen-tracking';
const keenTrackingCredentials = {
projectId: 'xxx',
writeKey: 'yyy'
};
export const keenClient = new KeenTracking(keenTrackingCredentials);
最后,我像这样在组件中导入 keenClient,
import {keenClient} from '../model/vendor';
const MyComponent :React.SFC<MemberProps> = props => {
return <div>
<Cover {...props} actionLabel="Label" action={function():void{ keenClient.recordEvent('pledge', {member: props.member.email} ) }} />
</div>
我 运行 我的项目使用 npm 启动,当我保存文件时,编译器抛出以下错误
Module not found: Can't resolve 'keen-tracking' in '~/projectDir/src/model'
Visual Studio 代码将 link 识别为 keen-tracking 类型定义,如下所示。我知道 typescript 版本是 2.0,当我在命令提示符下键入 tsc --version 时,我看到版本 3.2.2 是 运行ning,但我不确定这与 [=31= 是同一个版本] 代码正在使用。
// Type definitions for keen-tracking 2.0
// Project: https://github.com/keen/keen-tracking.js#readme
// Definitions by: Rui Ferreira <https://github.com/rui-ferreira>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
export default class KeenTracking {
constructor(options: { projectId: string; writeKey: string; requestType?: string });
recordEvent(
collectionName: string,
event: object
): Promise<{ created: boolean }>;
recordEvents(events: {
[collectionName: string]: object[];
}): Promise<{
[collectionName: string]: boolean[];
}>;
}
我很感激任何关于下一步可以解决这个问题的指示,我目前的想法是定义不正确或者我可能需要调试生成的 js 文件,尽管我还没有弄清楚如何现在就去做。谢谢!
您安装了类型但没有安装模块,因此请使用以下命令安装模块并重新启动应用程序
npm install keen-tracking --save
而且您安装类型的方式可能不起作用
没有版本也试试这个
npm install --save @types/keen-tracking
我是 Typescript 的新手,希望我在这里遗漏了一些简单的东西。我已经为 javascript 库安装了类型定义,但编译器抛出了一个错误,但似乎无法弄清楚发生了什么。我的编辑器是 Visual Studio Code,我正在 Mac 上开发一个 React TypeScript 项目。这是一出戏。
我像这样为 'keen-tracking' 安装了类型定义,
npm install @types/keen-tracking@2.0.0
接下来,我向 'src/vendor.ts' 添加了几行,如下所示,
import KeenTracking from 'keen-tracking';
const keenTrackingCredentials = {
projectId: 'xxx',
writeKey: 'yyy'
};
export const keenClient = new KeenTracking(keenTrackingCredentials);
最后,我像这样在组件中导入 keenClient,
import {keenClient} from '../model/vendor';
const MyComponent :React.SFC<MemberProps> = props => {
return <div>
<Cover {...props} actionLabel="Label" action={function():void{ keenClient.recordEvent('pledge', {member: props.member.email} ) }} />
</div>
我 运行 我的项目使用 npm 启动,当我保存文件时,编译器抛出以下错误
Module not found: Can't resolve 'keen-tracking' in '~/projectDir/src/model'
Visual Studio 代码将 link 识别为 keen-tracking 类型定义,如下所示。我知道 typescript 版本是 2.0,当我在命令提示符下键入 tsc --version 时,我看到版本 3.2.2 是 运行ning,但我不确定这与 [=31= 是同一个版本] 代码正在使用。
// Type definitions for keen-tracking 2.0
// Project: https://github.com/keen/keen-tracking.js#readme
// Definitions by: Rui Ferreira <https://github.com/rui-ferreira>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
export default class KeenTracking {
constructor(options: { projectId: string; writeKey: string; requestType?: string });
recordEvent(
collectionName: string,
event: object
): Promise<{ created: boolean }>;
recordEvents(events: {
[collectionName: string]: object[];
}): Promise<{
[collectionName: string]: boolean[];
}>;
}
我很感激任何关于下一步可以解决这个问题的指示,我目前的想法是定义不正确或者我可能需要调试生成的 js 文件,尽管我还没有弄清楚如何现在就去做。谢谢!
您安装了类型但没有安装模块,因此请使用以下命令安装模块并重新启动应用程序
npm install keen-tracking --save
而且您安装类型的方式可能不起作用
没有版本也试试这个
npm install --save @types/keen-tracking