You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Twilio发送SMS时出现TypeError: self._qs.unescape不是函数问题求助

Fix for TypeError: self._qs.unescape is not a function with Twilio SMS

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.1 is extremely old (released around 2016) and relies on a deprecated method (unescape) from the qs querystring library that’s no longer available in modern qs versions. If your project has other dependencies pulling in a newer qs, this creates a conflict.
  • Mismatched Type Definitions: The @types/twilio@0.0.9 package is also way out of date and doesn’t align with either the old SDK or modern TypeScript standards.

Step-by-Step Fix

  1. Uninstall outdated packages
    First, remove the old Twilio SDK and its outdated type definitions:

    npm uninstall twilio @types/twilio
    
  2. Install the latest compatible Twilio SDK
    The modern Twilio SDK includes built-in TypeScript types, so you don’t need a separate @types/twilio package anymore. Install the latest version:

    npm install twilio
    
  3. Upgrade 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-dev
    

    You can also upgrade @types/node to match your current Node.js version (e.g., @types/node@18 if you’re on Node.js 18) for better type support.

  4. 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.unescape method, eliminating the type error.
  • Built-in TypeScript types mean no more mismatched type definitions from old @types packages.
  • Proper client initialization ensures you’re using the SDK correctly (a common oversight that can cause weird errors too!).

内容的提问来源于stack exchange,提问作者Majid Ahmadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:20:28