0

我有这个tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "outDir": "dist"
    },
    "exclude": [
        "node_modules"
    ]
}

dashboard.component.ts

import { Component, OnInit } from '@angular/core';

import Logger = require('le_node');
var log = new Logger({
  token:'abcdef-1234-ghijklm-5678-nopqrstuvwxyz'
});

@Component({
    selector: 'dashboard',
    templateUrl: 'app/dashboard.component.html'
})

export class DashboardComponent implements OnInit {

    constructor() { }

    ngOnInit(): void {
        log.log('Test LogEntries...');
    }

}

并运行npm install le_node --save安装 LogEntries。当尝试使用 运行应用程序时npm start,我收到此错误:

> sample-app@2.0.0 start /Users/user/Workspace/sample-app
> tsc && concurrently "tsc -w" "lite-server" 

app/dashboard.component.ts(10,25): error TS2307: Cannot find module 'le_node'.
4

2 回答 2

0

一个对我有用的解决方案就是不使用对le_node浏览器不友好的模块。并使用logentries REST API

@Injectable()
export class LoggerService {
    constructor(private http: Http) {
    }

    public log(level: string, name: string) {
        // https://docs.logentries.com/docs/http-post
        const LOGENTRIES_URL = "https://webhook.logentries.com/noformat/logs/";
        let headers = new Headers();
        headers.append("Content-Type", 'application/json');
        let requestoptions = new RequestOptions({
            method: RequestMethod.Post,
            headers: headers,
            body: JSON.stringify({ "level": level, "message": message })
        });
        this.http.request(LOGENTRIES_URL + YOUR_LE_TOKEN, requestoptions).toPromise().catch((error) => {
            console.log("faield to send log to the logentries server");
        });
    }
}
于 2017-04-01T09:11:40.917 回答
0

错误 TS2307:找不到模块“le_node”。

您只安装了模块而不是其类型定义。如果模块不是用 TypeScript 编写的(例如 angular),则需要从外部获取其类型。这就是.d.ts文件的用途。

使固定

文件中的快速修复globals.d.ts

declare module "le_node";

更多的

此处介绍:https ://basarat.gitbooks.io/typescript/content/docs/types/migrating.html

于 2016-10-05T04:21:15.407 回答