Require() doesn't work

Hello,
I am new to WixCode and i have an issue.
I am trying to use twilio api to send an sms after a form is submitted. I followed Twilio documentation about the process and used a simple code to send sms after a button is clicked. The problem is, the documentation says that i need to use the ‘Require()’ function, and, after some searching i learn that function does not exist, and i couldn’t find anything to substitude without giving me an error. How can i change this to work my needs?
I followed the documentation here: Programmable SMS Quickstarts | Twilio

Thank you

You can install the Twilio NPM library from the Wix Code Package Manager.


See the article Wix Code: How to Manage External Code Libraries with the Package Manager for information .

Hey there,

I have done all of that. Installed the node, followed the instructions, but it still shows me an error. The require(‘twilio’) still shows me an error.

Edit: When i check the console, it shows me the following error as well
" Cannot find module ‘twilio’ in ‘public/pages/t218p.js’ "

require error is ok, the code will work regardless the error

The reason for your code not working must be something else. If you paste your code here, we can have a look

Thank you @shantanukumar847 ,
My code is simple i just want to send a message. The code is the following:

import twilio from ‘twilio’;

export function getTwilioName() {
return com.twilio.Twilio();
}

const accountSid = ‘CodeSID’;
const authToken = ‘CodeToken’;
const client = require (‘twilio’)(accountSid, authToken);

export function button1_click(event) {
client.messages
.create({ from : ‘+351927942821’, body: makeid(), to: ‘351934665900’})
.then(message => console.log(message.sid))
.done();
}

I’m problably missing something crucial here, if you could help here, i would thank you a lot.

Never write your API Key or any sensitive information on the page code.

You should create a .jsw file in your backend which will contain this

//twilioSms.jsw

import Twillo from 'twilio'; 

const accountSid = 'ACa10sddw0ef20ecf46ced49674'; //your accountSid
const authToken = 'your_auth_token'; // your authToken
const client = require('twilio')(accountSid, authToken); //ignore the error

export function sendSms(msg, number) {
    client.messages
      .create({from: '+15017122661', body: msg, to: number})
      .then(message => console.log(message.sid))
      .done();
}

Then call the function from your Page code like this

import {sendSms} from 'backend/twilioSms';

export function button1_click(event) {
    const msg = 'This is a message'; //replace it with the input or text field key
    const number = '+0000000000' //this is the number you want to send it to
    sendSms(msg, number); 
}

I have not tested it but it should work like this

Hey,
First of all, the api key was my mistake and i removed right away, thank you for warning.
Second, the code works great, i assumed that i had to put everything in one place. It was my bad.

Thank you for your help.

Hi,
I followed the script. It worked great if I hard coded the outbound phone number like
const number = ‘+16472092190’ : Example text number
However, if I tried to get the phone number from the input text,
var number = $w(‘#input1’).value; or
var number = ‘+1’ + $w(‘#input1’).value;
It didn’t work. Can you pls provide me some idea how to fix it??
https://zippyitsolutions.wixsite.com/mvp1


Make sure you give some time for the input fields to properly update. See this forum post for more information.

Hello,
I followed the same code and tried to send a SMS to my mobile via my Wix website but nothing is happening. I am not sure what I might be doing wrong here. It will be great if someone can help me out with this.
Here is my code:

// Filename: backend/twiliob.jsw
import twilio from ‘twilio’ ;
const accountSid = ‘abc’ ; //your accountSid
const authToken = ‘xyz’ ; // your authToken
const client = require (‘twilio’)(accountSid, authToken);
export function sendSms(msg, number) {
client.messages
.create({ from : ‘+10000000000’ , body: msg, to: number})
.then(message => console.log(message.sid))
.done() ;
}

//TODO: write your page related code here…import com.twilio.Twilio;
import {sendSms} from ‘backend/twiliob’ ;

export function button10_onClick(event) {
const msg = ‘This is a message’ ;
const number = ‘+13333333333’
sendSms(msg, number); }

If you are sending the message to an Indian number then you might have issues due to local regulations related to delivery of text messages to numbers who have not consented. Check with Twilio’s help center for more info.