Unable to import Twilio
Problem
Issue Summary Fresh install of `twilio` on my node project. Documentation was using `require` but I use `import` in my project. When attempting to use `import` I am given this error message: [code block] Steps to Reproduce 1. `npm i twilio -S` 2. Run code snippet Code Snippet [code block] Exception/Log [code block] Technical details: twilio-node version: `5.0.3` node version: `v16.20.2`
Error Output
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/developer/git/project/node_modules/twilio/node_modules/axios/index.js not supported.
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Fix Twilio Import Issue in Node.js Project
The error occurs because the Twilio library is an ES Module, and your project is attempting to use CommonJS syntax (require) to import it. Node.js version 16 supports ES Modules, but you need to ensure your project is configured to use them correctly.
Awaiting Verification
Be the first to verify this fix
- 1
Update package.json for ES Module support
Modify your project's package.json file to indicate that your project uses ES Modules by adding the 'type' field with the value 'module'. This allows you to use the import syntax.
json{ "type": "module" } - 2
Change Import Syntax
Update your import statement for Twilio in your code to use the correct ES Module syntax. Replace any require statements with import statements.
javascriptimport twilio from 'twilio'; - 3
Check Node.js Version
Ensure that you are running a compatible version of Node.js that supports ES Modules. You are currently using v16.20.2, which is compatible, but it's good to confirm.
bashnode -v - 4
Test the Import
Run your application to confirm that the Twilio import works without throwing an error. If the import is successful, you should be able to use Twilio functionalities.
bashnode your_script.js
Validation
Confirm that the application runs without the import error. You should be able to access Twilio functionalities without encountering the 'require() of ES Module' error.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep