Last modified: Jun 24, 2026
Fix Cannot Find Module Twilio Error
Seeing the error "Cannot find module 'twilio'" in Node.js can stop your project cold. This common issue happens when Node.js cannot locate the Twilio package in your project. Do not worry. This guide will show you simple steps to fix it.
We will cover installation problems, missing dependencies, and path issues. By the end, you will run your Twilio code without errors. Let's dive in.
What Causes This Error?
The Cannot find module 'twilio' error occurs for a few reasons. The most common cause is that the Twilio module is not installed. You might have forgotten to run npm install twilio. Another reason could be that you are in the wrong directory. Node.js looks for modules inside the node_modules folder. If you run your script from a different folder, the module may not be found.
Sometimes, the error appears after updating Node.js or npm. This can break module paths. A corrupted node_modules folder can also cause this problem. Let's look at each fix.
Step 1: Install the Twilio Module
The first step is to install the Twilio package. Open your terminal and navigate to your project root. Then run the following command:
npm install twilio
This command downloads the Twilio SDK and adds it to your node_modules folder. It also updates the package.json file. After installation, try running your script again. If the error persists, move to the next step.
Step 2: Check Your Working Directory
Make sure you are running your script from the correct folder. Node.js looks for modules starting from the current directory. If your script is inside a subfolder, the module might not be found. Use the cd command to move to your project root:
cd /path/to/your/project
node your-script.js
If the error continues, check if the node_modules folder exists. You can list files with ls (Linux/Mac) or dir (Windows). If the folder is missing, you need to install dependencies again.
Step 3: Delete and Reinstall node_modules
Sometimes the node_modules folder gets corrupted. Deleting it and reinstalling can fix the issue. Run these commands:
# Delete node_modules and package-lock.json
rm -rf node_modules package-lock.json
# Reinstall all dependencies
npm install
On Windows, use rmdir /s node_modules instead of rm -rf. This process ensures a fresh installation of all modules, including Twilio.
Step 4: Verify package.json
Check your package.json file. It should list "twilio" under "dependencies". Open the file and look for this entry:
{
"dependencies": {
"twilio": "^4.0.0"
}
}
If Twilio is missing, add it manually and run npm install again. If the version is old, update it with npm install twilio@latest.
Step 5: Use the Correct Import Syntax
Make sure you import the module correctly. In Node.js, use require for CommonJS or import for ES modules. Here is an example with require:
// Correct way to import twilio
const twilio = require('twilio');
// Now you can use the client
const client = new twilio(accountSid, authToken);
If you are using ES modules, enable it in package.json with "type": "module". Then use import:
// For ES modules
import twilio from 'twilio';
const client = new twilio(accountSid, authToken);
Using the wrong syntax can cause the "Cannot find module" error. Check your file extension too. CommonJS uses .js, while ES modules use .mjs or "type": "module".
Step 6: Check Global Installations
If you installed Twilio globally with npm install -g twilio, Node.js may not find it. Local scripts require local modules. Remove the global version and install locally:
npm uninstall -g twilio
npm install twilio
This ensures your project uses the correct version. For more help with module errors, see our guide on Fix Node.js Error: Cannot find module.
Step 7: Clear npm Cache
A corrupted npm cache can cause installation problems. Clear the cache and reinstall:
npm cache clean --force
npm install
This removes all cached data. After this, the Twilio module should install correctly.
Example: Working Twilio Code
Here is a complete example that sends an SMS. Make sure you have your Twilio credentials ready:
// Import the twilio module
const twilio = require('twilio');
// Your Account SID and Auth Token from twilio.com/console
const accountSid = 'your_account_sid_here';
const authToken = 'your_auth_token_here';
// Create a new Twilio client
const client = new twilio(accountSid, authToken);
// Send an SMS message
client.messages
.create({
body: 'Hello from Node.js!',
from: '+1234567890', // Your Twilio number
to: '+0987654321' // Recipient's number
})
.then(message => console.log('Message sent: ' + message.sid))
.catch(error => console.error('Error: ' + error.message));
Output should look like this:
Message sent: SM1234567890abcdef
If you still get the "Cannot find module" error, double-check your installation. Also, review our Fix Node.js Error: Cannot find module article for more tips.
Conclusion
The "Cannot find module 'twilio'" error is easy to fix. Start by installing the module locally with npm install twilio. Check your working directory and package.json file. If needed, delete and reinstall node_modules. Use the correct import syntax for your module system. Finally, clear the npm cache if problems persist.
With these steps, you can get back to building your Twilio application. Remember to always install dependencies locally and verify your project structure. Happy coding!