Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to import Twilio #1016

Open
Manbearpixel opened this issue Apr 4, 2024 · 8 comments
Open

Unable to import Twilio #1016

Manbearpixel opened this issue Apr 4, 2024 · 8 comments
Labels
status: help wanted requesting help from the community status: waiting for feedback waiting for feedback from the submitter

Comments

@Manbearpixel
Copy link

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:

/Users/developer/git/project/node_modules/twilio/lib/base/RequestClient.js:1
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/developer/git/project/node_modules/twilio/node_modules/axios/index.js not supported.
Instead change the require of index.js in null to a dynamic import() which is available in all CommonJS modules.

Steps to Reproduce

  1. npm i twilio -S
  2. Run code snippet

Code Snippet

import twilio from 'twilio'

const accountSid = 'xxx'
    const authToken = 'xxx' //ENV var
    const client = twilio(accountSid, authToken)

    client.messages
      .create({
          body: 'Hello from Twilio',
          from: '+1xxx',
          to: '+1xxx'
      })
      .then(message => {
        console.log(message.sid)
      })
      .done()

Exception/Log

# paste exception/log here

Technical details:

  • twilio-node version: 5.0.3
  • node version: v16.20.2
@tiwarishubham635
Copy link
Contributor

Hi @Manbearpixel! I just tried doing the fresh install and it works for me. Things to remember:

  1. Make sure you have "type": "module" present in your package.json
  2. I am not sure why you are using .done() in the last line. I removed it and it worked perfectly for me
    Let me know if you still face issue. Thanks!

@ehaynes99
Copy link

Make sure you have "type": "module" present in your package.json

That is a gigantic undertaking if you don't have it there already.

When libraries used in node decide to convert to ESM only, the overwhelming majority of people just use the previous version.

npm i twilio@^4

@tiwarishubham635
Copy link
Contributor

When libraries used in node decide to convert to ESM only, the overwhelming majority of people just use the previous version.

npm i twilio@^4

Does that mean twilio V4 was working without the package.json requirement but it is not the case with V5?

@ehaynes99
Copy link

Hrmm, actually, no, v5 works with a commonjs project as well. axios has dual publishing, so it can be loaded either way.

this works fine:

npm init
npm i twilio
# copy sample above into index.js
node index.js

I tried a couple of different combinations with TS, but couldn't reproduce there either. Sorry for the diversion.

@Manbearpixel
Copy link
Author

Hi @ehaynes99 @tiwarishubham635

I am still encountering this issue even in Twilio@4. I have not created this project as a "module" only initiative. Mostly because a lot of dependencies don't support ESM, for example: Knex

require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'project-x/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and 'project-x/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///project-x/knexfile.js:1:1
    at ModuleJob.run (node:internal/modules/esm/module_job:193:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:530:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)
    at async openKnexfile (/project-x/node_modules/knex/bin/cli.js:36:16)
    at async initKnex (/project-x/node_modules/knex/bin/cli.js:61:7)
    at async Command.<anonymous> (/project-x/node_modules/knex/bin/cli.js:223:26)

I was able to get it to work using twilio@3 however. I guess Twilio@4 also uses an updated version of axios which does not have dual loaders?

@tiwarishubham635
Copy link
Contributor

Hey @Manbearpixel! As mentioned by @ehaynes99, we are using axios with 1.6.8. As per the package.json, it supports dual publishing. I would suggest if your project is not module only, you can try using require also

@ehaynes99
Copy link

I have not created this project as a "module" only initiative.

The error suggests otherwise:

This file is being treated as an ES module because it has a '.js' file extension and 'project-x/package.json' contains "type": "module".

@tiwarishubham635
Copy link
Contributor

I think if you are using with "type": "module", then you can easily use import and the following code snippet:

import twilio from 'twilio'

const accountSid = 'xxx'
const authToken = 'xxx' //ENV var
const client = twilio(accountSid, authToken)

client.messages
  .create({
      body: 'Hello from Twilio',
      from: '+1xxx',
      to: '+1xxx'
  })
  .then(message => {
    console.log(message.sid)
  })
  .catch(error => {
        console.log(error);
  });

However, if "type": "module" is a change that you don't want to make, go for the require method:

const accountSid = 'xxx'
const authToken = 'xxx' //ENV var
const client = require('twilio')(accountSid, authToken);

client.messages
  .create({
      body: 'Hello from Twilio',
      from: '+1xxx',
      to: '+1xxx'
  })
  .then(message => {
    console.log(message.sid)
  })
  .catch(error => {
        console.log(error);
  });

I hope this answers your queries.

@tiwarishubham635 tiwarishubham635 added status: help wanted requesting help from the community status: waiting for feedback waiting for feedback from the submitter labels May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: help wanted requesting help from the community status: waiting for feedback waiting for feedback from the submitter
Projects
None yet
Development

No branches or pull requests

3 participants