使用Twilio发送SMS时出现TypeError: self._qs.unescape不是函数问题求助
Hey there, I’ve dealt with this exact error before when working with older Twilio SDK versions—let’s get this sorted out step by step.
What’s causing the issue?
Your setup has two key problems:
- Outdated Twilio SDK: Version
2.11.1is extremely old (released around 2016) and relies on a deprecated method (unescape) from theqsquerystring library that’s no longer available in modernqsversions. If your project has other dependencies pulling in a newerqs, this creates a conflict. - Mismatched Type Definitions: The
@types/twilio@0.0.9package is also way out of date and doesn’t align with either the old SDK or modern TypeScript standards.
Step-by-Step Fix
Uninstall outdated packages
First, remove the old Twilio SDK and its outdated type definitions:npm uninstall twilio @types/twilioInstall the latest compatible Twilio SDK
The modern Twilio SDK includes built-in TypeScript types, so you don’t need a separate@types/twiliopackage anymore. Install the latest version:npm install twilioUpgrade TypeScript (optional but recommended)
Your TypeScript version (2.8.3) is quite old and might not play well with the new SDK’s type definitions. Upgrade to a recent stable version:npm install typescript@latest --save-devYou can also upgrade
@types/nodeto match your current Node.js version (e.g.,@types/node@18if you’re on Node.js 18) for better type support.Fix your client initialization
Looking at your code, you forgot to initialize the Twilio client with your account SID and auth token! Here’s the corrected code (works for both CommonJS and TypeScript):// For CommonJS const twilio = require('twilio'); // For ES Modules/TypeScript // import twilio from 'twilio'; const ACCOUNT_SID = "your_account_sid"; const AUTH_TOKEN = "your_auth_token"; // Initialize the client first const client = twilio(ACCOUNT_SID, AUTH_TOKEN); client.messages.create({ body: 'Welcome', to: '+your_recipient_phone_number', from: '+your_valid_twilio_number' }) .then((message) => console.log(`Message sent! SID: ${message.sid}`)) .catch((error) => console.error('Failed to send message:', error));
Why this works
- The new Twilio SDK uses updated dependencies that don’t rely on the deprecated
qs.unescapemethod, eliminating the type error. - Built-in TypeScript types mean no more mismatched type definitions from old
@typespackages. - Proper client initialization ensures you’re using the SDK correctly (a common oversight that can cause weird errors too!).
内容的提问来源于stack exchange,提问作者Majid Ahmadi

