3

Typescript Error Cannot find name 'require'. C:/Users/me/project/src/pages/home/home.ts // require the Twilio module and create a REST client

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

Node: 6.10.3

OS Platform: Windows 10

Navigator Platform: Win32

User Agent: Mozilla/5.0 (Windows NT 10.0; Win64;

ts code

CODE inside home.ts:

TOP

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',
})

//ALERT FUNCTION

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

I followed these steps

install require --save

and

npm install @types/node --save-dev

My issue is that the message does not get sent.

if i add

declare var require: any    

the error goes away, but twilio never gets the request to send the message. enter image description here

I cannot find any tutorials for Ionic3 or Angular and twilio to send an sms I have not even passed the parameters, I copied the

I also tried this

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

my 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
  }
}

2 Answers 2

3

Add:

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

in the tsconfig.json file

Sign up to request clarification or add additional context in comments.

1 Comment

Just an FYI for others, you need to put this under compilerOptions.
2

@types/node

@types work best if you have module: node in your tsconfig.json.

More

2 Comments

npm install @types/node --save-dev
I did this in my project here is my tsconfig.json

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.