node.js - Typescript-fel Kan inte hitta namnet 'require'. IONIC 3


Typescript-fel Kan inte hitta namnet 'require'. C:/Users/me/project/src/pages/home/home.ts /kräver Twilio-modulen och skapar en REST-klient

const client=require('twilio')(accountSid, authToken);

Ionic Framework: 3.3.0

Ionic App Scripts: 1.3.7

Angular Core: 4.1.2

Angular Compiler CLI: 4.1.2

Nod: 6.10.3

OS-plattform: Windows 10

Navigatorplattform: Win32

Användaragenter: Mozilla/5.0 (Windows NT 10.0; Win64;

ts-kod

KOD inuti home.ts:

TOPP

import { Component } from '@angular/core';
import { FirebaseProvider } from './../../providers/firebase/firebase';
import { FirebaseListObservable } from 'angularfire2/database';
import { NavController, NavParams } from 'ionic-angular';
import { BarcodeScanner ,BarcodeScannerOptions } from '@ionic-native/barcode-scanner';
import { Request } from '@angular/http';


@Component({
selector: 'home',
templateUrl: 'home.html',
})

//ALERTFUNKTION

scanAlert(){
    this.options = {
      showFlipCameraButton : true,
      formats : "QR_CODE,PDF_417", 
      prompt : "SCAN QR CODE "
    }
    this.barcodeScanner.scan(this.options).then((barcodeData) => {
        console.log(barcodeData);
        this.pinText = barcodeData.text;
        const uid = this.pinText;
        this.firebaseProvider.afd.database.ref('pins')
        .orderByChild('QRCODE')
        .equalTo(uid)
        .limitToFirst(1)
        .once('value', snap => {
            let key = Object.keys(snap.val())[0]; // KEY VALUE 
            let user = snap.child(key).val(); // the whole user object
            this.message = 'Parameters to send to twilio' + user.phone + 
                  'regarding'+user.message;

            const accountSid = 'myacctid';
            const authToken = 'mytoken';

            // require the Twilio module and create a REST client
            const client = require('twilio')(accountSid, authToken);

            client.messages
            .create({
                to: '+142mynumber  ',
                from: '+mysms trial#',
                body: 'TEST Alert Message !',
  })
  .then((message) => console.log(message.sid));

Jag följde dessa steg

installera require --save

och

npm install @types/node --save-dev

Mitt problem är att meddelandet inte skickas.

om jag lägger till

declare var require: any    

felet försvinner, men Twilio får aldrig begäran om att skicka meddelandet. lägg in bildbeskrivning här

Jag kan inte hitta några handledningar för Ionic3 eller Angular och Twilio för att skicka ett sms. Jag har inte ens skickat parametrarna, jag kopierade

Jag försökte det här också

    var twilio = require('twilio');//Make sure to npm install @types/node
    var accountSid = 'myacctid'; // Your Account SID from www.twilio.com/console
    var authToken = 'myauthtoken';   // Your Auth Token from www.twilio.com/console


    var client = new twilio(accountSid, authToken);

    client.messages.create({
        body: 'ALERT! Someone has scanned the Alert Code for ',
        to: '+myphone'  ,// Text this number
        from: '+myTwilioTrial#' // From a valid Twilio number
    })
    .then((message) => console.log(message.sid));

min tsconfig.json:

{
  "compilerOptions": {
    "allowSyntheticDefaultImports": true,
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": [
      "dom",
      "es2015"
    ],
    "module": "es2015",
    "moduleResolution": "node",
    "sourceMap": true,
    "target": "es5"
  },
  "include": [
    "src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false,
  "atom": {
    "rewriteTsconfig": false
  }
}

Svar #1

Lägg till :

"types": [
    "node"
],
"typeRoots": [
    "node_modules/@types"
]

i tsconfig.json-filen

Svar #2

@types/node

@types fungerar bäst om du har module: node i din tsconfig.json.

Mer

  • Potentiell installation https://basarat.gitbooks.io/typescript/content/docs/quick/browser.html

Relaterade artiklar