打字稿错误找不到名称'require'.离子 3 [英] Typescript Error Cannot find name 'require'. IONIC 3

查看:24
本文介绍了打字稿错误找不到名称'require'.离子 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打字稿错误找不到名称要求".C:/Users/me/project/src/pages/home/home.ts//需要 Twilio 模块并创建一个 REST 客户端

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

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

离子框架:3.3.0

Ionic 应用脚本:1.3.7

Ionic App Scripts: 1.3.7

Angular 核心:4.1.2

Angular Core: 4.1.2

Angular 编译器 CLI:4.1.2

Angular Compiler CLI: 4.1.2

节点:6.10.3

操作系统平台:Windows 10

OS Platform: Windows 10

导航平台:Win32

用户代理:Mozilla/5.0(Windows NT 10.0;Win64;

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

ts 代码

home.ts 中的代码:

CODE inside home.ts:

顶部

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

//警报功能

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

我遵循了这些步骤

安装需要--save

npm install @types/node --save-dev

npm install @types/node --save-dev

我的问题是没有发送消息.

My issue is that the message does not get sent.

如果我添加

declare var require: any    

错误消失了,但 twilio 从未收到发送消息的请求.在此处输入图片描述

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

我找不到任何有关 Ionic3 或 Angular 和 twilio 发送短信的教程我什至没有传递参数,我复制了

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

我也试过了

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

我的 tsconfig.json:

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

推荐答案

添加:

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

tsconfig.json 文件中

这篇关于打字稿错误找不到名称'require'.离子 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆