Using the twilio library

Hello everybody
I am trying to use the twilio library to send sms.
Whether I use an atmospere (dispatch:twilio) package or directly use the Twilio API, I get the following error:

Uncaught Error: Cannot find module 'crypto'
    at makeMissingError (modules-runtime-hot.js?hash=1baba75a2760fb8ea6e194ad2f188f86fb440126:234:12)
    at Module.require (modules-runtime-hot.js?hash=1baba75a2760fb8ea6e194ad2f188f86fb440126:281:17)
    at require (modules-runtime-hot.js?hash=1baba75a2760fb8ea6e194ad2f188f86fb440126:310:21)
    at module (modules.js?hash=73d11dc623cbee3f26c15ad157d20c0e9478cd6b:316228:14)
    at fileEvaluate (modules-runtime-hot.js? ......

I couldn’t find a solution on the net.
Even twilio support couldn’t find an answer.

Can someone help me please.
Thank you
YC

1 Like

Have you tried running

meteor npm install crypto

from your app directory?

If you install with meteor add dispatch:twilio it may of not had the dependencies bundled, check the command line output when you start your app usually it will tell you something like module missing and the npm line to run, it may need the --save flag

Of course.
In my package.json file I have:

  "dependencies": {
    "@babel/runtime": "^7.15.4",
    "crypto-js": "^3.1.9-1",
    "dropbox": "^5.2.1",
    "es6-promise": "^4.2.8",
    "isomorphic-fetch": "^3.0.0",
    "jquery": "^3.6.0",
    "meteor-node-stubs": "^1.1.0",
    "ncrypt-js": "^2.0.0",
    "node-mailjet": "^3.3.5",
    "simpl-schema": "^1.12.0",
    "twilio": "^3.73.1"
  },

Try installing the package meteor-node-stubs

Where can I find this package please

Where you can you should try to use npm modules instead of Meteor atmosphere packages, unless the atmosphere package adds something particularly useful.

I’ve been using npm twilio@3.36.0 for years without any problems or dependency issues. Current version is 3.71.3: twilio - npm

package-lock.json

    "twilio": {
      "version": "3.36.0",
      "resolved": "https://registry.npmjs.org/twilio/-/twilio-3.36.0.tgz",
      "integrity": "sha512-rj042yEMNjE+Ze3XL2SMvxJOLik8EEMu+nkEc7sbf8c/9cdsJfdKienl9eEWHjsT2H5xn3zqZHDNvZLypA2Nxg==",
      "requires": {
        "@types/express": "^4.17.1",
        "deprecate": "1.0.0",
        "jsonwebtoken": "^8.5.1",
        "lodash": "^4.17.15",
        "moment": "^2.24.0",
        "q": "2.0.x",
        "request": "^2.88.0",
        "rootpath": "0.1.2",
        "scmp": "2.0.0",
        "xmlbuilder": "9.0.1"
      },

Then in my code (typescript):

import twilio from 'twilio';

const twilioSettings = Meteor.settings.private.twilio[Meteor.isProduction || false ? 'production':'sandbox'] as {sid: string, authToken: string, myNumber: string};
const twilioClient = twilio(twilioSettings.sid, twilioSettings.authToken);
const twilioMessagesCreateAsync = Meteor.wrapAsync(twilioClient.messages.create, twilioClient.messages);

const msgTemplate = {
	from: twilioSettings.myNumber,
	statusCallback: ...
}

export function smsSend(number: string, message: string) {
	try {
		const result = twilioMessagesCreateAsync({
			...msgTemplate,
			to,
			body: message+" "+Meteor.absoluteUrl(),
		});
	...
2 Likes

But I don’t use Atmosphèere packages, but the twilio api.
To isolate the problem, I created a new meteor application with just a button to send an sms and just for the test with the hard-coded authentication parameters.
in package.json

 "dependencies": {
   "@babel/runtime": "^7.15.4",
   "jquery": "^3.6.0",
   "meteor-node-stubs": "^1.1.0",
   "twilio": "^3.73.1"
 },

The code

    let client = require('twilio')("AC0c ....19","6a ..... 5");
    client.messages
        .create({body: 'Hi there', from: '+18106443545', to: '+33785370964'})
        .then(message => console.log(message.sid));

I no longer have the error concerning “crypto”, but a new error concerning “url.URL” appears

Uncaught TypeError: url.URL is not a constructor
    at Twilio.request (modules.js?hash=41b4a3a5c49c3dc70b25c857f3576c4a95442ef7:1528:13)
    at Api.Domain.request (modules.js?hash=41b4a3a5c49c3dc70b25c857f3576c4a95442ef7:287895:22)
    at V2010.Version.request (modules.js?hash=41b4a3a5c49c3dc70b25c857f3576c4a95442ef7:288297:23)
    ....

What is this new error?
Do I need to load other packages?

Thanks for your interest in my problem.
YC

The easiest solution is to use the twilio npm package and just follow the node examples in their documentation. Definitely do not use atmosphere packages for this. And using their API via REST is also possible, but as you are experiencing, a bit more complex than using their npm package.

Look at the code, it’s the twilio api I’m using.
However, I have the error on url.URL

Hi @yvancoyaud. There is a simple miscommunication - when you say you are using the “twilio api”, you make it sound like you are using the API directly via their HTTP endpoints. Yes you are using their api, but you are using it wrapped by the official twilio npm package, which as you say, is clear from your code.

Are you running this code on the Client, or on the Server? Twilio is only designed to run on the server - for one thing you don’t want to expose your api key and secret to the public in the browser. To send a message from the client you need to use a server-only Metor.method (a method who’s definition is not imported in the client) and then call that method from the client side code (the method should include some authentication/validation to make sure the someone is logged in and authorized to send messages).

Note that in my example, my Twilio settings are stored in Meteor.settings.private so they are not exposed to the client.

If you still get this issue on the server, try using the exact version I’m using with meteor npm install -s twilio@3.36.0.

Since you’re using the npm module ‘twilio’, the next source of help will be the issues page for this module. Visit www.npmjs.com and search for ‘twilio’, and you should find this page: https://www.npmjs.com/package/twilio

Fortunately this package is hosted on github which means it’s open source:

Click on that link then click on the Issues tab. In the search box remove the is:open and add your exact error message: https://github.com/twilio/twilio-node/issues?q=is%3Aissue+url.url+is+not+a+constructor

As you see, other people have had this problem and the solutions appear to be using particular versions of the library with particular versions of node, and only running it on the server. That’s why I suggest using the version which works for me.

4 Likes

Thanks wildhart for your long and detailed answer.
I actually passed the execution of my code on the server side, and now everything works correctly.
A Twilio support technician gave me this answer:
Unfortunately, you can’t use this code on the client side, since this library is only for back-end application due to the potential exposure of your account credentials.

Merci Ă  tous pour votre aide.
YC

3 Likes

De rein!

(post must be at least 20 characters)