2016-10-04 79 views
0

如何使用LogEntries我有這個tsconfig.json對角2個應用程序(打字稿)

{ 
    "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運行應用程序,我收到此錯誤:

> [email protected] 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'. 

回答

0

error TS2307: Cannot find module 'le_node'.

你只安裝了模塊不是它的類型定義。如果模塊不是用TypeScript寫的(例如角度),你需要從外部獲取它的類型。這就是.d.ts文件的用途。

修復

的QuickFix在文件globals.d.ts

declare module "le_node"; 

更多

這裏涵蓋:https://basarat.gitbooks.io/typescript/content/docs/types/migrating.html

+0

嗨@basarat,感謝您的幫助,但它不是爲我工作。我在_https://angular.io/docs/ts/latest/quickstart.html_中提到了啓動項目,並且在'npm install'時,它使用'global'構建了'typings'路徑(對於core-js,jasmine和節點類型定義)和'index.d.ts'。我試圖在'index.d.ts'和'globals.d.ts'上聲明** le_node **,但仍然得到'GET http:// localhost:3000/le_node 404(Not Found)'。如果你有其他的選擇,我也贊同。 – leomperes

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"); 
     }); 
    } 
}