Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 12, 2025

This PR adds comprehensive ESM (ECMAScript Modules) support to twilio-node while maintaining full backward compatibility with existing CommonJS usage. The implementation addresses the growing need for modern module system support in Node.js applications.

What's Changed

🚀 ESM Support Added

  • Dual Module System: Both CommonJS and ESM imports now work seamlessly
  • Zero Breaking Changes: All existing CommonJS code continues to work exactly as before
  • Full API Coverage: Complete Twilio SDK functionality available in both module systems

📦 Package Configuration

  • Added exports field to package.json for proper module resolution
  • Configured module field pointing to ESM entry point
  • Updated build process to generate both CJS and ESM distributions

🛠️ Implementation Details

  • Created ESM wrapper in lib-esm/ that imports from CJS build and re-exports in ESM format
  • Used safe wrapper approach to avoid invasive changes to existing codebase
  • Added build scripts for generating ESM-compatible modules
  • Enhanced TypeScript configuration to support both module systems

📚 Documentation & Examples

  • Updated README with ESM usage examples alongside existing CommonJS examples
  • Added comprehensive ESM example file (examples/example-esm.mjs)
  • Created verification script to ensure both module systems work correctly

✅ Testing & Verification

  • Added dedicated test suite for ESM functionality
  • All existing tests continue to pass (321/321 ✅)
  • Created verification script that tests both import methods

Usage Examples

CommonJS (unchanged):

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

ESM - Default Import:

import twilio from 'twilio';
const client = twilio(accountSid, authToken);

ESM - Named Imports:

import { Twilio, jwt, twiml, validateBody } from 'twilio';
const client = new Twilio(accountSid, authToken);

ESM - Mixed Usage:

import twilio, { RestException } from 'twilio';

try {
  const message = await twilio(accountSid, authToken).messages.create({
    body: 'Hello from ESM!',
    to: '+1234567890',
    from: '+0987654321'
  });
} catch (error) {
  if (error instanceof RestException) {
    console.log(`Twilio Error ${error.code}: ${error.message}`);
  }
}

Technical Implementation

The solution uses a wrapper approach that:

  1. Keeps the existing CommonJS build process unchanged (zero risk)
  2. Creates an ESM entry point that imports from the CJS build
  3. Re-exports all functionality using modern ESM syntax
  4. Uses package.json exports field to route imports correctly

This approach ensures maximum compatibility while providing the modern ESM interface that developers expect in 2025.

Fixes #1104.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits September 12, 2025 12:35
Co-authored-by: tiwarishubham635 <59199353+tiwarishubham635@users.noreply.github.com>
Co-authored-by: tiwarishubham635 <59199353+tiwarishubham635@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] [Feature Request]: Add ESM version Add ESM (ECMAScript Modules) support with dual CommonJS/ESM builds Sep 12, 2025
Copilot finished work on behalf of tiwarishubham635 September 12, 2025 12:46
@tiwarishubham635 tiwarishubham635 changed the title Add ESM (ECMAScript Modules) support with dual CommonJS/ESM builds feat: Add ESM (ECMAScript Modules) support with dual CommonJS/ESM builds Sep 15, 2025
@manisha1997 manisha1997 marked this pull request as ready for review September 17, 2025 08:25
Copy link

@tiwarishubham635 tiwarishubham635 merged commit eba84ef into main Sep 18, 2025
11 checks passed
@tiwarishubham635 tiwarishubham635 deleted the copilot/fix-1104 branch September 18, 2025 10:46
@LWJerri
Copy link

LWJerri commented Sep 18, 2025

Cool, use more AI, great job 🤡

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request]: Add ESM version

4 participants