打字稿错误 找不到名字'require'。 C:/Users/me/project/src/pages/home/home.ts //需要Twilio模块并创建REST客户端
const client = require('twilio')(accountSid,authToken);
离子框架:3.3.0
离子应用程序脚本:1.3.7
Angular Core:4.1.2
Angular Compiler CLI:4.1.2
节点:6.10.3
OS平台:Windows 10
Navigator Platform:Win32
用户代理:Mozilla / 5.0(Windows NT 10.0; Win64;
home.ts中的CODE:
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));
我遵循了这些步骤
install require --save
和
npm install @ types / node --save-dev
我的问题是邮件没有发送。
如果我添加
declare var require: any
错误消失,但twilio永远不会收到发送消息的请求。 enter image description here
我找不到Ionic3或Angular和twilio发送短信的任何教程 我甚至没有传递参数,我复制了
我也试过这个
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:
{
"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
}
}
答案 0 :(得分:2)
如果@ types / node
@types
中有module: node
, tsconfig.json
效果最佳。
答案 1 :(得分:2)
添加:
"types": [
"node"
],
"typeRoots": [
"node_modules/@types"
]
tsconfig.json 文件中的