reactjs - Import : module not found by compiler, 适用于 Visual Code,DefinitelyTyped

标签 reactjs typescript npm visual-studio-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 start 运行我的项目,当我保存文件时,编译器抛出以下错误

Module not found: Can't resolve 'keen-tracking' in '~/projectDir/src/model'

Visual Studio Code 识别指向 keen-tracking 类型定义的链接,如下所示。我知道 typescript 版本是 2.0,当我在命令提示符下键入 tsc --version 时,我看到版本 3.2.2 正在运行,但我不确定这是否与 Visual Studio Code 使用的版本相同。

// 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

关于reactjs - Import : module not found by compiler, 适用于 Visual Code,DefinitelyTyped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53953866/

相关文章:

javascript - 这个 React 组件中发生了什么 ES6 魔法?

javascript - React 和 Webpack 模块解析失败 :/testimonials. js 意外 token (6:4)

javascript - 使用 React Router 和 Webpack 2 如何仅在某些路由上需要外部库?

json - 在不导入的情况下将 JSON 文件包含到构建/输出目录

npm - npm发布-从package.json中删除脚本?

React Native 上的 Firebase 客户端

typescript - 按顺序在 observables 中执行

typescript - TS 找不到模块

sql-server - 如何使用 mssql 模块通过 Node.JS 的 Windows 身份验证连接到 SQL Server

node.js - 避免在 CI 环境中每次都安装所有的 node_modules 依赖