2

I am using Twilio Api in my project and I am unable to import the api in my typescript file, when do so it give errors. If I am using this api in my javascript file then it works fine but in typescript it doesn't work.

These are the errors -


ERROR in ./node_modules/aws-sign2/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/aws-sign2'
ERROR in ./node_modules/aws4/aws4.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/aws4'
ERROR in ./node_modules/ecc-jsbn/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/ecc-jsbn'
ERROR in ./node_modules/http-signature/lib/signer.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/http-signature/lib'
ERROR in ./node_modules/http-signature/lib/verify.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/http-signature/lib'
ERROR in ./node_modules/oauth-sign/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/oauth-sign'
ERROR in ./node_modules/request/lib/helpers.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/request/lib'
ERROR in ./node_modules/request/lib/hawk.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/request/lib'
ERROR in ./node_modules/request/lib/oauth.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/request/lib'
ERROR in ./node_modules/scmp/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/scmp'
ERROR in ./node_modules/sshpk/lib/key.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib'
ERROR in ./node_modules/sshpk/lib/fingerprint.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib'
ERROR in ./node_modules/sshpk/lib/signature.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib'
ERROR in ./node_modules/sshpk/lib/private-key.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib'
ERROR in ./node_modules/sshpk/lib/certificate.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib'
ERROR in ./node_modules/sshpk/lib/identity.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib'
ERROR in ./node_modules/sshpk/lib/dhe.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib'
ERROR in ./node_modules/sshpk/lib/utils.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib'
ERROR in ./node_modules/sshpk/lib/formats/pem.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib/formats'
ERROR in ./node_modules/sshpk/lib/formats/ssh-private.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib/formats'
ERROR in ./node_modules/sshpk/lib/formats/openssh-cert.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/sshpk/lib/formats'
ERROR in ./node_modules/twilio/lib/webhooks/webhooks.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/twilio/lib/webhooks'
ERROR in ./node_modules/twilio/node_modules/jwa/index.js
Module not found: Error: Can't resolve 'crypto' in '/home/prajwal/Work/tracexpence/node_modules/twilio/node_modules/jwa'
ERROR in ./node_modules/deprecate/index.js
Module not found: Error: Can't resolve 'os' in '/home/prajwal/Work/tracexpence/node_modules/deprecate'
ERROR in ./node_modules/request/request.js
Module not found: Error: Can't resolve 'zlib' in '/home/prajwal/Work/tracexpence/node_modules/request'

This is import in typescript file-

import * as twilio from 'twilio';

    const obj = {
        'accountSid': 'Axxxxxxxxxxxxxxxxxxxxxxx',
        'authToken': 'exxxxxxxx',
    };

    const client = twilio(obj.accountSid, obj.authToken);

Prajwal
  • 95
  • 10

2 Answers2

4

Twilio developer evangelist here.

The Twilio Node module is not intended for use in client side JavaScript, which is why it is failing for your Angular application.

This is because the Twilio Node module makes use of your Account SID and Auth Token. You should not store or use your Auth Token in the client side because it could be intercepted by a malicious user and used to abuse your account.

Instead you should implement your Twilio API usage on the server side and then call to your server from your Angular application.

philnash
  • 70,667
  • 10
  • 60
  • 88
0

You cannot use Twilio library inside Angular project.

This is caused by dependencies importing node modules that are not available in the browser. Add to your package.json the modules that give errors:

"browser": {
    "http": false,
    "https":false,
    "net": false,
    "path": false,
    "stream": false,
    "tls": false
}
Mises
  • 4,251
  • 2
  • 19
  • 32
  • And it will not work for you. U cannot USE Twilio library inside any Angular 6+ project. – Mises Jun 28 '19 at 20:11
  • I dont know if u use any database but i know that for `Angular` good database is `firebase.com`. In there you can host your Angular app and upload there `Functions` in `Firebase Cloud Functions` u can use `Twilio` library. – Mises Jun 28 '19 at 20:19